| 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.h" |
| 20 |
#include "rust-type-util.h" |
| 21 |
|
| 22 |
namespace Rust { |
| 23 |
namespace Resolver { |
| 24 |
|
| 25 |
TypeCheckContext * |
| 26 |
TypeCheckContext::get () |
| 27 |
{ |
| 28 |
static TypeCheckContext *instance; |
| 29 |
if (instance == nullptr) |
| 30 |
instance = new TypeCheckContext (); |
| 31 |
|
| 32 |
return instance; |
| 33 |
} |
| 34 |
|
| 35 |
TypeCheckContext::TypeCheckContext () { lifetime_resolver_stack.emplace (); } |
| 36 |
|
| 37 |
TypeCheckContext::~TypeCheckContext () {} |
| 38 |
|
| 39 |
bool |
| 40 |
TypeCheckContext::lookup_builtin (NodeId id, TyTy::BaseType **type) |
| 41 |
{ |
| 42 |
auto ref_it = node_id_refs.find (id); |
| 43 |
if (ref_it == node_id_refs.end ()) |
| 44 |
return false; |
| 45 |
|
| 46 |
auto it = resolved.find (ref_it->second); |
| 47 |
if (it == resolved.end ()) |
| 48 |
return false; |
| 49 |
|
| 50 |
*type = it->second; |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
bool |
| 55 |
TypeCheckContext::lookup_builtin (std::string name, TyTy::BaseType **type) |
| 56 |
{ |
| 57 |
for (auto &builtin : builtins) |
| 58 |
{ |
| 59 |
if (name.compare (builtin->as_string ()) == 0) |
| 60 |
{ |
| 61 |
*type = builtin.get (); |
| 62 |
return true; |
| 63 |
} |
| 64 |
} |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
void |
| 69 |
TypeCheckContext::insert_builtin (HirId id, NodeId ref, TyTy::BaseType *type) |
| 70 |
{ |
| 71 |
node_id_refs[ref] = id; |
| 72 |
resolved[id] = type; |
| 73 |
builtins.push_back (std::unique_ptr<TyTy::BaseType> (type)); |
| 74 |
} |
| 75 |
|
| 76 |
const std::vector<std::unique_ptr<TyTy::BaseType>> & |
| 77 |
TypeCheckContext::get_builtins () const |
| 78 |
{ |
| 79 |
return builtins; |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
TypeCheckContext::insert_type (const Analysis::NodeMapping &mappings, |
| 84 |
TyTy::BaseType *type) |
| 85 |
{ |
| 86 |
rust_assert (type != nullptr); |
| 87 |
NodeId ref = mappings.get_nodeid (); |
| 88 |
HirId id = mappings.get_hirid (); |
| 89 |
node_id_refs[ref] = id; |
| 90 |
resolved[id] = type; |
| 91 |
} |
| 92 |
|
| 93 |
void |
| 94 |
TypeCheckContext::insert_implicit_type (HirId id, TyTy::BaseType *type) |
| 95 |
{ |
| 96 |
rust_assert (type != nullptr); |
| 97 |
resolved[id] = type; |
| 98 |
} |
| 99 |
|
| 100 |
bool |
| 101 |
TypeCheckContext::lookup_type (HirId id, TyTy::BaseType **type) const |
| 102 |
{ |
| 103 |
auto it = resolved.find (id); |
| 104 |
if (it == resolved.end ()) |
| 105 |
return false; |
| 106 |
|
| 107 |
*type = it->second; |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
void |
| 112 |
TypeCheckContext::clear_type (TyTy::BaseType *ty) |
| 113 |
{ |
| 114 |
auto it = resolved.find (ty->get_ref ()); |
| 115 |
if (it == resolved.end ()) |
| 116 |
return; |
| 117 |
|
| 118 |
resolved.erase (it); |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
TypeCheckContext::insert_type_by_node_id (NodeId ref, HirId id) |
| 123 |
{ |
| 124 |
rust_assert (node_id_refs.find (ref) == node_id_refs.end ()); |
| 125 |
node_id_refs[ref] = id; |
| 126 |
} |
| 127 |
|
| 128 |
bool |
| 129 |
TypeCheckContext::lookup_type_by_node_id (NodeId ref, HirId *id) |
| 130 |
{ |
| 131 |
auto it = node_id_refs.find (ref); |
| 132 |
if (it == node_id_refs.end ()) |
| 133 |
return false; |
| 134 |
|
| 135 |
*id = it->second; |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
bool |
| 140 |
TypeCheckContext::have_function_context () const |
| 141 |
{ |
| 142 |
return !return_type_stack.empty (); |
| 143 |
} |
| 144 |
|
| 145 |
TyTy::BaseType * |
| 146 |
TypeCheckContext::peek_return_type () |
| 147 |
{ |
| 148 |
rust_assert (!return_type_stack.empty ()); |
| 149 |
return return_type_stack.back ().second; |
| 150 |
} |
| 151 |
|
| 152 |
void |
| 153 |
TypeCheckContext::push_return_type (TypeCheckContextItem item, |
| 154 |
TyTy::BaseType *return_type) |
| 155 |
{ |
| 156 |
return_type_stack.push_back ({std::move (item), return_type}); |
| 157 |
} |
| 158 |
|
| 159 |
void |
| 160 |
TypeCheckContext::pop_return_type () |
| 161 |
{ |
| 162 |
rust_assert (!return_type_stack.empty ()); |
| 163 |
return_type_stack.pop_back (); |
| 164 |
} |
| 165 |
|
| 166 |
TypeCheckContextItem |
| 167 |
TypeCheckContext::peek_context () |
| 168 |
{ |
| 169 |
rust_assert (!return_type_stack.empty ()); |
| 170 |
return return_type_stack.back ().first; |
| 171 |
} |
| 172 |
|
| 173 |
StackedContexts<TypeCheckBlockContextItem> & |
| 174 |
TypeCheckContext::block_context () |
| 175 |
{ |
| 176 |
return block_stack; |
| 177 |
} |
| 178 |
|
| 179 |
void |
| 180 |
TypeCheckContext::iterate (std::function<bool (HirId, TyTy::BaseType *)> cb) |
| 181 |
{ |
| 182 |
for (auto it = resolved.begin (); it != resolved.end (); it++) |
| 183 |
{ |
| 184 |
if (!cb (it->first, it->second)) |
| 185 |
return; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
bool |
| 190 |
TypeCheckContext::have_loop_context () const |
| 191 |
{ |
| 192 |
return !loop_type_stack.empty (); |
| 193 |
} |
| 194 |
|
| 195 |
void |
| 196 |
TypeCheckContext::push_new_loop_context (HirId id, location_t locus) |
| 197 |
{ |
| 198 |
TyTy::BaseType *infer_var |
| 199 |
= new TyTy::InferType (id, TyTy::InferType::InferTypeKind::GENERAL, |
| 200 |
TyTy::InferType::TypeHint::Default (), locus); |
| 201 |
loop_type_stack.push_back (infer_var); |
| 202 |
} |
| 203 |
|
| 204 |
void |
| 205 |
TypeCheckContext::push_new_while_loop_context (HirId id) |
| 206 |
{ |
| 207 |
TyTy::BaseType *infer_var = new TyTy::ErrorType (id); |
| 208 |
loop_type_stack.push_back (infer_var); |
| 209 |
} |
| 210 |
|
| 211 |
TyTy::BaseType * |
| 212 |
TypeCheckContext::peek_loop_context () |
| 213 |
{ |
| 214 |
return loop_type_stack.back (); |
| 215 |
} |
| 216 |
|
| 217 |
TyTy::BaseType * |
| 218 |
TypeCheckContext::pop_loop_context () |
| 219 |
{ |
| 220 |
auto back = peek_loop_context (); |
| 221 |
loop_type_stack.pop_back (); |
| 222 |
return back; |
| 223 |
} |
| 224 |
|
| 225 |
void |
| 226 |
TypeCheckContext::swap_head_loop_context (TyTy::BaseType *val) |
| 227 |
{ |
| 228 |
loop_type_stack.pop_back (); |
| 229 |
loop_type_stack.push_back (val); |
| 230 |
} |
| 231 |
|
| 232 |
void |
| 233 |
TypeCheckContext::insert_trait_reference (DefId id, TraitReference &&ref) |
| 234 |
{ |
| 235 |
rust_assert (trait_context.find (id) == trait_context.end ()); |
| 236 |
trait_context.emplace (id, std::move (ref)); |
| 237 |
} |
| 238 |
|
| 239 |
bool |
| 240 |
TypeCheckContext::lookup_trait_reference (DefId id, TraitReference **ref) |
| 241 |
{ |
| 242 |
auto it = trait_context.find (id); |
| 243 |
if (it == trait_context.end ()) |
| 244 |
return false; |
| 245 |
|
| 246 |
*ref = &it->second; |
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
void |
| 251 |
TypeCheckContext::insert_associated_trait_impl ( |
| 252 |
HirId id, AssociatedImplTrait &&associated) |
| 253 |
{ |
| 254 |
rust_assert (associated_impl_traits.find (id) |
| 255 |
== associated_impl_traits.end ()); |
| 256 |
associated_impl_traits.emplace (id, std::move (associated)); |
| 257 |
} |
| 258 |
|
| 259 |
bool |
| 260 |
TypeCheckContext::lookup_associated_trait_impl ( |
| 261 |
HirId id, AssociatedImplTrait **associated) |
| 262 |
{ |
| 263 |
auto it = associated_impl_traits.find (id); |
| 264 |
if (it == associated_impl_traits.end ()) |
| 265 |
return false; |
| 266 |
|
| 267 |
*associated = &it->second; |
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
void |
| 272 |
TypeCheckContext::insert_associated_type_mapping (HirId id, HirId mapping) |
| 273 |
{ |
| 274 |
associated_type_mappings[id] = mapping; |
| 275 |
} |
| 276 |
|
| 277 |
void |
| 278 |
TypeCheckContext::clear_associated_type_mapping (HirId id) |
| 279 |
{ |
| 280 |
auto it = associated_type_mappings.find (id); |
| 281 |
if (it != associated_type_mappings.end ()) |
| 282 |
associated_type_mappings.erase (it); |
| 283 |
} |
| 284 |
|
| 285 |
// lookup any associated type mappings, the out parameter of mapping is |
| 286 |
// allowed to be nullptr which allows this interface to do a simple does exist |
| 287 |
// check |
| 288 |
bool |
| 289 |
TypeCheckContext::lookup_associated_type_mapping (HirId id, HirId *mapping) |
| 290 |
{ |
| 291 |
auto it = associated_type_mappings.find (id); |
| 292 |
if (it == associated_type_mappings.end ()) |
| 293 |
return false; |
| 294 |
|
| 295 |
if (mapping != nullptr) |
| 296 |
*mapping = it->second; |
| 297 |
|
| 298 |
return true; |
| 299 |
} |
| 300 |
|
| 301 |
void |
| 302 |
TypeCheckContext::insert_associated_impl_mapping ( |
| 303 |
HirId trait_id, const TyTy::BaseType *impl_type, HirId impl_id) |
| 304 |
{ |
| 305 |
auto it = associated_traits_to_impls.find (trait_id); |
| 306 |
if (it == associated_traits_to_impls.end ()) |
| 307 |
{ |
| 308 |
associated_traits_to_impls[trait_id] = {}; |
| 309 |
} |
| 310 |
|
| 311 |
associated_traits_to_impls[trait_id].push_back ({impl_type, impl_id}); |
| 312 |
} |
| 313 |
|
| 314 |
bool |
| 315 |
TypeCheckContext::lookup_associated_impl_mapping_for_self ( |
| 316 |
HirId trait_id, const TyTy::BaseType *self, HirId *mapping) |
| 317 |
{ |
| 318 |
auto it = associated_traits_to_impls.find (trait_id); |
| 319 |
if (it == associated_traits_to_impls.end ()) |
| 320 |
return false; |
| 321 |
|
| 322 |
for (auto &item : it->second) |
| 323 |
{ |
| 324 |
if (item.first->can_eq (self, false)) |
| 325 |
{ |
| 326 |
*mapping = item.second; |
| 327 |
return true; |
| 328 |
} |
| 329 |
} |
| 330 |
return false; |
| 331 |
} |
| 332 |
|
| 333 |
void |
| 334 |
TypeCheckContext::insert_autoderef_mappings ( |
| 335 |
HirId id, std::vector<Adjustment> &&adjustments) |
| 336 |
{ |
| 337 |
autoderef_mappings.emplace (id, std::move (adjustments)); |
| 338 |
} |
| 339 |
|
| 340 |
bool |
| 341 |
TypeCheckContext::lookup_autoderef_mappings ( |
| 342 |
HirId id, std::vector<Adjustment> **adjustments) |
| 343 |
{ |
| 344 |
auto it = autoderef_mappings.find (id); |
| 345 |
if (it == autoderef_mappings.end ()) |
| 346 |
return false; |
| 347 |
|
| 348 |
*adjustments = &it->second; |
| 349 |
return true; |
| 350 |
} |
| 351 |
|
| 352 |
void |
| 353 |
TypeCheckContext::insert_cast_autoderef_mappings ( |
| 354 |
HirId id, std::vector<Adjustment> &&adjustments) |
| 355 |
{ |
| 356 |
cast_autoderef_mappings.emplace (id, std::move (adjustments)); |
| 357 |
} |
| 358 |
|
| 359 |
bool |
| 360 |
TypeCheckContext::lookup_cast_autoderef_mappings ( |
| 361 |
HirId id, std::vector<Adjustment> **adjustments) |
| 362 |
{ |
| 363 |
auto it = cast_autoderef_mappings.find (id); |
| 364 |
if (it == cast_autoderef_mappings.end ()) |
| 365 |
return false; |
| 366 |
|
| 367 |
*adjustments = &it->second; |
| 368 |
return true; |
| 369 |
} |
| 370 |
|
| 371 |
void |
| 372 |
TypeCheckContext::insert_variant_definition (HirId id, HirId variant) |
| 373 |
{ |
| 374 |
auto it = variants.find (id); |
| 375 |
rust_assert (it == variants.end ()); |
| 376 |
|
| 377 |
variants[id] = variant; |
| 378 |
} |
| 379 |
|
| 380 |
bool |
| 381 |
TypeCheckContext::lookup_variant_definition (HirId id, HirId *variant) |
| 382 |
{ |
| 383 |
auto it = variants.find (id); |
| 384 |
if (it == variants.end ()) |
| 385 |
return false; |
| 386 |
|
| 387 |
*variant = it->second; |
| 388 |
return true; |
| 389 |
} |
| 390 |
|
| 391 |
void |
| 392 |
TypeCheckContext::insert_operator_overload (HirId id, TyTy::FnType *call_site) |
| 393 |
{ |
| 394 |
auto it = operator_overloads.find (id); |
| 395 |
rust_assert (it == operator_overloads.end ()); |
| 396 |
|
| 397 |
operator_overloads[id] = call_site; |
| 398 |
} |
| 399 |
|
| 400 |
bool |
| 401 |
TypeCheckContext::lookup_operator_overload (HirId id, TyTy::FnType **call) |
| 402 |
{ |
| 403 |
auto it = operator_overloads.find (id); |
| 404 |
if (it == operator_overloads.end ()) |
| 405 |
return false; |
| 406 |
|
| 407 |
*call = it->second; |
| 408 |
return true; |
| 409 |
} |
| 410 |
|
| 411 |
void |
| 412 |
TypeCheckContext::insert_unconstrained_check_marker (HirId id, bool status) |
| 413 |
{ |
| 414 |
unconstrained[id] = status; |
| 415 |
} |
| 416 |
|
| 417 |
bool |
| 418 |
TypeCheckContext::have_checked_for_unconstrained (HirId id, bool *result) |
| 419 |
{ |
| 420 |
auto it = unconstrained.find (id); |
| 421 |
bool found = it != unconstrained.end (); |
| 422 |
if (!found) |
| 423 |
return false; |
| 424 |
|
| 425 |
*result = it->second; |
| 426 |
return true; |
| 427 |
} |
| 428 |
|
| 429 |
void |
| 430 |
TypeCheckContext::insert_resolved_predicate (HirId id, |
| 431 |
TyTy::TypeBoundPredicate predicate) |
| 432 |
{ |
| 433 |
// auto it = predicates.find (id); |
| 434 |
// rust_assert (it == predicates.end ()); |
| 435 |
|
| 436 |
predicates.insert ({id, predicate}); |
| 437 |
} |
| 438 |
|
| 439 |
bool |
| 440 |
TypeCheckContext::lookup_predicate (HirId id, TyTy::TypeBoundPredicate *result) |
| 441 |
{ |
| 442 |
auto it = predicates.find (id); |
| 443 |
bool found = it != predicates.end (); |
| 444 |
if (!found) |
| 445 |
return false; |
| 446 |
|
| 447 |
*result = it->second; |
| 448 |
return true; |
| 449 |
} |
| 450 |
|
| 451 |
void |
| 452 |
TypeCheckContext::insert_query (HirId id) |
| 453 |
{ |
| 454 |
querys_in_progress.insert (id); |
| 455 |
} |
| 456 |
|
| 457 |
void |
| 458 |
TypeCheckContext::query_completed (HirId id) |
| 459 |
{ |
| 460 |
querys_in_progress.erase (id); |
| 461 |
} |
| 462 |
|
| 463 |
bool |
| 464 |
TypeCheckContext::query_in_progress (HirId id) const |
| 465 |
{ |
| 466 |
return querys_in_progress.find (id) != querys_in_progress.end (); |
| 467 |
} |
| 468 |
|
| 469 |
void |
| 470 |
TypeCheckContext::insert_trait_query (DefId id) |
| 471 |
{ |
| 472 |
trait_queries_in_progress.insert (id); |
| 473 |
} |
| 474 |
|
| 475 |
void |
| 476 |
TypeCheckContext::trait_query_completed (DefId id) |
| 477 |
{ |
| 478 |
trait_queries_in_progress.erase (id); |
| 479 |
} |
| 480 |
|
| 481 |
bool |
| 482 |
TypeCheckContext::trait_query_in_progress (DefId id) const |
| 483 |
{ |
| 484 |
return trait_queries_in_progress.find (id) |
| 485 |
!= trait_queries_in_progress.end (); |
| 486 |
} |
| 487 |
|
| 488 |
Lifetime |
| 489 |
TypeCheckContext::intern_lifetime (const HIR::Lifetime &lifetime) |
| 490 |
{ |
| 491 |
if (lifetime.get_lifetime_type () == AST::Lifetime::NAMED) |
| 492 |
{ |
| 493 |
auto maybe_interned = lookup_lifetime (lifetime); |
| 494 |
if (maybe_interned) |
| 495 |
return *maybe_interned; |
| 496 |
|
| 497 |
auto interned = next_lifetime_index.next (); |
| 498 |
lifetime_name_interner[lifetime.get_name ()] = interned; |
| 499 |
return interned; |
| 500 |
} |
| 501 |
if (lifetime.get_lifetime_type () == AST::Lifetime::WILDCARD) |
| 502 |
{ |
| 503 |
return next_lifetime_index.next (); |
| 504 |
} |
| 505 |
if (lifetime.get_lifetime_type () == AST::Lifetime::STATIC) |
| 506 |
{ |
| 507 |
return Lifetime::static_lifetime (); |
| 508 |
} |
| 509 |
rust_unreachable (); |
| 510 |
} |
| 511 |
|
| 512 |
tl::optional<Lifetime> |
| 513 |
TypeCheckContext::lookup_lifetime (const HIR::Lifetime &lifetime) const |
| 514 |
{ |
| 515 |
if (lifetime.get_lifetime_type () == AST::Lifetime::NAMED) |
| 516 |
{ |
| 517 |
if (lifetime.get_name () == "static") |
| 518 |
{ |
| 519 |
rich_location r (line_table, lifetime.get_locus ()); |
| 520 |
r.add_fixit_insert_after (lifetime.get_locus (), |
| 521 |
"static is a reserved lifetime name"); |
| 522 |
rust_error_at (r, ErrorCode::E0262, |
| 523 |
"invalid lifetime parameter name: %qs", |
| 524 |
lifetime.get_name ().c_str ()); |
| 525 |
return tl::nullopt; |
| 526 |
} |
| 527 |
const auto name = lifetime.get_name (); |
| 528 |
auto it = lifetime_name_interner.find (name); |
| 529 |
if (it == lifetime_name_interner.end ()) |
| 530 |
return tl::nullopt; |
| 531 |
return it->second; |
| 532 |
} |
| 533 |
if (lifetime.get_lifetime_type () == AST::Lifetime::WILDCARD) |
| 534 |
{ |
| 535 |
return Lifetime::anonymous_lifetime (); |
| 536 |
} |
| 537 |
if (lifetime.get_lifetime_type () == AST::Lifetime::STATIC) |
| 538 |
{ |
| 539 |
return Lifetime::static_lifetime (); |
| 540 |
} |
| 541 |
rust_unreachable (); |
| 542 |
} |
| 543 |
|
| 544 |
WARN_UNUSED_RESULT tl::optional<TyTy::Region> |
| 545 |
TypeCheckContext::lookup_and_resolve_lifetime ( |
| 546 |
const HIR::Lifetime &lifetime) const |
| 547 |
{ |
| 548 |
auto maybe_interned = lookup_lifetime (lifetime); |
| 549 |
if (!maybe_interned) |
| 550 |
return tl::nullopt; |
| 551 |
|
| 552 |
return get_lifetime_resolver ().resolve (maybe_interned.value ()); |
| 553 |
} |
| 554 |
void |
| 555 |
TypeCheckContext::intern_and_insert_lifetime (const HIR::Lifetime &lifetime) |
| 556 |
{ |
| 557 |
get_lifetime_resolver ().insert_mapping (intern_lifetime (lifetime)); |
| 558 |
} |
| 559 |
|
| 560 |
WARN_UNUSED_RESULT std::vector<TyTy::Region> |
| 561 |
TypeCheckContext::regions_from_generic_args (const HIR::GenericArgs &args) const |
| 562 |
{ |
| 563 |
std::vector<TyTy::Region> regions; |
| 564 |
for (const auto &lifetime : args.get_lifetime_args ()) |
| 565 |
{ |
| 566 |
auto resolved = lookup_and_resolve_lifetime (lifetime); |
| 567 |
if (!resolved) |
| 568 |
{ |
| 569 |
rust_error_at (lifetime.get_locus (), "unresolved lifetime"); |
| 570 |
return {}; |
| 571 |
} |
| 572 |
regions.push_back (*resolved); |
| 573 |
} |
| 574 |
return regions; |
| 575 |
} |
| 576 |
|
| 577 |
void |
| 578 |
TypeCheckContext::compute_inference_variables (bool error) |
| 579 |
{ |
| 580 |
auto &mappings = Analysis::Mappings::get (); |
| 581 |
|
| 582 |
// default inference variables if possible |
| 583 |
iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool { |
| 584 |
// nothing to do |
| 585 |
if (ty->get_kind () != TyTy::TypeKind::INFER) |
| 586 |
return true; |
| 587 |
|
| 588 |
TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty); |
| 589 |
TyTy::BaseType *default_type; |
| 590 |
|
| 591 |
rust_debug_loc (mappings.lookup_location (id), |
| 592 |
"trying to default infer-var: %s", |
| 593 |
infer_var->as_string ().c_str ()); |
| 594 |
bool ok = infer_var->default_type (&default_type); |
| 595 |
if (!ok) |
| 596 |
{ |
| 597 |
if (error) |
| 598 |
rust_error_at (mappings.lookup_location (id), ErrorCode::E0282, |
| 599 |
"type annotations needed"); |
| 600 |
return true; |
| 601 |
} |
| 602 |
|
| 603 |
auto result |
| 604 |
= unify_site (id, TyTy::TyWithLocation (ty), |
| 605 |
TyTy::TyWithLocation (default_type), UNDEF_LOCATION); |
| 606 |
rust_assert (result); |
| 607 |
rust_assert (result->get_kind () != TyTy::TypeKind::ERROR); |
| 608 |
result->set_ref (id); |
| 609 |
insert_type (Analysis::NodeMapping (mappings.get_current_crate (), 0, id, |
| 610 |
UNKNOWN_LOCAL_DEFID), |
| 611 |
result); |
| 612 |
|
| 613 |
return true; |
| 614 |
}); |
| 615 |
} |
| 616 |
|
| 617 |
TyTy::VarianceAnalysis::CrateCtx & |
| 618 |
TypeCheckContext::get_variance_analysis_ctx () |
| 619 |
{ |
| 620 |
return variance_analysis_ctx; |
| 621 |
} |
| 622 |
|
| 623 |
// TypeCheckContextItem |
| 624 |
|
| 625 |
TypeCheckContextItem::Item::Item (HIR::Function *item) : item (item) {} |
| 626 |
|
| 627 |
TypeCheckContextItem::Item::Item (HIR::ImplBlock *impl_block, |
| 628 |
HIR::Function *item) |
| 629 |
: impl_item ({impl_block, item}) |
| 630 |
{} |
| 631 |
|
| 632 |
TypeCheckContextItem::Item::Item (HIR::TraitItemFunc *trait_item) |
| 633 |
: trait_item (trait_item) |
| 634 |
{} |
| 635 |
|
| 636 |
TypeCheckContextItem::TypeCheckContextItem (HIR::Function *item) |
| 637 |
: type (ItemType::ITEM), item (item) |
| 638 |
{} |
| 639 |
|
| 640 |
TypeCheckContextItem::TypeCheckContextItem (HIR::ImplBlock &impl_block, |
| 641 |
HIR::Function *item) |
| 642 |
: type (ItemType::IMPL_ITEM), item (&impl_block, item) |
| 643 |
{} |
| 644 |
|
| 645 |
TypeCheckContextItem::TypeCheckContextItem (HIR::TraitItemFunc *trait_item) |
| 646 |
: type (ItemType::TRAIT_ITEM), item (trait_item) |
| 647 |
{} |
| 648 |
|
| 649 |
TypeCheckContextItem::TypeCheckContextItem (const TypeCheckContextItem &other) |
| 650 |
: type (other.type), item (other.item) |
| 651 |
{ |
| 652 |
switch (other.type) |
| 653 |
{ |
| 654 |
case ITEM: |
| 655 |
item.item = other.item.item; |
| 656 |
break; |
| 657 |
|
| 658 |
case IMPL_ITEM: |
| 659 |
item.impl_item = other.item.impl_item; |
| 660 |
break; |
| 661 |
|
| 662 |
case TRAIT_ITEM: |
| 663 |
item.trait_item = other.item.trait_item; |
| 664 |
break; |
| 665 |
|
| 666 |
case ERROR: |
| 667 |
item.item = nullptr; |
| 668 |
break; |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
TypeCheckContextItem::TypeCheckContextItem () |
| 673 |
: type (ItemType::ERROR), item (static_cast<HIR::Function *> (nullptr)) |
| 674 |
{} |
| 675 |
|
| 676 |
TypeCheckContextItem & |
| 677 |
TypeCheckContextItem::operator= (const TypeCheckContextItem &other) |
| 678 |
{ |
| 679 |
type = other.type; |
| 680 |
switch (other.type) |
| 681 |
{ |
| 682 |
case ITEM: |
| 683 |
item.item = other.item.item; |
| 684 |
break; |
| 685 |
|
| 686 |
case IMPL_ITEM: |
| 687 |
item.impl_item = other.item.impl_item; |
| 688 |
break; |
| 689 |
|
| 690 |
case TRAIT_ITEM: |
| 691 |
item.trait_item = other.item.trait_item; |
| 692 |
break; |
| 693 |
|
| 694 |
case ERROR: |
| 695 |
item.item = nullptr; |
| 696 |
break; |
| 697 |
} |
| 698 |
|
| 699 |
return *this; |
| 700 |
} |
| 701 |
|
| 702 |
TypeCheckContextItem |
| 703 |
TypeCheckContextItem::get_error () |
| 704 |
{ |
| 705 |
return TypeCheckContextItem (); |
| 706 |
} |
| 707 |
|
| 708 |
bool |
| 709 |
TypeCheckContextItem::is_error () const |
| 710 |
{ |
| 711 |
return type == ERROR; |
| 712 |
} |
| 713 |
|
| 714 |
HIR::Function * |
| 715 |
TypeCheckContextItem::get_item () |
| 716 |
{ |
| 717 |
rust_assert (get_type () == ItemType::ITEM); |
| 718 |
return item.item; |
| 719 |
} |
| 720 |
|
| 721 |
std::pair<HIR::ImplBlock *, HIR::Function *> & |
| 722 |
TypeCheckContextItem::get_impl_item () |
| 723 |
{ |
| 724 |
rust_assert (get_type () == ItemType::IMPL_ITEM); |
| 725 |
return item.impl_item; |
| 726 |
} |
| 727 |
|
| 728 |
HIR::TraitItemFunc * |
| 729 |
TypeCheckContextItem::get_trait_item () |
| 730 |
{ |
| 731 |
rust_assert (get_type () == ItemType::TRAIT_ITEM); |
| 732 |
return item.trait_item; |
| 733 |
} |
| 734 |
|
| 735 |
TypeCheckContextItem::ItemType |
| 736 |
TypeCheckContextItem::get_type () const |
| 737 |
{ |
| 738 |
return type; |
| 739 |
} |
| 740 |
|
| 741 |
TyTy::FnType * |
| 742 |
TypeCheckContextItem::get_context_type () |
| 743 |
{ |
| 744 |
auto &context = *TypeCheckContext::get (); |
| 745 |
|
| 746 |
HirId reference = UNKNOWN_HIRID; |
| 747 |
switch (get_type ()) |
| 748 |
{ |
| 749 |
case ITEM: |
| 750 |
reference = get_item ()->get_mappings ().get_hirid (); |
| 751 |
break; |
| 752 |
|
| 753 |
case IMPL_ITEM: |
| 754 |
reference = get_impl_item ().second->get_mappings ().get_hirid (); |
| 755 |
break; |
| 756 |
|
| 757 |
case TRAIT_ITEM: |
| 758 |
reference = get_trait_item ()->get_mappings ().get_hirid (); |
| 759 |
break; |
| 760 |
|
| 761 |
case ERROR: |
| 762 |
rust_unreachable (); |
| 763 |
return nullptr; |
| 764 |
} |
| 765 |
|
| 766 |
rust_assert (reference != UNKNOWN_HIRID); |
| 767 |
|
| 768 |
TyTy::BaseType *lookup = nullptr; |
| 769 |
bool ok = context.lookup_type (reference, &lookup); |
| 770 |
rust_assert (ok); |
| 771 |
rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF); |
| 772 |
return static_cast<TyTy::FnType *> (lookup); |
| 773 |
} |
| 774 |
|
| 775 |
DefId |
| 776 |
TypeCheckContextItem::get_defid () const |
| 777 |
{ |
| 778 |
switch (get_type ()) |
| 779 |
{ |
| 780 |
case ITEM: |
| 781 |
return item.item->get_mappings ().get_defid (); |
| 782 |
|
| 783 |
case IMPL_ITEM: |
| 784 |
return item.impl_item.second->get_mappings ().get_defid (); |
| 785 |
|
| 786 |
case TRAIT_ITEM: |
| 787 |
return item.trait_item->get_mappings ().get_defid (); |
| 788 |
|
| 789 |
case ERROR: |
| 790 |
return UNKNOWN_DEFID; |
| 791 |
} |
| 792 |
|
| 793 |
return UNKNOWN_DEFID; |
| 794 |
} |
| 795 |
|
| 796 |
// TypeCheckBlockContextItem |
| 797 |
|
| 798 |
TypeCheckBlockContextItem::Item::Item (HIR::ImplBlock *b) : block (b) {} |
| 799 |
|
| 800 |
TypeCheckBlockContextItem::Item::Item (HIR::Trait *t) : trait (t) {} |
| 801 |
|
| 802 |
TypeCheckBlockContextItem::TypeCheckBlockContextItem (HIR::ImplBlock *block) |
| 803 |
: type (TypeCheckBlockContextItem::ItemType::IMPL_BLOCK), item (block) |
| 804 |
{} |
| 805 |
|
| 806 |
TypeCheckBlockContextItem::TypeCheckBlockContextItem (HIR::Trait *trait) |
| 807 |
: type (TypeCheckBlockContextItem::ItemType::TRAIT), item (trait) |
| 808 |
{} |
| 809 |
|
| 810 |
bool |
| 811 |
TypeCheckBlockContextItem::is_impl_block () const |
| 812 |
{ |
| 813 |
return type == IMPL_BLOCK; |
| 814 |
} |
| 815 |
|
| 816 |
bool |
| 817 |
TypeCheckBlockContextItem::is_trait_block () const |
| 818 |
{ |
| 819 |
return type == TRAIT; |
| 820 |
} |
| 821 |
|
| 822 |
HIR::ImplBlock & |
| 823 |
TypeCheckBlockContextItem::get_impl_block () |
| 824 |
{ |
| 825 |
return *(item.block); |
| 826 |
} |
| 827 |
|
| 828 |
HIR::Trait & |
| 829 |
TypeCheckBlockContextItem::get_trait () |
| 830 |
{ |
| 831 |
return *(item.trait); |
| 832 |
} |
| 833 |
|
| 834 |
} // namespace Resolver |
| 835 |
} // namespace Rust |