| // Copyright (C) 2020-2026 Free Software Foundation, Inc. | |
| // This file is part of GCC. | |
| // GCC is free software; you can redistribute it and/or modify it under | |
| // the terms of the GNU General Public License as published by the Free | |
| // Software Foundation; either version 3, or (at your option) any later | |
| // version. | |
| // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
| // WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| // for more details. | |
| // You should have received a copy of the GNU General Public License | |
| // along with GCC; see the file COPYING3. If not see | |
| // <http://www.gnu.org/licenses/>. | |
| #include "rust-hir-type-check.h" | |
| #include "rust-type-util.h" | |
| #include "rust-hir-type-check-expr.h" | |
| namespace Rust { | |
| namespace Resolver { | |
| TypeCheckContext * | |
| TypeCheckContext::get () | |
| { | |
| static TypeCheckContext *instance; | |
| if (instance == nullptr) | |
| instance = new TypeCheckContext (); | |
| return instance; | |
| } | |
| TypeCheckContext::TypeCheckContext () { lifetime_resolver_stack.emplace (); } | |
| TypeCheckContext::~TypeCheckContext () {} | |
| bool | |
| TypeCheckContext::lookup_builtin (NodeId id, TyTy::BaseType **type) | |
| { | |
| auto ref_it = node_id_refs.find (id); | |
| if (ref_it == node_id_refs.end ()) | |
| return false; | |
| auto it = resolved.find (ref_it->second); | |
| if (it == resolved.end ()) | |
| return false; | |
| *type = it->second; | |
| return true; | |
| } | |
| bool | |
| TypeCheckContext::lookup_builtin (std::string name, TyTy::BaseType **type) | |
| { | |
| for (auto &builtin : builtins) | |
| { | |
| if (name.compare (builtin->as_string ()) == 0) | |
| { | |
| *type = builtin.get (); | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| void | |
| TypeCheckContext::insert_builtin (HirId id, NodeId ref, TyTy::BaseType *type) | |
| { | |
| node_id_refs[ref] = id; | |
| resolved[id] = type; | |
| builtins.push_back (std::unique_ptr<TyTy::BaseType> (type)); | |
| } | |
| const std::vector<std::unique_ptr<TyTy::BaseType>> & | |
| TypeCheckContext::get_builtins () const | |
| { | |
| return builtins; | |
| } | |
| void | |
| TypeCheckContext::insert_type (const Analysis::NodeMapping &mappings, | |
| TyTy::BaseType *type) | |
| { | |
| rust_assert (type != nullptr); | |
| NodeId ref = mappings.get_nodeid (); | |
| HirId id = mappings.get_hirid (); | |
| node_id_refs[ref] = id; | |
| resolved[id] = type; | |
| } | |
| void | |
| TypeCheckContext::insert_implicit_type (HirId id, TyTy::BaseType *type) | |
| { | |
| rust_assert (type != nullptr); | |
| resolved[id] = type; | |
| } | |
| static std::pair<DefId, std::vector<HirId>> | |
| make_adt_substitution_key (DefId id, | |
| const TyTy::SubstitutionArgumentMappings &mappings) | |
| { | |
| std::vector<HirId> arguments; | |
| arguments.reserve (mappings.size ()); | |
| for (const auto &mapping : mappings.get_mappings ()) | |
| arguments.push_back (mapping.get_tyty ()->get_ty_ref ()); | |
| return {id, std::move (arguments)}; | |
| } | |
| bool | |
| TypeCheckContext::lookup_adt_substitution ( | |
| DefId id, const TyTy::SubstitutionArgumentMappings &mappings, | |
| TyTy::ADTType **type) const | |
| { | |
| auto it = adt_substitutions.find (make_adt_substitution_key (id, mappings)); | |
| if (it == adt_substitutions.end ()) | |
| return false; | |
| *type = it->second; | |
| return true; | |
| } | |
| void | |
| TypeCheckContext::insert_adt_substitution ( | |
| DefId id, const TyTy::SubstitutionArgumentMappings &mappings, | |
| TyTy::ADTType *type) | |
| { | |
| rust_assert (type != nullptr); | |
| auto inserted | |
| = adt_substitutions.emplace (make_adt_substitution_key (id, mappings), | |
| type); | |
| rust_assert (inserted.second); | |
| } | |
| bool | |
| TypeCheckContext::lookup_type (HirId id, TyTy::BaseType **type) const | |
| { | |
| auto it = resolved.find (id); | |
| if (it == resolved.end ()) | |
| return false; | |
| *type = it->second; | |
| return true; | |
| } | |
| void | |
| TypeCheckContext::mark_function_body_pending (DefId id) | |
| { | |
| function_bodies_pending.insert (id); | |
| } | |
| void | |
| TypeCheckContext::clear_function_body_pending (DefId id) | |
| { | |
| function_bodies_pending.erase (id); | |
| } | |
| bool | |
| TypeCheckContext::function_body_pending (DefId id) const | |
| { | |
| return function_bodies_pending.find (id) != function_bodies_pending.end (); | |
| } | |
| void | |
| TypeCheckContext::clear_type (TyTy::BaseType *ty) | |
| { | |
| auto it = resolved.find (ty->get_ref ()); | |
| if (it == resolved.end ()) | |
| return; | |
| resolved.erase (it); | |
| } | |
| void | |
| TypeCheckContext::insert_type_by_node_id (NodeId ref, HirId id) | |
| { | |
| rust_assert (node_id_refs.find (ref) == node_id_refs.end ()); | |
| node_id_refs[ref] = id; | |
| } | |
| bool | |
| TypeCheckContext::lookup_type_by_node_id (NodeId ref, HirId *id) | |
| { | |
| auto it = node_id_refs.find (ref); | |
| if (it == node_id_refs.end ()) | |
| return false; | |
| *id = it->second; | |
| return true; | |
| } | |
| bool | |
| TypeCheckContext::have_function_context () const | |
| { | |
| return !return_type_stack.empty (); | |
| } | |
| TyTy::BaseType * | |
| TypeCheckContext::peek_return_type () | |
| { | |
| rust_assert (!return_type_stack.empty ()); | |
| return return_type_stack.back ().second; | |
| } | |
| void | |
| TypeCheckContext::push_return_type (TypeCheckContextItem item, | |
| TyTy::BaseType *return_type) | |
| { | |
| return_type_stack.emplace_back (std::move (item), return_type); | |
| // a query can check another function body while an expression in the | |
| // caller has an expected type | |
| push_expected_type (nullptr); | |
| } | |
| void | |
| TypeCheckContext::pop_return_type () | |
| { | |
| rust_assert (!return_type_stack.empty ()); | |
| pop_expected_type (); | |
| return_type_stack.pop_back (); | |
| } | |
| TypeCheckContextItem | |
| TypeCheckContext::peek_context () | |
| { | |
| rust_assert (!return_type_stack.empty ()); | |
| return return_type_stack.back ().first; | |
| } | |
| void | |
| TypeCheckContext::push_expected_type (TyTy::BaseType *expected) | |
| { | |
| expected_type_stack.push_back (expected); | |
| } | |
| void | |
| TypeCheckContext::pop_expected_type () | |
| { | |
| rust_assert (!expected_type_stack.empty ()); | |
| expected_type_stack.pop_back (); | |
| } | |
| TyTy::BaseType * | |
| TypeCheckContext::peek_expected_type () const | |
| { | |
| if (expected_type_stack.empty ()) | |
| return nullptr; | |
| return expected_type_stack.back (); | |
| } | |
| StackedContexts<TypeCheckBlockContextItem> & | |
| TypeCheckContext::block_context () | |
| { | |
| return block_stack; | |
| } | |
| void | |
| TypeCheckContext::iterate (std::function<bool (HirId, TyTy::BaseType *)> cb) | |
| { | |
| for (auto it = resolved.begin (); it != resolved.end (); it++) | |
| { | |
| if (!cb (it->first, it->second)) | |
| return; | |
| } | |
| } | |
| bool | |
| TypeCheckContext::have_loop_context () const | |
| { | |
| return !loop_type_stack.empty (); | |
| } | |
| void | |
| TypeCheckContext::push_new_loop_context (HirId id, location_t locus) | |
| { | |
| TyTy::BaseType *infer_var | |
| = new TyTy::InferType (id, TyTy::InferType::InferTypeKind::GENERAL, | |
| TyTy::InferType::TypeHint::Default (), locus); | |
| loop_type_stack.push_back (infer_var); | |
| } | |
| void | |
| TypeCheckContext::push_new_while_loop_context (HirId id) | |
| { | |
| TyTy::BaseType *infer_var = new TyTy::ErrorType (id); | |
| loop_type_stack.push_back (infer_var); | |
| } | |
| TyTy::BaseType * | |
| TypeCheckContext::peek_loop_context () | |
| { | |
| return loop_type_stack.back (); | |
| } | |
| TyTy::BaseType * | |
| TypeCheckContext::pop_loop_context () | |
| { | |
| auto back = peek_loop_context (); | |
| loop_type_stack.pop_back (); | |
| return back; | |
| } | |
| void | |
| TypeCheckContext::swap_head_loop_context (TyTy::BaseType *val) | |
| { | |
| loop_type_stack.pop_back (); | |
| loop_type_stack.push_back (val); | |
| } | |
| bool | |
| TypeCheckContext::find_matching_impl_trait_frame ( | |
| const TraitReference &tref, TyTy::BaseType &self, | |
| struct ImplTraitContextFrame *find) const | |
| { | |
| if (!have_impl_trait_context ()) | |
| return false; | |
| for (auto it = impl_trait_frame_stack.rbegin (); | |
| it != impl_trait_frame_stack.rend (); ++it) | |
| { | |
| const auto &i = *it; | |
| if (!i.trait->is_equal (tref)) | |
| continue; | |
| TyTy::BaseType *resolved_self = &self; | |
| bool unresolved_trait_self = false; | |
| if (auto param = self.try_as<TyTy::ParamType> ()) | |
| { | |
| if (param->can_resolve ()) | |
| resolved_self = param->resolve (); | |
| else | |
| unresolved_trait_self = param->is_implicit_self_trait (); | |
| } | |
| // Select an existing impl context without inferring a new Self | |
| // binding | |
| bool compatible_self = unresolved_trait_self; | |
| if (!compatible_self) | |
| { | |
| auto res | |
| = unify_site_and (UNKNOWN_HIRID, TyTy::TyWithLocation (i.self), | |
| TyTy::TyWithLocation (resolved_self), | |
| UNDEF_LOCATION, false /* emit_errors */, | |
| false /* commit */, false /* infer */, | |
| true /* cleanup */, false /* check_bounds */); | |
| compatible_self = res->get_kind () != TyTy::TypeKind::ERROR; | |
| } | |
| if (compatible_self) | |
| { | |
| *find = i; | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| bool | |
| TypeCheckContext::have_impl_trait_context () const | |
| { | |
| return !impl_trait_frame_stack.empty (); | |
| } | |
| void | |
| TypeCheckContext::push_impl_trait_context (struct ImplTraitContextFrame frame) | |
| { | |
| impl_trait_frame_stack.push_back (frame); | |
| } | |
| struct ImplTraitContextFrame | |
| TypeCheckContext::pop_impl_trait_context () | |
| { | |
| auto back = peek_impl_trait_context (); | |
| impl_trait_frame_stack.pop_back (); | |
| return back; | |
| } | |
| struct ImplTraitContextFrame | |
| TypeCheckContext::peek_impl_trait_context () | |
| { | |
| return impl_trait_frame_stack.back (); | |
| } | |
| void | |
| TypeCheckContext::insert_trait_reference (DefId id, TraitReference &&ref) | |
| { | |
| rust_assert (trait_context.find (id) == trait_context.end ()); | |
| trait_context.emplace (id, std::move (ref)); | |
| } | |
| bool | |
| TypeCheckContext::lookup_trait_reference (DefId id, TraitReference **ref) | |
| { | |
| auto it = trait_context.find (id); | |
| if (it == trait_context.end ()) | |
| return false; | |
| *ref = &it->second; | |
| return true; | |
| } | |
| bool | |
| TypeCheckContext::insert_associated_trait_impl ( | |
| HirId id, AssociatedImplTrait &&associated) | |
| { | |
| auto it = associated_impl_traits.find (id); | |
| if (it != associated_impl_traits.end ()) | |
| { | |
| return false; | |
| } | |
| associated_impl_traits.emplace (id, std::move (associated)); | |
| return true; | |
| } | |
| bool | |
| TypeCheckContext::lookup_associated_trait_impl ( | |
| HirId id, AssociatedImplTrait **associated) | |
| { | |
| auto it = associated_impl_traits.find (id); | |
| if (it == associated_impl_traits.end ()) | |
| return false; | |
| *associated = &it->second; | |
| return true; | |
| } | |
| void | |
| TypeCheckContext::insert_associated_type_mapping (HirId id, HirId mapping) | |
| { | |
| associated_type_mappings[id] = mapping; | |
| } | |
| void | |
| TypeCheckContext::clear_associated_type_mapping (HirId id) | |
| { | |
| auto it = associated_type_mappings.find (id); | |
| if (it != associated_type_mappings.end ()) | |
| associated_type_mappings.erase (it); | |
| } | |
| // lookup any associated type mappings, the out parameter of mapping is | |
| // allowed to be nullptr which allows this interface to do a simple does exist | |
| // check | |
| bool | |
| TypeCheckContext::lookup_associated_type_mapping (HirId id, HirId *mapping) | |
| { | |
| auto it = associated_type_mappings.find (id); | |
| if (it == associated_type_mappings.end ()) | |
| return false; | |
| if (mapping != nullptr) | |
| *mapping = it->second; | |
| return true; | |
| } | |
| void | |
| TypeCheckContext::insert_associated_impl_mapping (HirId trait_id, | |
| TyTy::BaseType *impl_type, | |
| HirId impl_id) | |
| { | |
| auto it = associated_traits_to_impls.find (trait_id); | |
| if (it == associated_traits_to_impls.end ()) | |
| { | |
| associated_traits_to_impls[trait_id] = {}; | |
| } | |
| associated_traits_to_impls[trait_id].emplace_back (impl_type, impl_id); | |
| } | |
| bool | |
| TypeCheckContext::lookup_associated_impl_mapping_for_self (HirId trait_id, | |
| TyTy::BaseType *self, | |
| HirId *mapping) | |
| { | |
| auto it = associated_traits_to_impls.find (trait_id); | |
| if (it == associated_traits_to_impls.end ()) | |
| return false; | |
| for (auto &item : it->second) | |
| { | |
| if (types_compatable (TyTy::TyWithLocation (item.first), | |
| TyTy::TyWithLocation (self), UNKNOWN_LOCATION, | |
| false)) | |
| { | |
| *mapping = item.second; | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| void | |
| TypeCheckContext::insert_autoderef_mappings ( | |
| HirId id, std::vector<Adjustment> &&adjustments) | |
| { | |
| autoderef_mappings.emplace (id, std::move (adjustments)); | |
| } | |
| bool | |
| TypeCheckContext::lookup_autoderef_mappings ( | |
| HirId id, std::vector<Adjustment> **adjustments) | |
| { | |
| auto it = autoderef_mappings.find (id); | |
| if (it == autoderef_mappings.end ()) | |
| return false; | |
| *adjustments = &it->second; | |
| return true; | |
| } | |
| void | |
| TypeCheckContext::insert_cast_autoderef_mappings ( | |
| HirId id, std::vector<Adjustment> &&adjustments) | |
| { | |
| cast_autoderef_mappings.emplace (id, std::move (adjustments)); | |
| } | |
| bool | |
| TypeCheckContext::lookup_cast_autoderef_mappings ( | |
| HirId id, std::vector<Adjustment> **adjustments) | |
| { | |
| auto it = cast_autoderef_mappings.find (id); | |
| if (it == cast_autoderef_mappings.end ()) | |
| return false; | |
| *adjustments = &it->second; | |
| return true; | |
| } | |
| void | |
| TypeCheckContext::insert_variant_definition (HirId id, HirId variant) | |
| { | |
| auto it = variants.find (id); | |
| rust_assert (it == variants.end ()); | |
| variants[id] = variant; | |
| } | |
| bool | |
| TypeCheckContext::lookup_variant_definition (HirId id, HirId *variant) | |
| { | |
| auto it = variants.find (id); | |
| if (it == variants.end ()) | |
| return false; | |
| *variant = it->second; | |
| return true; | |
| } | |
| void | |
| TypeCheckContext::insert_operator_overload (HirId id, TyTy::FnType *call_site) | |
| { | |
| auto it = operator_overloads.find (id); | |
| rust_assert (it == operator_overloads.end ()); | |
| operator_overloads[id] = call_site; | |
| } | |
| bool | |
| TypeCheckContext::lookup_operator_overload (HirId id, TyTy::FnType **call) | |
| { | |
| auto it = operator_overloads.find (id); | |
| if (it == operator_overloads.end ()) | |
| return false; | |
| *call = it->second; | |
| return true; | |
| } | |
| void | |
| TypeCheckContext::insert_deferred_operator_overload ( | |
| DeferredOpOverload deferred) | |
| { | |
| HirId expr_id = deferred.expr_id; | |
| deferred_operator_overloads.emplace (std::make_pair (expr_id, deferred)); | |
| } | |
| bool | |
| TypeCheckContext::lookup_deferred_operator_overload ( | |
| HirId id, DeferredOpOverload *deferred) | |
| { | |
| auto it = deferred_operator_overloads.find (id); | |
| if (it == deferred_operator_overloads.end ()) | |
| return false; | |
| *deferred = it->second; | |
| return true; | |
| } | |
| void | |
| TypeCheckContext::iterate_deferred_operator_overloads ( | |
| std::function<bool (HirId, DeferredOpOverload &)> cb) | |
| { | |
| for (auto it = deferred_operator_overloads.begin (); | |
| it != deferred_operator_overloads.end (); it++) | |
| { | |
| if (!cb (it->first, it->second)) | |
| return; | |
| } | |
| } | |
| void | |
| TypeCheckContext::insert_unconstrained_check_marker (HirId id, bool status) | |
| { | |
| unconstrained[id] = status; | |
| } | |
| bool | |
| TypeCheckContext::have_checked_for_unconstrained (HirId id, bool *result) | |
| { | |
| auto it = unconstrained.find (id); | |
| bool found = it != unconstrained.end (); | |
| if (!found) | |
| return false; | |
| *result = it->second; | |
| return true; | |
| } | |
| void | |
| TypeCheckContext::insert_resolved_predicate ( | |
| HirId id, const TyTy::TypeBoundPredicate &predicate) | |
| { | |
| // auto it = predicates.find (id); | |
| // rust_assert (it == predicates.end ()); | |
| predicates.emplace (id, predicate); | |
| } | |
| bool | |
| TypeCheckContext::lookup_predicate (HirId id, TyTy::TypeBoundPredicate *result) | |
| { | |
| auto it = predicates.find (id); | |
| bool found = it != predicates.end (); | |
| if (!found) | |
| return false; | |
| *result = it->second; | |
| return true; | |
| } | |
| std::vector<TyTy::TypeBoundPredicate> | |
| TypeCheckContext::predicates_for_type (const TyTy::BaseType *receiver) const | |
| { | |
| std::vector<TyTy::TypeBoundPredicate> result | |
| = receiver->get_specified_bounds (); | |
| for (const auto &entry : predicates) | |
| { | |
| const auto &predicate = entry.second; | |
| const auto &args = predicate.get_substitution_arguments (); | |
| if (args.get_mappings ().empty ()) | |
| continue; | |
| TyTy::BaseType *bound_self = args.get_mappings ().front ().get_tyty (); | |
| if (bound_self == nullptr | |
| || bound_self->get_kind () == TyTy::TypeKind::ERROR) | |
| continue; | |
| if (receiver->get_kind () != TyTy::TypeKind::PROJECTION | |
| || bound_self->get_kind () != TyTy::TypeKind::PROJECTION) | |
| continue; | |
| const auto *receiver_projection | |
| = static_cast<const TyTy::ProjectionType *> (receiver); | |
| const auto *bound_projection | |
| = static_cast<const TyTy::ProjectionType *> (bound_self); | |
| DefId receiver_item_defid = receiver_projection->get_item_defid (); | |
| DefId bound_item_defid = bound_projection->get_item_defid (); | |
| bool same_item = receiver_item_defid == bound_item_defid; | |
| if (!same_item) | |
| continue; | |
| DefId receiver_trait_defid | |
| = receiver_projection->get_trait_ref ()->get_mappings ().get_defid (); | |
| DefId bound_trait_defid | |
| = bound_projection->get_trait_ref ()->get_mappings ().get_defid (); | |
| bool same_trait = receiver_trait_defid == bound_trait_defid; | |
| if (!same_trait) | |
| continue; | |
| // Compatibility can unify unrelated parameters. Match their declaration | |
| // identities instead; copies and synthetic bound parameters retain them. | |
| const TyTy::BaseType *receiver_self = receiver_projection->get_self (); | |
| const TyTy::BaseType *bound_self_ty = bound_projection->get_self (); | |
| const auto *receiver_param | |
| = receiver_self->try_as<const TyTy::ParamType> (); | |
| const auto *bound_param = bound_self_ty->try_as<const TyTy::ParamType> (); | |
| bool either_self_is_param | |
| = receiver_param != nullptr || bound_param != nullptr; | |
| if (either_self_is_param) | |
| { | |
| bool both_selfs_are_param | |
| = receiver_param != nullptr && bound_param != nullptr; | |
| bool same_declaration | |
| = both_selfs_are_param | |
| && receiver_param->get_decl_id () == bound_param->get_decl_id (); | |
| if (!same_declaration) | |
| continue; | |
| } | |
| else | |
| { | |
| bool selfs_compatible = types_compatable ( | |
| TyTy::TyWithLocation (const_cast<TyTy::BaseType *> (receiver_self)), | |
| TyTy::TyWithLocation (const_cast<TyTy::BaseType *> (bound_self_ty)), | |
| UNKNOWN_LOCATION, false, false); | |
| if (!selfs_compatible) | |
| continue; | |
| } | |
| bool projections_compatible = types_compatable ( | |
| TyTy::TyWithLocation (const_cast<TyTy::BaseType *> (receiver)), | |
| TyTy::TyWithLocation (bound_self), UNKNOWN_LOCATION, false, false); | |
| if (projections_compatible) | |
| result.push_back (predicate); | |
| } | |
| return result; | |
| } | |
| void | |
| TypeCheckContext::insert_query (HirId id) | |
| { | |
| querys_in_progress.insert (id); | |
| } | |
| void | |
| TypeCheckContext::query_completed (HirId id) | |
| { | |
| querys_in_progress.erase (id); | |
| } | |
| bool | |
| TypeCheckContext::query_in_progress (HirId id) const | |
| { | |
| return querys_in_progress.find (id) != querys_in_progress.end (); | |
| } | |
| void | |
| TypeCheckContext::insert_trait_query (DefId id) | |
| { | |
| trait_queries_in_progress.insert (id); | |
| } | |
| void | |
| TypeCheckContext::trait_query_completed (DefId id) | |
| { | |
| trait_queries_in_progress.erase (id); | |
| } | |
| bool | |
| TypeCheckContext::trait_query_in_progress (DefId id) const | |
| { | |
| return trait_queries_in_progress.find (id) | |
| != trait_queries_in_progress.end (); | |
| } | |
| Lifetime | |
| TypeCheckContext::intern_lifetime (const HIR::Lifetime &lifetime) | |
| { | |
| if (lifetime.get_lifetime_type () == AST::Lifetime::NAMED) | |
| { | |
| auto maybe_interned = lookup_lifetime (lifetime); | |
| if (maybe_interned) | |
| return *maybe_interned; | |
| auto interned = next_lifetime_index.next (); | |
| lifetime_name_interner[lifetime.get_name ()] = interned; | |
| return interned; | |
| } | |
| if (lifetime.get_lifetime_type () == AST::Lifetime::WILDCARD) | |
| { | |
| return next_lifetime_index.next (); | |
| } | |
| if (lifetime.get_lifetime_type () == AST::Lifetime::STATIC) | |
| { | |
| return Lifetime::static_lifetime (); | |
| } | |
| rust_unreachable (); | |
| } | |
| tl::optional<Lifetime> | |
| TypeCheckContext::lookup_lifetime (const HIR::Lifetime &lifetime) const | |
| { | |
| if (lifetime.get_lifetime_type () == AST::Lifetime::NAMED) | |
| { | |
| if (lifetime.get_name () == "static") | |
| { | |
| rich_location r (line_table, lifetime.get_locus ()); | |
| r.add_fixit_insert_after (lifetime.get_locus (), | |
| "static is a reserved lifetime name"); | |
| rust_error_at (r, ErrorCode::E0262, | |
| "invalid lifetime parameter name: %qs", | |
| lifetime.get_name ().c_str ()); | |
| return tl::nullopt; | |
| } | |
| const auto name = lifetime.get_name (); | |
| auto it = lifetime_name_interner.find (name); | |
| if (it == lifetime_name_interner.end ()) | |
| return tl::nullopt; | |
| return it->second; | |
| } | |
| if (lifetime.get_lifetime_type () == AST::Lifetime::WILDCARD) | |
| { | |
| return Lifetime::anonymous_lifetime (); | |
| } | |
| if (lifetime.get_lifetime_type () == AST::Lifetime::STATIC) | |
| { | |
| return Lifetime::static_lifetime (); | |
| } | |
| rust_unreachable (); | |
| } | |
| WARN_UNUSED_RESULT tl::optional<TyTy::Region> | |
| TypeCheckContext::lookup_and_resolve_lifetime ( | |
| const HIR::Lifetime &lifetime) const | |
| { | |
| auto maybe_interned = lookup_lifetime (lifetime); | |
| if (!maybe_interned) | |
| return tl::nullopt; | |
| return get_lifetime_resolver ().resolve (maybe_interned.value ()); | |
| } | |
| void | |
| TypeCheckContext::intern_and_insert_lifetime (const HIR::Lifetime &lifetime) | |
| { | |
| get_lifetime_resolver ().insert_mapping (intern_lifetime (lifetime)); | |
| } | |
| WARN_UNUSED_RESULT std::vector<TyTy::Region> | |
| TypeCheckContext::regions_from_generic_args (const HIR::GenericArgs &args) const | |
| { | |
| std::vector<TyTy::Region> regions; | |
| for (const auto &lifetime : args.get_lifetime_args ()) | |
| { | |
| auto resolved = lookup_and_resolve_lifetime (lifetime); | |
| if (!resolved) | |
| { | |
| rust_error_at (lifetime.get_locus (), "unresolved lifetime"); | |
| return {}; | |
| } | |
| regions.push_back (*resolved); | |
| } | |
| return regions; | |
| } | |
| bool | |
| TypeCheckContext::compute_ambigious_op_overload (HirId id, | |
| DeferredOpOverload &op) | |
| { | |
| rust_debug ("attempting resolution of deferred operator overload"); | |
| TyTy::BaseType *lhs = nullptr; | |
| bool ok = lookup_type (op.op.get_lvalue_mappings ().get_hirid (), &lhs); | |
| rust_assert (ok); | |
| TyTy::BaseType *rhs = nullptr; | |
| if (op.op.has_rvalue_mappings ()) | |
| { | |
| bool ok = lookup_type (op.op.get_rvalue_mappings ().get_hirid (), &rhs); | |
| rust_assert (ok); | |
| } | |
| TyTy::BaseType *current_result = op.result_type.get_tyty (); | |
| rust_assert (current_result != nullptr); | |
| rust_debug ("deferred operator expr=%u lhs=%s rhs=%s " | |
| "stored-result=%s current-result=%s result-ref=%u ty-ref=%u", | |
| id, lhs->debug_str ().c_str (), | |
| rhs == nullptr ? "<none>" : rhs->debug_str ().c_str (), | |
| current_result->debug_str ().c_str (), | |
| current_result->debug_str ().c_str (), op.result_type.get_ref (), | |
| current_result->get_ty_ref ()); | |
| TyTy::BaseType *resolved | |
| = TypeCheckExpr::ResolveOpOverload (op.lang_item_type, op.op, lhs, rhs, | |
| op.specified_segment, current_result); | |
| if (resolved == nullptr) | |
| return false; | |
| TyTy::BaseType *result | |
| = unify_site (id, TyTy::TyWithLocation (current_result), | |
| TyTy::TyWithLocation (resolved), op.op.get_locus ()); | |
| rust_assert (result != nullptr); | |
| rust_assert (result->get_kind () != TyTy::TypeKind::ERROR); | |
| return true; | |
| } | |
| void | |
| TypeCheckContext::compute_inference_variables (bool emit_error) | |
| { | |
| auto resolve_deferred_operator_overloads = [this] () { | |
| bool progress; | |
| do | |
| { | |
| progress = false; | |
| for (auto it = deferred_operator_overloads.begin (); | |
| it != deferred_operator_overloads.end ();) | |
| { | |
| if (compute_ambigious_op_overload (it->first, it->second)) | |
| { | |
| it = deferred_operator_overloads.erase (it); | |
| progress = true; | |
| } | |
| else | |
| ++it; | |
| } | |
| } | |
| while (progress); | |
| }; | |
| resolve_deferred_operator_overloads (); | |
| iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool { | |
| return compute_infer_var (id, ty, emit_error); | |
| }); | |
| resolve_deferred_operator_overloads (); | |
| } | |
| bool | |
| TypeCheckContext::compute_infer_var (HirId id, TyTy::BaseType *ty, | |
| bool emit_error) | |
| { | |
| auto &mappings = Analysis::Mappings::get (); | |
| // nothing to do | |
| if (ty->get_kind () != TyTy::TypeKind::INFER) | |
| return true; | |
| TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty); | |
| TyTy::BaseType *default_type; | |
| TyTy::BaseType *current = TyTy::TyVar (ty->get_ref ()).get_tyty (); | |
| rust_assert (current != nullptr); | |
| rust_debug_loc (mappings.lookup_location (id), | |
| "trying to default infer-var id=%u ref=%u ty-ref=%u " | |
| "stored=%s current=%s", | |
| id, ty->get_ref (), ty->get_ty_ref (), | |
| infer_var->debug_str ().c_str (), | |
| current->debug_str ().c_str ()); | |
| bool ok = infer_var->default_type (&default_type); | |
| if (!ok) | |
| { | |
| if (emit_error) | |
| rust_error_at (mappings.lookup_location (id), ErrorCode::E0282, | |
| "type annotations needed"); | |
| return true; | |
| } | |
| rust_debug_loc (mappings.lookup_location (id), "default type selected: %s", | |
| default_type->debug_str ().c_str ()); | |
| auto result | |
| = unify_site (id, TyTy::TyWithLocation (ty), | |
| TyTy::TyWithLocation (default_type), UNDEF_LOCATION); | |
| rust_assert (result); | |
| rust_assert (result->get_kind () != TyTy::TypeKind::ERROR); | |
| result->set_ref (id); | |
| insert_implicit_type (id, result); | |
| return true; | |
| } | |
| TyTy::VarianceAnalysis::CrateCtx & | |
| TypeCheckContext::get_variance_analysis_ctx () | |
| { | |
| return variance_analysis_ctx; | |
| } | |
| // TypeCheckContextItem | |
| TypeCheckContextItem::Item::Item (HIR::Function *item) : item (item) {} | |
| TypeCheckContextItem::Item::Item (HIR::ImplBlock *impl_block, | |
| HIR::Function *item) | |
| : impl_item ({impl_block, item}) | |
| {} | |
| TypeCheckContextItem::Item::Item (HIR::TraitItemFunc *trait_item) | |
| : trait_item (trait_item) | |
| {} | |
| TypeCheckContextItem::TypeCheckContextItem (HIR::Function *item) | |
| : type (ItemType::ITEM), item (item) | |
| {} | |
| TypeCheckContextItem::TypeCheckContextItem (HIR::ImplBlock &impl_block, | |
| HIR::Function *item) | |
| : type (ItemType::IMPL_ITEM), item (&impl_block, item) | |
| {} | |
| TypeCheckContextItem::TypeCheckContextItem (HIR::TraitItemFunc *trait_item) | |
| : type (ItemType::TRAIT_ITEM), item (trait_item) | |
| {} | |
| TypeCheckContextItem::TypeCheckContextItem (const TypeCheckContextItem &other) | |
| : type (other.type), item (other.item) | |
| { | |
| switch (other.type) | |
| { | |
| case ITEM: | |
| item.item = other.item.item; | |
| break; | |
| case IMPL_ITEM: | |
| item.impl_item = other.item.impl_item; | |
| break; | |
| case TRAIT_ITEM: | |
| item.trait_item = other.item.trait_item; | |
| break; | |
| case ERROR: | |
| item.item = nullptr; | |
| break; | |
| } | |
| } | |
| TypeCheckContextItem::TypeCheckContextItem () | |
| : type (ItemType::ERROR), item (static_cast<HIR::Function *> (nullptr)) | |
| {} | |
| TypeCheckContextItem & | |
| TypeCheckContextItem::operator= (const TypeCheckContextItem &other) | |
| { | |
| type = other.type; | |
| switch (other.type) | |
| { | |
| case ITEM: | |
| item.item = other.item.item; | |
| break; | |
| case IMPL_ITEM: | |
| item.impl_item = other.item.impl_item; | |
| break; | |
| case TRAIT_ITEM: | |
| item.trait_item = other.item.trait_item; | |
| break; | |
| case ERROR: | |
| item.item = nullptr; | |
| break; | |
| } | |
| return *this; | |
| } | |
| TypeCheckContextItem | |
| TypeCheckContextItem::get_error () | |
| { | |
| return TypeCheckContextItem (); | |
| } | |
| bool | |
| TypeCheckContextItem::is_error () const | |
| { | |
| return type == ERROR; | |
| } | |
| HIR::Function * | |
| TypeCheckContextItem::get_item () | |
| { | |
| rust_assert (get_type () == ItemType::ITEM); | |
| return item.item; | |
| } | |
| std::pair<HIR::ImplBlock *, HIR::Function *> & | |
| TypeCheckContextItem::get_impl_item () | |
| { | |
| rust_assert (get_type () == ItemType::IMPL_ITEM); | |
| return item.impl_item; | |
| } | |
| HIR::TraitItemFunc * | |
| TypeCheckContextItem::get_trait_item () | |
| { | |
| rust_assert (get_type () == ItemType::TRAIT_ITEM); | |
| return item.trait_item; | |
| } | |
| TypeCheckContextItem::ItemType | |
| TypeCheckContextItem::get_type () const | |
| { | |
| return type; | |
| } | |
| TyTy::FnType * | |
| TypeCheckContextItem::get_context_type () | |
| { | |
| auto &context = *TypeCheckContext::get (); | |
| HirId reference = UNKNOWN_HIRID; | |
| switch (get_type ()) | |
| { | |
| case ITEM: | |
| reference = get_item ()->get_mappings ().get_hirid (); | |
| break; | |
| case IMPL_ITEM: | |
| reference = get_impl_item ().second->get_mappings ().get_hirid (); | |
| break; | |
| case TRAIT_ITEM: | |
| reference = get_trait_item ()->get_mappings ().get_hirid (); | |
| break; | |
| case ERROR: | |
| rust_unreachable (); | |
| return nullptr; | |
| } | |
| rust_assert (reference != UNKNOWN_HIRID); | |
| TyTy::BaseType *lookup = nullptr; | |
| bool ok = context.lookup_type (reference, &lookup); | |
| rust_assert (ok); | |
| rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF); | |
| return static_cast<TyTy::FnType *> (lookup); | |
| } | |
| DefId | |
| TypeCheckContextItem::get_defid () const | |
| { | |
| switch (get_type ()) | |
| { | |
| case ITEM: | |
| return item.item->get_mappings ().get_defid (); | |
| case IMPL_ITEM: | |
| return item.impl_item.second->get_mappings ().get_defid (); | |
| case TRAIT_ITEM: | |
| return item.trait_item->get_mappings ().get_defid (); | |
| case ERROR: | |
| return UNKNOWN_DEFID; | |
| } | |
| return UNKNOWN_DEFID; | |
| } | |
| // TypeCheckBlockContextItem | |
| TypeCheckBlockContextItem::Item::Item (HIR::ImplBlock *b) : block (b) {} | |
| TypeCheckBlockContextItem::Item::Item (HIR::Trait *t) : trait (t) {} | |
| TypeCheckBlockContextItem::TypeCheckBlockContextItem (HIR::ImplBlock *block) | |
| : type (TypeCheckBlockContextItem::ItemType::IMPL_BLOCK), item (block) | |
| {} | |
| TypeCheckBlockContextItem::TypeCheckBlockContextItem (HIR::Trait *trait) | |
| : type (TypeCheckBlockContextItem::ItemType::TRAIT), item (trait) | |
| {} | |
| bool | |
| TypeCheckBlockContextItem::is_impl_block () const | |
| { | |
| return type == IMPL_BLOCK; | |
| } | |
| bool | |
| TypeCheckBlockContextItem::is_trait_block () const | |
| { | |
| return type == TRAIT; | |
| } | |
| HIR::ImplBlock & | |
| TypeCheckBlockContextItem::get_impl_block () | |
| { | |
| return *(item.block); | |
| } | |
| HIR::Trait & | |
| TypeCheckBlockContextItem::get_trait () | |
| { | |
| return *(item.trait); | |
| } | |
| } // namespace Resolver | |
| } // namespace Rust |