| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 5 |
// GCC is free software; you can redistribute it and/or modify it under |
| 6 |
// the terms of the GNU General Public License as published by the Free |
| 7 |
// Software Foundation; either version 3, or (at your option) any later |
| 8 |
// version. |
| 9 |
|
| 10 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
// for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with GCC; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
#include "rust-hir-type-check-pattern.h" |
| 20 |
#include "rust-hir-pattern.h" |
| 21 |
#include "rust-hir-type-check-expr.h" |
| 22 |
#include "rust-type-util.h" |
| 23 |
#include "rust-immutable-name-resolution-context.h" |
| 24 |
|
| 25 |
// for flag_name_resolution_2_0 |
| 26 |
#include "options.h" |
| 27 |
|
| 28 |
namespace Rust { |
| 29 |
namespace Resolver { |
| 30 |
|
| 31 |
TypeCheckPattern::TypeCheckPattern (TyTy::BaseType *parent) |
| 32 |
: TypeCheckBase (), parent (parent), infered (new TyTy::ErrorType (0)) |
| 33 |
{} |
| 34 |
|
| 35 |
TyTy::BaseType * |
| 36 |
TypeCheckPattern::Resolve (HIR::Pattern &pattern, TyTy::BaseType *parent) |
| 37 |
{ |
| 38 |
TypeCheckPattern resolver (parent); |
| 39 |
pattern.accept_vis (resolver); |
| 40 |
|
| 41 |
if (resolver.infered == nullptr) |
| 42 |
return new TyTy::ErrorType (pattern.get_mappings ().get_hirid ()); |
| 43 |
|
| 44 |
resolver.context->insert_type (pattern.get_mappings (), resolver.infered); |
| 45 |
return resolver.infered; |
| 46 |
} |
| 47 |
|
| 48 |
void |
| 49 |
TypeCheckPattern::visit (HIR::PathInExpression &pattern) |
| 50 |
{ |
| 51 |
// Pattern must be enum variants, sturcts, constants, or associated constansts |
| 52 |
TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (pattern); |
| 53 |
|
| 54 |
NodeId ref_node_id = UNKNOWN_NODEID; |
| 55 |
bool maybe_item = false; |
| 56 |
|
| 57 |
if (flag_name_resolution_2_0) |
| 58 |
{ |
| 59 |
auto &nr_ctx |
| 60 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 61 |
|
| 62 |
if (auto id = nr_ctx.lookup (pattern.get_mappings ().get_nodeid ())) |
| 63 |
{ |
| 64 |
ref_node_id = *id; |
| 65 |
maybe_item = true; |
| 66 |
} |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
maybe_item |= resolver->lookup_resolved_name ( |
| 71 |
pattern.get_mappings ().get_nodeid (), &ref_node_id); |
| 72 |
maybe_item |= resolver->lookup_resolved_type ( |
| 73 |
pattern.get_mappings ().get_nodeid (), &ref_node_id); |
| 74 |
} |
| 75 |
|
| 76 |
bool path_is_const_item = false; |
| 77 |
|
| 78 |
if (maybe_item) |
| 79 |
{ |
| 80 |
tl::optional<HirId> definition_id |
| 81 |
= mappings.lookup_node_to_hir (ref_node_id); |
| 82 |
rust_assert (definition_id.has_value ()); |
| 83 |
HirId def_id = definition_id.value (); |
| 84 |
|
| 85 |
tl::optional<HIR::Item *> hir_item = mappings.lookup_hir_item (def_id); |
| 86 |
// If the path refrerences an item, it must be constants or structs. |
| 87 |
if (hir_item.has_value ()) |
| 88 |
{ |
| 89 |
HIR::Item *item = hir_item.value (); |
| 90 |
if (item->get_item_kind () == HIR::Item::ItemKind::Constant) |
| 91 |
{ |
| 92 |
path_is_const_item = true; |
| 93 |
} |
| 94 |
else if (item->get_item_kind () != HIR::Item::ItemKind::Struct) |
| 95 |
{ |
| 96 |
HIR::Item *item = hir_item.value (); |
| 97 |
std::string item_kind |
| 98 |
= HIR::Item::item_kind_string (item->get_item_kind ()); |
| 99 |
|
| 100 |
std::string path_buf; |
| 101 |
for (size_t i = 0; i < pattern.get_segments ().size (); i++) |
| 102 |
{ |
| 103 |
HIR::PathExprSegment &seg = pattern.get_segments ().at (i); |
| 104 |
path_buf += seg.as_string (); |
| 105 |
if (i != pattern.get_segments ().size () - 1) |
| 106 |
path_buf += "::"; |
| 107 |
} |
| 108 |
|
| 109 |
rich_location rich_locus ( |
| 110 |
line_table, pattern.get_final_segment ().get_locus ()); |
| 111 |
rich_locus.add_fixit_replace ( |
| 112 |
"not a unit struct, unit variant or constant"); |
| 113 |
rust_error_at (rich_locus, ErrorCode::E0532, |
| 114 |
"expected unit struct, unit variant or constant, " |
| 115 |
"found %s %<%s%>", |
| 116 |
item_kind.c_str (), path_buf.c_str ()); |
| 117 |
return; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// If the path is a constructor, it must be a unit struct or unit variants. |
| 123 |
if (!path_is_const_item && pattern_ty->get_kind () == TyTy::TypeKind::ADT) |
| 124 |
{ |
| 125 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (pattern_ty); |
| 126 |
rust_assert (adt->get_variants ().size () > 0); |
| 127 |
|
| 128 |
TyTy::VariantDef *variant = adt->get_variants ().at (0); |
| 129 |
if (adt->is_enum ()) |
| 130 |
{ |
| 131 |
HirId variant_id = UNKNOWN_HIRID; |
| 132 |
bool ok = context->lookup_variant_definition ( |
| 133 |
pattern.get_mappings ().get_hirid (), &variant_id); |
| 134 |
rust_assert (ok); |
| 135 |
|
| 136 |
ok = adt->lookup_variant_by_id (variant_id, &variant); |
| 137 |
rust_assert (ok); |
| 138 |
} |
| 139 |
|
| 140 |
if (variant->get_variant_type () != TyTy::VariantDef::VariantType::NUM) |
| 141 |
{ |
| 142 |
std::string variant_type = TyTy::VariantDef::variant_type_string ( |
| 143 |
variant->get_variant_type ()); |
| 144 |
|
| 145 |
rich_location rich_locus (line_table, |
| 146 |
pattern.get_final_segment ().get_locus ()); |
| 147 |
rich_locus.add_fixit_replace ( |
| 148 |
"not a unit struct, unit variant or constatnt"); |
| 149 |
rust_error_at (rich_locus, ErrorCode::E0532, |
| 150 |
"expected unit struct, unit variant or constant, " |
| 151 |
"found %s variant %<%s::%s%>", |
| 152 |
variant_type.c_str (), adt->get_name ().c_str (), |
| 153 |
variant->get_identifier ().c_str ()); |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
infered = pattern_ty; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
void |
| 162 |
TypeCheckPattern::visit (HIR::TupleStructPattern &pattern) |
| 163 |
{ |
| 164 |
TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (pattern.get_path ()); |
| 165 |
if (pattern_ty->get_kind () != TyTy::TypeKind::ADT) |
| 166 |
{ |
| 167 |
rust_error_at ( |
| 168 |
pattern.get_locus (), ErrorCode::E0532, |
| 169 |
"expected tuple struct or tuple variant, found function %qs", |
| 170 |
pattern_ty->get_name ().c_str ()); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
infered = pattern_ty; |
| 175 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (infered); |
| 176 |
rust_assert (adt->number_of_variants () > 0); |
| 177 |
|
| 178 |
TyTy::VariantDef *variant = adt->get_variants ().at (0); |
| 179 |
if (adt->is_enum ()) |
| 180 |
{ |
| 181 |
HirId variant_id = UNKNOWN_HIRID; |
| 182 |
bool ok = context->lookup_variant_definition ( |
| 183 |
pattern.get_path ().get_mappings ().get_hirid (), &variant_id); |
| 184 |
rust_assert (ok); |
| 185 |
|
| 186 |
ok = adt->lookup_variant_by_id (variant_id, &variant); |
| 187 |
rust_assert (ok); |
| 188 |
} |
| 189 |
|
| 190 |
// error[E0532]: expected tuple struct or tuple variant, found struct |
| 191 |
// variant `Foo::D`, E0532 by rustc 1.49.0 , E0164 by rustc 1.71.0 |
| 192 |
if (variant->get_variant_type () != TyTy::VariantDef::VariantType::TUPLE) |
| 193 |
{ |
| 194 |
std::string variant_type |
| 195 |
= TyTy::VariantDef::variant_type_string (variant->get_variant_type ()); |
| 196 |
|
| 197 |
rich_location rich_locus (line_table, pattern.get_locus ()); |
| 198 |
rich_locus.add_fixit_replace ("not a tuple struct or tuple variant"); |
| 199 |
rust_error_at ( |
| 200 |
rich_locus, ErrorCode::E0164, |
| 201 |
"expected tuple struct or tuple variant, found %s variant %<%s::%s%>", |
| 202 |
variant_type.c_str (), adt->get_name ().c_str (), |
| 203 |
variant->get_identifier ().c_str ()); |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
// check the elements |
| 208 |
// error[E0023]: this pattern has 2 fields, but the corresponding tuple |
| 209 |
// variant has 1 field |
| 210 |
// error[E0023]: this pattern has 0 fields, but the corresponding tuple |
| 211 |
// variant has 1 field |
| 212 |
|
| 213 |
auto &items = pattern.get_items (); |
| 214 |
switch (items.get_item_type ()) |
| 215 |
{ |
| 216 |
case HIR::TupleStructItems::RANGED: { |
| 217 |
// TODO |
| 218 |
rust_unreachable (); |
| 219 |
} |
| 220 |
break; |
| 221 |
|
| 222 |
case HIR::TupleStructItems::MULTIPLE: { |
| 223 |
HIR::TupleStructItemsNoRange &items_no_range |
| 224 |
= static_cast<HIR::TupleStructItemsNoRange &> (items); |
| 225 |
|
| 226 |
if (items_no_range.get_patterns ().size () != variant->num_fields ()) |
| 227 |
{ |
| 228 |
rust_error_at ( |
| 229 |
pattern.get_locus (), ErrorCode::E0023, |
| 230 |
"this pattern has %lu fields but the corresponding " |
| 231 |
"tuple variant has %lu field", |
| 232 |
(unsigned long) items_no_range.get_patterns ().size (), |
| 233 |
(unsigned long) variant->num_fields ()); |
| 234 |
// we continue on to try and setup the types as best we can for |
| 235 |
// type checking |
| 236 |
} |
| 237 |
|
| 238 |
// iterate the fields and set them up, I wish we had ZIP |
| 239 |
size_t i = 0; |
| 240 |
for (auto &pattern : items_no_range.get_patterns ()) |
| 241 |
{ |
| 242 |
if (i >= variant->num_fields ()) |
| 243 |
break; |
| 244 |
|
| 245 |
TyTy::StructFieldType *field = variant->get_field_at_index (i++); |
| 246 |
TyTy::BaseType *fty = field->get_field_type (); |
| 247 |
|
| 248 |
// setup the type on this pattern type |
| 249 |
context->insert_type (pattern->get_mappings (), fty); |
| 250 |
TypeCheckPattern::Resolve (*pattern, fty); |
| 251 |
} |
| 252 |
} |
| 253 |
break; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
void |
| 258 |
emit_invalid_field_error (location_t loc, Rust::TyTy::VariantDef *variant, |
| 259 |
const std::string &name) |
| 260 |
{ |
| 261 |
rust_error_at (loc, ErrorCode::E0026, |
| 262 |
"variant %s does not have a field named %s", |
| 263 |
variant->get_identifier ().c_str (), name.c_str ()); |
| 264 |
} |
| 265 |
|
| 266 |
void |
| 267 |
TypeCheckPattern::visit (HIR::StructPattern &pattern) |
| 268 |
{ |
| 269 |
TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (pattern.get_path ()); |
| 270 |
if (pattern_ty->get_kind () != TyTy::TypeKind::ADT) |
| 271 |
{ |
| 272 |
rust_error_at (pattern.get_locus (), |
| 273 |
"expected tuple struct/variant, found: %s", |
| 274 |
pattern_ty->get_name ().c_str ()); |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
infered = pattern_ty; |
| 279 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (infered); |
| 280 |
if (adt->number_of_variants () == 0) |
| 281 |
{ |
| 282 |
HIR::PathInExpression &path = pattern.get_path (); |
| 283 |
const AST::SimplePath &sp = path.as_simple_path (); |
| 284 |
rust_error_at (pattern.get_locus (), ErrorCode::E0574, |
| 285 |
"expected struct, variant or union type, found enum %qs", |
| 286 |
sp.as_string ().c_str ()); |
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
TyTy::VariantDef *variant = adt->get_variants ().at (0); |
| 291 |
if (adt->is_enum ()) |
| 292 |
{ |
| 293 |
HirId variant_id = UNKNOWN_HIRID; |
| 294 |
bool ok = context->lookup_variant_definition ( |
| 295 |
pattern.get_path ().get_mappings ().get_hirid (), &variant_id); |
| 296 |
if (!ok) |
| 297 |
{ |
| 298 |
HIR::PathInExpression &path = pattern.get_path (); |
| 299 |
const AST::SimplePath &sp = path.as_simple_path (); |
| 300 |
rust_error_at ( |
| 301 |
pattern.get_locus (), ErrorCode::E0574, |
| 302 |
"expected struct, variant or union type, found enum %qs", |
| 303 |
sp.as_string ().c_str ()); |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
ok = adt->lookup_variant_by_id (variant_id, &variant); |
| 308 |
rust_assert (ok); |
| 309 |
} |
| 310 |
|
| 311 |
// error[E0532]: expected tuple struct or tuple variant, found struct |
| 312 |
// variant `Foo::D` |
| 313 |
if (variant->get_variant_type () != TyTy::VariantDef::VariantType::STRUCT) |
| 314 |
{ |
| 315 |
std::string variant_type |
| 316 |
= TyTy::VariantDef::variant_type_string (variant->get_variant_type ()); |
| 317 |
|
| 318 |
rich_location rich_locus (line_table, pattern.get_locus ()); |
| 319 |
std::string rich_msg = "use the tuple variant pattern syntax instead " |
| 320 |
+ variant->get_identifier () + "(_)"; |
| 321 |
rich_locus.add_fixit_replace (rich_msg.c_str ()); |
| 322 |
|
| 323 |
rust_error_at (rich_locus, ErrorCode::E0769, |
| 324 |
"%s variant %qs written as struct variant", |
| 325 |
variant_type.c_str (), |
| 326 |
variant->get_identifier ().c_str ()); |
| 327 |
return; |
| 328 |
} |
| 329 |
|
| 330 |
std::vector<std::string> named_fields; |
| 331 |
auto &struct_pattern_elems = pattern.get_struct_pattern_elems (); |
| 332 |
for (auto &field : struct_pattern_elems.get_struct_pattern_fields ()) |
| 333 |
{ |
| 334 |
switch (field->get_item_type ()) |
| 335 |
{ |
| 336 |
case HIR::StructPatternField::ItemType::TUPLE_PAT: { |
| 337 |
// TODO |
| 338 |
rust_unreachable (); |
| 339 |
} |
| 340 |
break; |
| 341 |
|
| 342 |
case HIR::StructPatternField::ItemType::IDENT_PAT: { |
| 343 |
HIR::StructPatternFieldIdentPat &ident |
| 344 |
= static_cast<HIR::StructPatternFieldIdentPat &> (*field); |
| 345 |
|
| 346 |
TyTy::StructFieldType *field = nullptr; |
| 347 |
if (!variant->lookup_field (ident.get_identifier ().as_string (), |
| 348 |
&field, nullptr)) |
| 349 |
{ |
| 350 |
emit_invalid_field_error (ident.get_locus (), variant, |
| 351 |
ident.get_identifier ().as_string ()); |
| 352 |
break; |
| 353 |
} |
| 354 |
named_fields.push_back (ident.get_identifier ().as_string ()); |
| 355 |
|
| 356 |
TyTy::BaseType *fty = field->get_field_type (); |
| 357 |
TypeCheckPattern::Resolve (ident.get_pattern (), fty); |
| 358 |
} |
| 359 |
break; |
| 360 |
|
| 361 |
case HIR::StructPatternField::ItemType::IDENT: { |
| 362 |
HIR::StructPatternFieldIdent &ident |
| 363 |
= static_cast<HIR::StructPatternFieldIdent &> (*field); |
| 364 |
|
| 365 |
TyTy::StructFieldType *field = nullptr; |
| 366 |
if (!variant->lookup_field (ident.get_identifier ().as_string (), |
| 367 |
&field, nullptr)) |
| 368 |
{ |
| 369 |
emit_invalid_field_error (ident.get_locus (), variant, |
| 370 |
ident.get_identifier ().as_string ()); |
| 371 |
break; |
| 372 |
} |
| 373 |
named_fields.push_back (ident.get_identifier ().as_string ()); |
| 374 |
|
| 375 |
// setup the type on this pattern |
| 376 |
TyTy::BaseType *fty = field->get_field_type (); |
| 377 |
context->insert_type (ident.get_mappings (), fty); |
| 378 |
} |
| 379 |
break; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
// check the elements |
| 384 |
if (adt->is_union ()) |
| 385 |
{ |
| 386 |
auto &struct_pattern_elems = pattern.get_struct_pattern_elems (); |
| 387 |
if (struct_pattern_elems.get_struct_pattern_fields ().size () != 1) |
| 388 |
rust_error_at (pattern.get_locus (), |
| 389 |
"union patterns should have exactly one field"); |
| 390 |
|
| 391 |
else |
| 392 |
{ |
| 393 |
switch (struct_pattern_elems.get_struct_pattern_fields () |
| 394 |
.at (0) |
| 395 |
->get_item_type ()) |
| 396 |
{ |
| 397 |
case HIR::StructPatternField::ItemType::IDENT: |
| 398 |
case HIR::StructPatternField::ItemType::IDENT_PAT: |
| 399 |
break; |
| 400 |
default: { |
| 401 |
auto first_elem |
| 402 |
= struct_pattern_elems.get_struct_pattern_fields () |
| 403 |
.at (0) |
| 404 |
->as_string (); |
| 405 |
rust_error_at (pattern.get_locus (), |
| 406 |
"%qs cannot be used in union patterns", |
| 407 |
first_elem.c_str ()); |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
else |
| 413 |
{ |
| 414 |
// Expects enum struct or struct struct. |
| 415 |
// error[E0027]: pattern does not mention fields `x`, `y` |
| 416 |
// error[E0026]: variant `Foo::D` does not have a field named `b` |
| 417 |
if (named_fields.size () != variant->num_fields ()) |
| 418 |
{ |
| 419 |
std::map<std::string, bool> missing_names; |
| 420 |
|
| 421 |
// populate with all fields |
| 422 |
for (auto &field : variant->get_fields ()) |
| 423 |
missing_names[field->get_name ()] = true; |
| 424 |
|
| 425 |
// then eliminate with named_fields |
| 426 |
for (auto &named : named_fields) |
| 427 |
missing_names.erase (named); |
| 428 |
|
| 429 |
// then get the list of missing names |
| 430 |
size_t i = 0; |
| 431 |
std::string missing_fields_str; |
| 432 |
for (auto it = missing_names.begin (); it != missing_names.end (); |
| 433 |
it++) |
| 434 |
{ |
| 435 |
bool has_next = (i + 1) < missing_names.size (); |
| 436 |
missing_fields_str += it->first + (has_next ? ", " : ""); |
| 437 |
i++; |
| 438 |
} |
| 439 |
|
| 440 |
rust_error_at (pattern.get_locus (), ErrorCode::E0027, |
| 441 |
"pattern does not mention fields %s", |
| 442 |
missing_fields_str.c_str ()); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
void |
| 448 |
TypeCheckPattern::visit (HIR::WildcardPattern &pattern) |
| 449 |
{ |
| 450 |
// wildcard patterns within the MatchArm's are simply just the same type as |
| 451 |
// the parent |
| 452 |
infered = parent->clone (); |
| 453 |
infered->set_ref (pattern.get_mappings ().get_hirid ()); |
| 454 |
} |
| 455 |
|
| 456 |
void |
| 457 |
TypeCheckPattern::visit (HIR::TuplePattern &pattern) |
| 458 |
{ |
| 459 |
std::unique_ptr<HIR::TuplePatternItems> items; |
| 460 |
switch (pattern.get_items ().get_item_type ()) |
| 461 |
{ |
| 462 |
case HIR::TuplePatternItems::ItemType::MULTIPLE: { |
| 463 |
auto &ref = static_cast<HIR::TuplePatternItemsMultiple &> ( |
| 464 |
pattern.get_items ()); |
| 465 |
|
| 466 |
auto resolved_parent = parent->destructure (); |
| 467 |
if (resolved_parent->get_kind () != TyTy::TUPLE) |
| 468 |
{ |
| 469 |
rust_error_at (pattern.get_locus (), "expected %s, found tuple", |
| 470 |
parent->as_string ().c_str ()); |
| 471 |
break; |
| 472 |
} |
| 473 |
|
| 474 |
const auto &patterns = ref.get_patterns (); |
| 475 |
size_t nitems_to_resolve = patterns.size (); |
| 476 |
|
| 477 |
TyTy::TupleType &par |
| 478 |
= *static_cast<TyTy::TupleType *> (resolved_parent); |
| 479 |
if (patterns.size () != par.get_fields ().size ()) |
| 480 |
{ |
| 481 |
emit_pattern_size_error (pattern, par.get_fields ().size (), |
| 482 |
patterns.size ()); |
| 483 |
nitems_to_resolve |
| 484 |
= std::min (nitems_to_resolve, par.get_fields ().size ()); |
| 485 |
} |
| 486 |
|
| 487 |
std::vector<TyTy::TyVar> pattern_elems; |
| 488 |
for (size_t i = 0; i < nitems_to_resolve; i++) |
| 489 |
{ |
| 490 |
auto &p = patterns[i]; |
| 491 |
TyTy::BaseType *par_type = par.get_field (i); |
| 492 |
|
| 493 |
TyTy::BaseType *elem = TypeCheckPattern::Resolve (*p, par_type); |
| 494 |
pattern_elems.push_back (TyTy::TyVar (elem->get_ref ())); |
| 495 |
} |
| 496 |
infered = new TyTy::TupleType (pattern.get_mappings ().get_hirid (), |
| 497 |
pattern.get_locus (), pattern_elems); |
| 498 |
} |
| 499 |
break; |
| 500 |
|
| 501 |
case HIR::TuplePatternItems::ItemType::RANGED: { |
| 502 |
// HIR::TuplePatternItemsRanged &ref |
| 503 |
// = *static_cast<HIR::TuplePatternItemsRanged *> ( |
| 504 |
// pattern.get_items ().get ()); |
| 505 |
// TODO |
| 506 |
rust_unreachable (); |
| 507 |
} |
| 508 |
break; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
void |
| 513 |
TypeCheckPattern::visit (HIR::LiteralPattern &pattern) |
| 514 |
{ |
| 515 |
infered = resolve_literal (pattern.get_mappings (), pattern.get_literal (), |
| 516 |
pattern.get_locus ()); |
| 517 |
} |
| 518 |
|
| 519 |
void |
| 520 |
TypeCheckPattern::visit (HIR::RangePattern &pattern) |
| 521 |
{ |
| 522 |
// Resolve the upper and lower bounds, and ensure they are compatible types |
| 523 |
TyTy::BaseType *upper = nullptr, *lower = nullptr; |
| 524 |
|
| 525 |
upper = typecheck_range_pattern_bound (pattern.get_upper_bound (), |
| 526 |
pattern.get_mappings (), |
| 527 |
pattern.get_locus ()); |
| 528 |
|
| 529 |
lower = typecheck_range_pattern_bound (pattern.get_lower_bound (), |
| 530 |
pattern.get_mappings (), |
| 531 |
pattern.get_locus ()); |
| 532 |
|
| 533 |
infered = unify_site (pattern.get_mappings ().get_hirid (), |
| 534 |
TyTy::TyWithLocation (upper), |
| 535 |
TyTy::TyWithLocation (lower), pattern.get_locus ()); |
| 536 |
} |
| 537 |
|
| 538 |
void |
| 539 |
TypeCheckPattern::visit (HIR::IdentifierPattern &pattern) |
| 540 |
{ |
| 541 |
if (!pattern.get_is_ref ()) |
| 542 |
{ |
| 543 |
infered = parent; |
| 544 |
return; |
| 545 |
} |
| 546 |
|
| 547 |
infered = new TyTy::ReferenceType (pattern.get_mappings ().get_hirid (), |
| 548 |
TyTy::TyVar (parent->get_ref ()), |
| 549 |
pattern.is_mut () ? Mutability::Mut |
| 550 |
: Mutability::Imm); |
| 551 |
} |
| 552 |
|
| 553 |
void |
| 554 |
TypeCheckPattern::visit (HIR::QualifiedPathInExpression &pattern) |
| 555 |
{ |
| 556 |
rust_sorry_at (pattern.get_locus (), |
| 557 |
"type checking qualified path patterns not supported"); |
| 558 |
} |
| 559 |
|
| 560 |
void |
| 561 |
TypeCheckPattern::visit (HIR::ReferencePattern &pattern) |
| 562 |
{ |
| 563 |
if (parent->get_kind () != TyTy::TypeKind::REF) |
| 564 |
{ |
| 565 |
rust_error_at (pattern.get_locus (), "expected %s, found reference", |
| 566 |
parent->as_string ().c_str ()); |
| 567 |
return; |
| 568 |
} |
| 569 |
|
| 570 |
auto &ref_ty_ty = static_cast<TyTy::ReferenceType &> (*parent); |
| 571 |
TyTy::BaseType *infered_base |
| 572 |
= TypeCheckPattern::Resolve (pattern.get_referenced_pattern (), |
| 573 |
ref_ty_ty.get_base ()); |
| 574 |
infered = new TyTy::ReferenceType (pattern.get_mappings ().get_hirid (), |
| 575 |
TyTy::TyVar (infered_base->get_ref ()), |
| 576 |
pattern.is_mut () ? Mutability::Mut |
| 577 |
: Mutability::Imm); |
| 578 |
} |
| 579 |
|
| 580 |
void |
| 581 |
TypeCheckPattern::visit (HIR::SlicePattern &pattern) |
| 582 |
{ |
| 583 |
rust_sorry_at (pattern.get_locus (), |
| 584 |
"type checking qualified path patterns not supported"); |
| 585 |
} |
| 586 |
|
| 587 |
void |
| 588 |
TypeCheckPattern::emit_pattern_size_error (const HIR::Pattern &pattern, |
| 589 |
size_t expected_field_count, |
| 590 |
size_t got_field_count) |
| 591 |
{ |
| 592 |
rich_location r (line_table, pattern.get_locus ()); |
| 593 |
r.add_range (mappings.lookup_location (parent->get_ref ())); |
| 594 |
rust_error_at (r, |
| 595 |
"expected a tuple with %lu %s, found one " |
| 596 |
"with %lu %s", |
| 597 |
(unsigned long) expected_field_count, |
| 598 |
expected_field_count == 1 ? "element" : "elements", |
| 599 |
(unsigned long) got_field_count, |
| 600 |
got_field_count == 1 ? "element" : "elements"); |
| 601 |
} |
| 602 |
|
| 603 |
TyTy::BaseType * |
| 604 |
TypeCheckPattern::typecheck_range_pattern_bound ( |
| 605 |
Rust::HIR::RangePatternBound &bound, Analysis::NodeMapping mappings, |
| 606 |
location_t locus) |
| 607 |
{ |
| 608 |
TyTy::BaseType *resolved_bound = nullptr; |
| 609 |
switch (bound.get_bound_type ()) |
| 610 |
{ |
| 611 |
case HIR::RangePatternBound::RangePatternBoundType::LITERAL: { |
| 612 |
auto &ref = static_cast<HIR::RangePatternBoundLiteral &> (bound); |
| 613 |
|
| 614 |
HIR::Literal lit = ref.get_literal (); |
| 615 |
|
| 616 |
resolved_bound = resolve_literal (mappings, lit, locus); |
| 617 |
} |
| 618 |
break; |
| 619 |
|
| 620 |
case HIR::RangePatternBound::RangePatternBoundType::PATH: { |
| 621 |
auto &ref = static_cast<HIR::RangePatternBoundPath &> (bound); |
| 622 |
|
| 623 |
resolved_bound = TypeCheckExpr::Resolve (ref.get_path ()); |
| 624 |
} |
| 625 |
break; |
| 626 |
|
| 627 |
case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: { |
| 628 |
auto &ref = static_cast<HIR::RangePatternBoundQualPath &> (bound); |
| 629 |
|
| 630 |
resolved_bound = TypeCheckExpr::Resolve (ref.get_qualified_path ()); |
| 631 |
} |
| 632 |
break; |
| 633 |
} |
| 634 |
|
| 635 |
return resolved_bound; |
| 636 |
} |
| 637 |
|
| 638 |
void |
| 639 |
TypeCheckPattern::visit (HIR::AltPattern &pattern) |
| 640 |
{ |
| 641 |
const auto &alts = pattern.get_alts (); |
| 642 |
|
| 643 |
// lub - taken from TypeCheckExpr::visit(ArrayExpr) |
| 644 |
std::vector<TyTy::BaseType *> types; |
| 645 |
for (auto &alt_pattern : alts) |
| 646 |
{ |
| 647 |
types.push_back (TypeCheckPattern::Resolve (*alt_pattern, parent)); |
| 648 |
} |
| 649 |
|
| 650 |
TyTy::BaseType *alt_pattern_type |
| 651 |
= TyTy::TyVar::get_implicit_infer_var (pattern.get_locus ()).get_tyty (); |
| 652 |
|
| 653 |
for (auto &type : types) |
| 654 |
{ |
| 655 |
alt_pattern_type |
| 656 |
= unify_site (pattern.get_mappings ().get_hirid (), |
| 657 |
TyTy::TyWithLocation (alt_pattern_type), |
| 658 |
TyTy::TyWithLocation (type, type->get_locus ()), |
| 659 |
pattern.get_locus ()); |
| 660 |
} |
| 661 |
|
| 662 |
infered = alt_pattern_type; |
| 663 |
} |
| 664 |
|
| 665 |
TyTy::BaseType * |
| 666 |
ClosureParamInfer::Resolve (HIR::Pattern &pattern) |
| 667 |
{ |
| 668 |
ClosureParamInfer resolver; |
| 669 |
pattern.accept_vis (resolver); |
| 670 |
|
| 671 |
if (resolver.infered->get_kind () != TyTy::TypeKind::ERROR) |
| 672 |
{ |
| 673 |
resolver.context->insert_implicit_type (resolver.infered->get_ref (), |
| 674 |
resolver.infered); |
| 675 |
resolver.mappings.insert_location (resolver.infered->get_ref (), |
| 676 |
pattern.get_locus ()); |
| 677 |
} |
| 678 |
return resolver.infered; |
| 679 |
} |
| 680 |
|
| 681 |
ClosureParamInfer::ClosureParamInfer () |
| 682 |
: TypeCheckBase (), infered (new TyTy::ErrorType (0)) |
| 683 |
{} |
| 684 |
|
| 685 |
void |
| 686 |
ClosureParamInfer::visit (HIR::WildcardPattern &pattern) |
| 687 |
{ |
| 688 |
HirId id = pattern.get_mappings ().get_hirid (); |
| 689 |
infered = new TyTy::InferType (id, TyTy::InferType::InferTypeKind::GENERAL, |
| 690 |
TyTy::InferType::TypeHint::Default (), |
| 691 |
pattern.get_locus ()); |
| 692 |
} |
| 693 |
|
| 694 |
void |
| 695 |
ClosureParamInfer::visit (HIR::IdentifierPattern &pattern) |
| 696 |
{ |
| 697 |
HirId id = pattern.get_mappings ().get_hirid (); |
| 698 |
infered = new TyTy::InferType (id, TyTy::InferType::InferTypeKind::GENERAL, |
| 699 |
TyTy::InferType::TypeHint::Default (), |
| 700 |
pattern.get_locus ()); |
| 701 |
} |
| 702 |
|
| 703 |
void |
| 704 |
ClosureParamInfer::visit (HIR::ReferencePattern &pattern) |
| 705 |
{ |
| 706 |
TyTy::BaseType *element |
| 707 |
= ClosureParamInfer::Resolve (pattern.get_referenced_pattern ()); |
| 708 |
|
| 709 |
HirId id = pattern.get_mappings ().get_hirid (); |
| 710 |
infered = new TyTy::ReferenceType (id, TyTy::TyVar (element->get_ref ()), |
| 711 |
pattern.get_mutability ()); |
| 712 |
} |
| 713 |
|
| 714 |
void |
| 715 |
ClosureParamInfer::visit (HIR::PathInExpression &pattern) |
| 716 |
{ |
| 717 |
rust_sorry_at (pattern.get_locus (), |
| 718 |
"unable to infer this kind of parameter pattern"); |
| 719 |
} |
| 720 |
|
| 721 |
void |
| 722 |
ClosureParamInfer::visit (HIR::StructPattern &pattern) |
| 723 |
{ |
| 724 |
rust_sorry_at (pattern.get_locus (), |
| 725 |
"unable to infer this kind of parameter pattern"); |
| 726 |
} |
| 727 |
|
| 728 |
void |
| 729 |
ClosureParamInfer::visit (HIR::TupleStructPattern &pattern) |
| 730 |
{ |
| 731 |
rust_sorry_at (pattern.get_locus (), |
| 732 |
"unable to infer this kind of parameter pattern"); |
| 733 |
} |
| 734 |
|
| 735 |
void |
| 736 |
ClosureParamInfer::visit (HIR::TuplePattern &pattern) |
| 737 |
{ |
| 738 |
rust_sorry_at (pattern.get_locus (), |
| 739 |
"unable to infer this kind of parameter pattern"); |
| 740 |
} |
| 741 |
|
| 742 |
void |
| 743 |
ClosureParamInfer::visit (HIR::LiteralPattern &pattern) |
| 744 |
{ |
| 745 |
rust_sorry_at (pattern.get_locus (), |
| 746 |
"unable to infer this kind of parameter pattern"); |
| 747 |
} |
| 748 |
|
| 749 |
void |
| 750 |
ClosureParamInfer::visit (HIR::RangePattern &pattern) |
| 751 |
{ |
| 752 |
rust_sorry_at (pattern.get_locus (), |
| 753 |
"unable to infer this kind of parameter pattern"); |
| 754 |
} |
| 755 |
|
| 756 |
void |
| 757 |
ClosureParamInfer::visit (HIR::QualifiedPathInExpression &pattern) |
| 758 |
{ |
| 759 |
rust_sorry_at (pattern.get_locus (), |
| 760 |
"unable to infer this kind of parameter pattern"); |
| 761 |
} |
| 762 |
|
| 763 |
void |
| 764 |
ClosureParamInfer::visit (HIR::SlicePattern &pattern) |
| 765 |
{ |
| 766 |
rust_sorry_at (pattern.get_locus (), |
| 767 |
"unable to infer this kind of parameter pattern"); |
| 768 |
} |
| 769 |
|
| 770 |
void |
| 771 |
ClosureParamInfer::visit (HIR::AltPattern &pattern) |
| 772 |
{ |
| 773 |
rust_sorry_at (pattern.get_locus (), |
| 774 |
"unable to infer this kind of parameter pattern"); |
| 775 |
} |
| 776 |
|
| 777 |
} // namespace Resolver |
| 778 |
} // namespace Rust |