Line data Source code
1 : // Copyright (C) 2021-2026 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-trait-resolve.h"
20 : #include "rich-location.h"
21 : #include "rust-hir-trait-reference.h"
22 : #include "rust-hir-type-check-expr.h"
23 : #include "rust-rib.h"
24 : #include "rust-substitution-mapper.h"
25 : #include "text-range-label.h"
26 : #include "rust-type-util.h"
27 : #include "rust-finalized-name-resolution-context.h"
28 :
29 : namespace Rust {
30 : namespace Resolver {
31 :
32 : static bool
33 6887 : validate_impl_substitution_bounds (
34 : const std::vector<TyTy::SubstitutionArg> &resolved_args, location_t locus,
35 : bool emit_error)
36 : {
37 6887 : auto &mctx = Analysis::Mappings::get ();
38 :
39 6887 : std::vector<TyTy::SubstitutionArg> args;
40 7636 : for (const auto &arg : resolved_args)
41 749 : args.push_back (arg);
42 :
43 6887 : TyTy::SubstitutionArgumentMappings mappings (std::move (args),
44 : {} /*binding_args*/,
45 6887 : TyTy::RegionParamList (0),
46 6887 : locus);
47 :
48 7635 : for (const auto &arg : resolved_args)
49 : {
50 749 : TyTy::BaseGeneric *param
51 749 : = const_cast<TyTy::BaseGeneric *> (arg.get_param_ty ());
52 749 : if (param == nullptr)
53 0 : continue;
54 :
55 749 : TyTy::BaseType *resolved_arg = arg.get_tyty ();
56 749 : if (resolved_arg->get_kind () == TyTy::TypeKind::PARAM)
57 20 : resolved_arg
58 20 : = static_cast<TyTy::ParamType *> (resolved_arg)->resolve ();
59 :
60 749 : if (resolved_arg->get_kind () == TyTy::TypeKind::PARAM
61 749 : || resolved_arg->get_kind () == TyTy::TypeKind::INFER)
62 102 : continue;
63 :
64 647 : auto arg_type_locus
65 647 : = mctx.lookup_location (arg.get_tyty ()->get_ty_ref ());
66 1463 : for (auto bound : param->get_specified_bounds ())
67 : {
68 817 : auto bound_locus = bound.get_locus ();
69 817 : auto trait_locus = bound.get ()->get_locus ();
70 817 : bound.apply_argument_mappings (mappings, false /*is_super_trait*/);
71 :
72 817 : if (!resolved_arg->satisfies_bound (bound, false /*emit_error*/))
73 : {
74 1 : if (emit_error)
75 : {
76 1 : rich_location r (line_table, locus);
77 :
78 2 : std::string arg_label_text = "the trait " + bound.get_name ()
79 2 : + " is not implemented for "
80 2 : + resolved_arg->get_name ();
81 :
82 1 : text_range_label arg_label (arg_label_text.c_str ());
83 1 : r.add_range (arg_type_locus, SHOW_RANGE_WITHOUT_CARET,
84 : &arg_label);
85 :
86 1 : bool ambiguous = false;
87 1 : auto *trait_impl
88 1 : = lookup_associated_impl_block (bound, resolved_arg,
89 : &ambiguous);
90 1 : text_range_label trait_label (
91 1 : "this trait has no implementations, consider adding one");
92 1 : if (trait_impl == nullptr)
93 1 : r.add_range (trait_locus, SHOW_RANGE_WITHOUT_CARET,
94 : &trait_label);
95 :
96 1 : text_range_label bound_label (
97 1 : "unsatisfied trait bound introduced here");
98 1 : r.add_range (bound_locus, SHOW_RANGE_WITHOUT_CARET,
99 : &bound_label);
100 :
101 1 : rust_error_at (r, ErrorCode::E0277,
102 : "the trait bound %<%s: %s%> is not satisfied",
103 2 : resolved_arg->get_name ().c_str (),
104 2 : bound.get_name ().c_str ());
105 1 : }
106 1 : return false;
107 : }
108 817 : }
109 : }
110 :
111 : return true;
112 6887 : }
113 :
114 : TraitItemReference
115 6020 : ResolveTraitItemToRef::Resolve (
116 : HIR::TraitItem &item, TyTy::BaseType *self,
117 : std::vector<TyTy::SubstitutionParamMapping> substitutions)
118 : {
119 6020 : ResolveTraitItemToRef resolver (self, std::move (substitutions));
120 6020 : item.accept_vis (resolver);
121 6020 : return std::move (resolver.resolved);
122 6020 : }
123 :
124 : void
125 1076 : ResolveTraitItemToRef::visit (HIR::TraitItemType &type)
126 : {
127 : // create trait-item-ref
128 1076 : location_t locus = type.get_locus ();
129 1076 : bool is_optional = false;
130 2152 : std::string identifier = type.get_name ().as_string ();
131 :
132 1076 : resolved = TraitItemReference (identifier, is_optional,
133 : TraitItemReference::TraitItemType::TYPE, &type,
134 3228 : self, substitutions, locus);
135 1076 : }
136 :
137 : void
138 173 : ResolveTraitItemToRef::visit (HIR::TraitItemConst &cst)
139 : {
140 : // create trait-item-ref
141 173 : location_t locus = cst.get_locus ();
142 173 : bool is_optional = cst.has_expr ();
143 346 : std::string identifier = cst.get_name ().as_string ();
144 :
145 173 : resolved = TraitItemReference (identifier, is_optional,
146 : TraitItemReference::TraitItemType::CONST, &cst,
147 519 : self, substitutions, locus);
148 173 : }
149 :
150 : void
151 4771 : ResolveTraitItemToRef::visit (HIR::TraitItemFunc &fn)
152 : {
153 : // create trait-item-ref
154 4771 : location_t locus = fn.get_locus ();
155 4771 : bool is_optional = fn.has_definition ();
156 9542 : std::string identifier = fn.get_decl ().get_function_name ().as_string ();
157 :
158 4771 : resolved = TraitItemReference (identifier, is_optional,
159 : TraitItemReference::TraitItemType::FN, &fn,
160 14313 : self, std::move (substitutions), locus);
161 4771 : }
162 :
163 6020 : ResolveTraitItemToRef::ResolveTraitItemToRef (
164 : TyTy::BaseType *self,
165 : std::vector<TyTy::SubstitutionParamMapping> &&substitutions)
166 6020 : : TypeCheckBase (), resolved (TraitItemReference::error ()), self (self),
167 6020 : substitutions (std::move (substitutions))
168 6020 : {}
169 :
170 : // TraitItemReference items
171 :
172 : TraitReference *
173 306708 : TraitResolver::Resolve (HIR::TypePath &path)
174 : {
175 306708 : TraitResolver resolver;
176 306708 : return resolver.resolve_path (path);
177 306708 : }
178 :
179 : TraitReference *
180 295988 : TraitResolver::Resolve (HIR::Trait &trait)
181 : {
182 295988 : TraitResolver resolver;
183 295988 : return resolver.resolve_trait (&trait);
184 295988 : }
185 :
186 : TraitReference *
187 612 : TraitResolver::Lookup (HIR::TypePath &path)
188 : {
189 612 : TraitResolver resolver;
190 612 : return resolver.lookup_path (path);
191 612 : }
192 :
193 : HIR::Trait *
194 4854516 : TraitResolver::ResolveHirItem (const HIR::TypePath &path)
195 : {
196 4854516 : TraitResolver resolver;
197 :
198 4854516 : HIR::Trait *lookup = nullptr;
199 4854516 : bool ok = resolver.resolve_path_to_trait (path, &lookup);
200 4854516 : return ok ? lookup : nullptr;
201 4854516 : }
202 :
203 5457824 : TraitResolver::TraitResolver () : TypeCheckBase () {}
204 :
205 : bool
206 5161836 : TraitResolver::resolve_path_to_trait (const HIR::TypePath &path,
207 : HIR::Trait **resolved) const
208 : {
209 5161836 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
210 :
211 5161836 : NodeId ref;
212 5161836 : if (auto ref_opt = nr_ctx.lookup (path.get_mappings ().get_nodeid (),
213 5161836 : Resolver2_0::Namespace::Types))
214 : {
215 5161836 : ref = *ref_opt;
216 : }
217 : else
218 : {
219 0 : rust_error_at (path.get_locus (), "Failed to resolve path to node-id");
220 0 : return false;
221 : }
222 :
223 5161836 : auto hid = mappings.lookup_node_to_hir (ref);
224 5161836 : if (!hid)
225 : {
226 0 : rust_error_at (path.get_locus (), "Failed to resolve path to hir-id");
227 0 : return false;
228 : }
229 :
230 5161836 : auto resolved_item = mappings.hir.items.lookup (hid.value ());
231 5161836 : if (!resolved_item.has_value ())
232 : {
233 0 : rust_error_at (path.get_locus (),
234 : "Failed to resolve trait by looking up hir node");
235 0 : return false;
236 : }
237 :
238 5161836 : if (resolved_item.value ()->get_item_kind () != HIR::Item::ItemKind::Trait)
239 : {
240 3 : rich_location r (line_table, path.get_locus ());
241 3 : r.add_fixit_replace ("not a trait");
242 3 : rust_error_at (r, ErrorCode::E0404, "Expected a trait found %qs",
243 6 : path.as_simple_path ().as_string ().c_str ());
244 3 : return false;
245 3 : }
246 :
247 5161833 : *resolved = static_cast<HIR::Trait *> (*resolved_item);
248 5161833 : return true;
249 : }
250 :
251 : TraitReference *
252 306708 : TraitResolver::resolve_path (HIR::TypePath &path)
253 : {
254 306708 : HIR::Trait *resolved_trait_reference;
255 306708 : bool ok = resolve_path_to_trait (path, &resolved_trait_reference);
256 306708 : if (!ok)
257 3 : return &TraitReference::error_node ();
258 :
259 306705 : return resolve_trait (resolved_trait_reference);
260 : }
261 :
262 : TraitReference *
263 602693 : TraitResolver::resolve_trait (HIR::Trait *trait_reference)
264 : {
265 602693 : TraitReference *tref = &TraitReference::error_node ();
266 602693 : if (context->lookup_trait_reference (
267 602693 : trait_reference->get_mappings ().get_defid (), &tref))
268 : {
269 597315 : return tref;
270 : }
271 :
272 5378 : DefId trait_id = trait_reference->get_mappings ().get_defid ();
273 5378 : if (context->trait_query_in_progress (trait_id))
274 : {
275 4 : rust_error_at (
276 : trait_reference->get_locus (), ErrorCode::E0391,
277 : "cycle detected when computing the super predicates of %qs",
278 4 : trait_reference->get_name ().as_string ().c_str ());
279 4 : return &TraitReference::error_node ();
280 : }
281 :
282 5374 : TraitQueryGuard guard (trait_id);
283 5374 : TyTy::BaseType *self = nullptr;
284 5374 : std::vector<TyTy::SubstitutionParamMapping> substitutions;
285 :
286 5374 : auto lifetime_pin = context->push_clean_lifetime_resolver ();
287 :
288 : // this needs to be special cased for the sized trait to not auto implemented
289 : // Sized on Self
290 11900 : for (auto &generic_param : trait_reference->get_generic_params ())
291 : {
292 6526 : switch (generic_param.get ()->get_kind ())
293 : {
294 35 : case HIR::GenericParam::GenericKind::LIFETIME:
295 35 : {
296 35 : auto &lifetime_param
297 35 : = static_cast<HIR::LifetimeParam &> (*generic_param);
298 70 : context->intern_and_insert_lifetime (
299 35 : lifetime_param.get_lifetime ());
300 : }
301 35 : break;
302 :
303 : case HIR::GenericParam::GenericKind::CONST:
304 : // FIXME: Skipping Lifetime and Const completely until better
305 : // handling.
306 : break;
307 :
308 6489 : case HIR::GenericParam::GenericKind::TYPE:
309 6489 : {
310 6489 : auto &typaram = static_cast<HIR::TypeParam &> (*generic_param);
311 6489 : bool is_self
312 6489 : = typaram.get_type_representation ().as_string ().compare ("Self")
313 6489 : == 0;
314 :
315 : // https://doc.rust-lang.org/std/marker/trait.Sized.html
316 : // The one exception is the implicit Self type of a trait
317 6489 : bool apply_sized = !is_self;
318 6489 : auto param_type
319 6489 : = TypeResolveGenericParam::Resolve (*generic_param, true,
320 6489 : apply_sized);
321 :
322 6489 : context->insert_type (generic_param->get_mappings (), param_type);
323 6489 : substitutions.emplace_back (typaram, param_type);
324 :
325 6489 : if (is_self)
326 : {
327 5374 : rust_assert (param_type->get_kind () == TyTy::TypeKind::PARAM);
328 5374 : TyTy::ParamType *p
329 : = static_cast<TyTy::ParamType *> (param_type);
330 5374 : p->set_implicit_self_trait ();
331 5374 : self = p;
332 : }
333 : }
334 6489 : break;
335 : }
336 : }
337 5374 : rust_assert (self != nullptr);
338 :
339 : // Check if there is a super-trait, and apply this bound to the Self
340 : // TypeParam
341 10748 : std::vector<TyTy::TypeBoundPredicate> specified_bounds;
342 :
343 : // copy the substitition mappings
344 5374 : std::vector<TyTy::SubstitutionParamMapping> self_subst_copy;
345 5374 : self_subst_copy.reserve (substitutions.size ());
346 :
347 11863 : for (auto &sub : substitutions)
348 6489 : self_subst_copy.push_back (sub.clone ());
349 :
350 : // They also inherit themselves as a bound this enables a trait item to
351 : // reference other Self::trait_items
352 5374 : specified_bounds.emplace_back (trait_reference->get_mappings ().get_defid (),
353 : std::move (self_subst_copy),
354 10748 : BoundPolarity::RegularBound,
355 5374 : trait_reference->get_locus ());
356 :
357 : // look for any
358 5374 : std::vector<TyTy::TypeBoundPredicate> super_traits;
359 5374 : if (trait_reference->has_type_param_bounds ())
360 : {
361 2236 : for (auto &bound : trait_reference->get_type_param_bounds ())
362 : {
363 1227 : if (bound->get_bound_type ()
364 : == HIR::TypeParamBound::BoundType::TRAITBOUND)
365 : {
366 1220 : HIR::TraitBound *b
367 1220 : = static_cast<HIR::TraitBound *> (bound.get ());
368 :
369 1220 : auto predicate = get_predicate_from_bound (
370 : b->get_path (),
371 : tl::nullopt /*this will setup a PLACEHOLDER for self*/,
372 1220 : BoundPolarity::RegularBound, false, true);
373 1220 : if (predicate.is_error ())
374 15 : return &TraitReference::error_node ();
375 :
376 1205 : specified_bounds.push_back (predicate);
377 1205 : super_traits.push_back (predicate);
378 1220 : }
379 : }
380 : }
381 5359 : self->inherit_bounds (specified_bounds);
382 :
383 5359 : context->block_context ().enter (TypeCheckBlockContextItem (trait_reference));
384 5359 : std::vector<TraitItemReference> item_refs;
385 11379 : for (auto &item : trait_reference->get_trait_items ())
386 : {
387 : // make a copy of the substs
388 6020 : std::vector<TyTy::SubstitutionParamMapping> item_subst;
389 6020 : item_subst.reserve (substitutions.size ());
390 :
391 14364 : for (auto &sub : substitutions)
392 8344 : item_subst.push_back (sub.clone ());
393 :
394 6020 : TraitItemReference trait_item_ref
395 6020 : = ResolveTraitItemToRef::Resolve (*item.get (), self,
396 6020 : std::move (item_subst));
397 6020 : item_refs.push_back (std::move (trait_item_ref));
398 6020 : }
399 :
400 5359 : TraitReference trait_object (trait_reference, item_refs, super_traits,
401 5359 : std::move (substitutions));
402 5359 : context->insert_trait_reference (
403 5359 : trait_reference->get_mappings ().get_defid (), std::move (trait_object));
404 :
405 5359 : tref = &TraitReference::error_node ();
406 5359 : bool ok = context->lookup_trait_reference (
407 5359 : trait_reference->get_mappings ().get_defid (), &tref);
408 5359 : rust_assert (ok);
409 :
410 : // hook to allow the trait to resolve its optional item blocks, we cant
411 : // resolve the blocks of functions etc because it can end up in a recursive
412 : // loop of trying to resolve traits as required by the types
413 5359 : tref->on_resolved ();
414 5359 : context->block_context ().exit ();
415 :
416 5359 : return tref;
417 5374 : }
418 :
419 : TraitReference *
420 612 : TraitResolver::lookup_path (HIR::TypePath &path)
421 : {
422 612 : HIR::Trait *resolved_trait_reference;
423 612 : bool ok = resolve_path_to_trait (path, &resolved_trait_reference);
424 612 : if (!ok)
425 0 : return &TraitReference::error_node ();
426 :
427 612 : TraitReference *tref = &TraitReference::error_node ();
428 612 : if (context->lookup_trait_reference (
429 612 : resolved_trait_reference->get_mappings ().get_defid (), &tref))
430 : {
431 612 : return tref;
432 : }
433 0 : return &TraitReference::error_node ();
434 : }
435 :
436 : void
437 6020 : TraitItemReference::on_resolved (const TraitReference *tref)
438 : {
439 6020 : switch (type)
440 : {
441 173 : case CONST:
442 173 : resolve_item (tref, static_cast<HIR::TraitItemConst &> (*hir_trait_item));
443 173 : break;
444 :
445 1076 : case TYPE:
446 1076 : resolve_item (tref, static_cast<HIR::TraitItemType &> (*hir_trait_item));
447 1076 : break;
448 :
449 4771 : case FN:
450 4771 : {
451 4771 : TyTy::BaseType *fn_type = get_tyty ();
452 4771 : if (is_optional () && fn_type->get_kind () == TyTy::TypeKind::FNDEF)
453 1766 : context->mark_function_body_pending (
454 : static_cast<TyTy::FnType *> (fn_type)->get_id ());
455 : }
456 : break;
457 :
458 : default:
459 : break;
460 : }
461 6020 : }
462 :
463 : void
464 3776 : TraitItemReference::resolve_default_function_body (const TraitReference *tref)
465 : {
466 3776 : if (type != FN || !is_optional ())
467 : return;
468 :
469 926 : auto &func = static_cast<HIR::TraitItemFunc &> (*hir_trait_item);
470 926 : TyTy::BaseType *item_tyty = get_tyty ();
471 926 : if (item_tyty->get_kind () != TyTy::TypeKind::FNDEF)
472 : return;
473 :
474 926 : auto fn_type = static_cast<TyTy::FnType *> (item_tyty);
475 926 : if (!context->function_body_pending (fn_type->get_id ()))
476 : return;
477 :
478 926 : resolve_item (tref, func);
479 : }
480 :
481 : void
482 1076 : TraitItemReference::resolve_item (const TraitReference *tref,
483 : HIR::TraitItemType &type)
484 : {
485 1076 : auto lifetime_pin = context->push_clean_lifetime_resolver ();
486 2657 : for (auto ¶m : tref->get_hir_trait_ref ()->get_generic_params ())
487 : {
488 1581 : if (param->get_kind () == HIR::GenericParam::GenericKind::LIFETIME)
489 : {
490 8 : auto &lifetime_param = static_cast<HIR::LifetimeParam &> (*param);
491 8 : context->intern_and_insert_lifetime (lifetime_param.get_lifetime ());
492 : }
493 : }
494 :
495 1076 : auto substitutions = inherited_substitutions;
496 1076 : tl::optional<TypeCheckContext::LifetimeResolverGuard> binder_pin;
497 1076 : if (type.has_generics ())
498 : {
499 17 : binder_pin.emplace (*context,
500 17 : TypeCheckContext::LifetimeResolverGuard::BINDER);
501 17 : TypeCheckBase::ResolveGenericParams (HIR::Item::ItemKind::TypeAlias,
502 : type.get_locus (),
503 17 : type.get_generic_params (),
504 : substitutions, false, ABI::RUST);
505 : }
506 :
507 1076 : size_t inherited_count = inherited_substitutions.size ();
508 1076 : auto projection
509 1076 : = new TyTy::ProjectionType (type.get_mappings ().get_hirid (), nullptr,
510 1076 : tref, type.get_mappings ().get_defid (),
511 : substitutions, self,
512 2152 : TyTy::SubstitutionArgumentMappings::error (),
513 2152 : {}, {}, inherited_count);
514 :
515 1076 : context->insert_type (type.get_mappings (), projection);
516 :
517 : // Attach the bounds declared on the associated type itself:
518 : //
519 : // type IntoIter: Iterator<Item = Self::Item>
520 : //
521 : // so satisfies_bound finds them in specified_bounds
522 1076 : if (type.has_type_param_bounds ())
523 : {
524 114 : std::vector<TyTy::TypeBoundPredicate> trait_item_bounds;
525 298 : for (auto &bound : type.get_type_param_bounds ())
526 : {
527 184 : if (bound->get_bound_type ()
528 : != HIR::TypeParamBound::BoundType::TRAITBOUND)
529 1 : continue;
530 183 : auto *b = static_cast<HIR::TraitBound *> (bound.get ());
531 183 : auto predicate = TypeCheckBase::ResolvePredicateFromBound (
532 : b->get_path (),
533 : tl::nullopt /*this will setup a PLACEHOLDER for self*/,
534 183 : BoundPolarity::RegularBound, false, true);
535 183 : if (!predicate.is_error ())
536 183 : trait_item_bounds.push_back (std::move (predicate));
537 183 : }
538 114 : if (!trait_item_bounds.empty ())
539 113 : projection->inherit_bounds (trait_item_bounds);
540 114 : }
541 1076 : }
542 :
543 : void
544 173 : TraitItemReference::resolve_item (const TraitReference *tref,
545 : HIR::TraitItemConst &constant)
546 : {
547 173 : TyTy::BaseType *ty = nullptr;
548 173 : if (constant.has_type ())
549 11 : ty = TypeCheckType::Resolve (constant.get_type ());
550 :
551 173 : TyTy::BaseType *expr = nullptr;
552 173 : if (constant.has_expr ())
553 11 : expr = TypeCheckExpr::Resolve (constant.get_expr ());
554 :
555 184 : bool have_specified_ty = ty != nullptr && !ty->is<TyTy::ErrorType> ();
556 184 : bool have_expr_ty = expr != nullptr && !expr->is<TyTy::ErrorType> ();
557 :
558 10 : if (have_specified_ty && have_expr_ty)
559 : {
560 20 : coercion_site (constant.get_mappings ().get_hirid (),
561 : TyTy::TyWithLocation (ty,
562 10 : constant.get_type ().get_locus ()),
563 : TyTy::TyWithLocation (expr,
564 10 : constant.get_expr ().get_locus ()),
565 : constant.get_locus ());
566 : }
567 173 : }
568 :
569 : void
570 926 : TraitItemReference::resolve_item (const TraitReference *tref,
571 : HIR::TraitItemFunc &func)
572 : {
573 926 : TyTy::BaseType *item_tyty = get_tyty ();
574 926 : if (item_tyty->get_kind () == TyTy::TypeKind::ERROR)
575 : return;
576 :
577 926 : if (!is_optional ())
578 : return;
579 :
580 : // check the block and return types
581 926 : rust_assert (item_tyty->get_kind () == TyTy::TypeKind::FNDEF);
582 :
583 : // need to get the return type from this
584 926 : TyTy::FnType *resolved_fn_type = static_cast<TyTy::FnType *> (item_tyty);
585 926 : context->clear_function_body_pending (resolved_fn_type->get_id ());
586 926 : auto expected_ret_tyty = resolved_fn_type->get_return_type ();
587 926 : context->push_return_type (TypeCheckContextItem (&func), expected_ret_tyty);
588 :
589 926 : auto block_expr_ty = TypeCheckExpr::Resolve (func.get_block_expr ());
590 :
591 926 : location_t fn_return_locus
592 926 : = func.get_decl ().has_return_type ()
593 926 : ? func.get_decl ().get_return_type ().get_locus ()
594 260 : : func.get_locus ();
595 :
596 1852 : coercion_site (func.get_mappings ().get_hirid (),
597 926 : TyTy::TyWithLocation (expected_ret_tyty, fn_return_locus),
598 926 : TyTy::TyWithLocation (block_expr_ty), func.get_locus ());
599 :
600 926 : context->pop_return_type ();
601 : }
602 :
603 : TyTy::SubstitutionArgumentMappings
604 3925 : AssociatedImplTrait::bind_impl_for_projection (TyTy::ProjectionType &proj,
605 : location_t locus)
606 : {
607 3925 : std::vector<TyTy::SubstitutionParamMapping> impl_substitutions;
608 4498 : for (auto &generic_param : impl->get_generic_params ())
609 : {
610 573 : if (generic_param->get_kind () != HIR::GenericParam::GenericKind::TYPE
611 573 : && generic_param->get_kind ()
612 : != HIR::GenericParam::GenericKind::CONST)
613 2 : continue;
614 571 : TyTy::BaseType *l = nullptr;
615 571 : bool ok
616 571 : = context->lookup_type (generic_param->get_mappings ().get_hirid (),
617 : &l);
618 571 : if (!ok)
619 0 : continue;
620 :
621 571 : TyTy::BaseGeneric *param = nullptr;
622 571 : if (l->get_kind () == TyTy::TypeKind::PARAM)
623 400 : param = static_cast<TyTy::ParamType *> (l);
624 171 : else if (l->get_kind () == TyTy::TypeKind::CONST
625 171 : && l->as_const_type ()->const_kind ()
626 : == TyTy::BaseConstType::ConstKind::Decl)
627 0 : param = static_cast<TyTy::ConstParamType *> (l);
628 571 : if (param != nullptr)
629 400 : impl_substitutions.emplace_back (*generic_param, param);
630 : }
631 :
632 : // Build infer args for each impl param so we dont mutate the impls own
633 : // ParamTys when unifying. param_mappings records impl_param_symbol ->
634 : // hirid_of_fresh_infer_var so we can read out what each param resolved to
635 : // after unification.
636 3925 : std::vector<TyTy::SubstitutionArg> infer_arg_vec;
637 3925 : std::map<std::string, HirId> param_mappings;
638 4325 : for (auto &p : impl_substitutions)
639 : {
640 400 : const std::string &symbol = p.get_param_ty ()->get_symbol ();
641 400 : TyTy::TyVar infer_var
642 400 : = p.get_generic_param ().get_kind ()
643 : == HIR::GenericParam::GenericKind::CONST
644 400 : ? TyTy::TyVar::get_implicit_const_infer_var (locus)
645 400 : : TyTy::TyVar::get_implicit_infer_var (locus);
646 400 : TyTy::BaseType *resolved = infer_var.get_tyty ();
647 400 : infer_arg_vec.emplace_back (&p, resolved);
648 400 : param_mappings[symbol] = resolved->get_ref ();
649 400 : }
650 3925 : TyTy::SubstitutionArgumentMappings infer_arguments (
651 : std::move (infer_arg_vec), {} /*binding_args*/,
652 3925 : TyTy::RegionParamList (0) /*regions*/, locus);
653 :
654 3925 : TyTy::BaseType *impl_self_infer
655 3925 : = !self->is_concrete ()
656 3925 : ? SubstMapperInternal::Resolve (self->clone (), infer_arguments)
657 3693 : : self->clone ();
658 :
659 : // sub the impls trait predicate args with infers:
660 : // SliceIndex<[Y]> -> SliceIndex<[?Y]>
661 3925 : const TyTy::TypeBoundPredicate &impl_predicate = predicate;
662 3925 : std::vector<TyTy::BaseType *> impl_predicate_args;
663 6555 : for (size_t i = 1; i < impl_predicate.get_substs ().size (); i++)
664 : {
665 2630 : auto p = impl_predicate.get_substs ().at (i).get_param_ty ();
666 2630 : TyTy::BaseType *r = p->resolve ();
667 2630 : if (!r->is_concrete ())
668 224 : r = SubstMapperInternal::Resolve (r, infer_arguments);
669 2630 : impl_predicate_args.push_back (r);
670 : }
671 :
672 : // Read the projections own (trait-coord) substs and skip Self at index 0.
673 : // The projection's substs match by symbol in handle_substitions, so a
674 : // fn-level substitution that binds T won't have walked into the
675 : // projections X slot whose resolved value is [T]. Re-apply the
676 : // projections own used_arguments to each arg so any stale impl-coord
677 : // params inside (the T inside [T]) are followed through to their
678 : // current bindings
679 3925 : TyTy::SubstitutionArgumentMappings proj_used
680 3925 : = proj.get_substitution_arguments ();
681 3925 : std::vector<TyTy::BaseType *> proj_args;
682 6562 : for (size_t i = 1; i < proj.get_substs ().size (); i++)
683 : {
684 2637 : auto p = proj.get_substs ().at (i).get_param_ty ();
685 2637 : TyTy::BaseType *r = p->resolve ();
686 2637 : if (!r->is_concrete () && !proj_used.is_empty ())
687 14 : r = SubstMapperInternal::Resolve (r, proj_used);
688 2637 : proj_args.push_back (r);
689 : }
690 :
691 3925 : if (impl_predicate_args.size () != proj_args.size ())
692 7 : return TyTy::SubstitutionArgumentMappings::error ();
693 :
694 : // unify trait args [?Y] -> [{integer}]
695 6548 : for (size_t i = 0; i < proj_args.size (); i++)
696 : {
697 2630 : TyTy::BaseType *proj_arg = proj_args[i];
698 2630 : if (impl_predicate_args[i]->get_kind () == TyTy::TypeKind::SLICE
699 2630 : && proj_arg->get_kind () != TyTy::TypeKind::SLICE)
700 : {
701 84 : auto &mappings = Analysis::Mappings::get ();
702 84 : HirId id = mappings.get_next_hir_id ();
703 84 : auto *wrapped
704 : = new TyTy::SliceType (id, proj_arg->get_locus (),
705 84 : TyTy::TyVar (proj_arg->get_ref ()));
706 84 : context->insert_implicit_type (id, wrapped);
707 84 : proj_arg = wrapped;
708 : }
709 :
710 2630 : auto *res = unify_site_and (0 /*id*/,
711 2630 : TyTy::TyWithLocation (impl_predicate_args[i]),
712 2630 : TyTy::TyWithLocation (proj_arg), locus,
713 : false /*emit_errors*/, true /*commit_if_ok*/,
714 : true /*infer*/, true /*cleanup_on_fail*/,
715 : false /*check_bounds*/);
716 2630 : if (res->get_kind () == TyTy::TypeKind::ERROR)
717 0 : return TyTy::SubstitutionArgumentMappings::error ();
718 : }
719 :
720 : // unify self Range<{integer}> -> Range<usize>
721 3918 : auto *res = unify_site_and (impl->get_mappings ().get_hirid (),
722 3918 : TyTy::TyWithLocation (proj.get_self ()),
723 3918 : TyTy::TyWithLocation (impl_self_infer), locus,
724 : false /*emit_errors*/, true /*commit_if_ok*/,
725 : true /*infer*/, true /*cleanup_on_fail*/,
726 : false /*check_bounds*/);
727 3918 : if (res->get_kind () == TyTy::TypeKind::ERROR)
728 0 : return TyTy::SubstitutionArgumentMappings::error ();
729 :
730 3918 : std::vector<TyTy::SubstitutionArg> resolved_args;
731 4318 : for (auto &p : impl_substitutions)
732 : {
733 400 : const std::string &symbol = p.get_param_ty ()->get_symbol ();
734 400 : auto it = param_mappings.find (symbol);
735 400 : if (it == param_mappings.end ())
736 0 : continue;
737 400 : TyTy::BaseType *r = nullptr;
738 400 : bool ok = context->lookup_type (it->second, &r);
739 400 : if (!ok)
740 0 : continue;
741 :
742 : // r can keep the ref of whatever it was unified with, which may not
743 : // map back to r, so point it at the infer var that does
744 400 : if (r->get_ref () != it->second)
745 : {
746 399 : r = r->clone ();
747 399 : r->set_ref (it->second);
748 : }
749 400 : resolved_args.emplace_back (&p, r);
750 400 : }
751 :
752 3918 : if (!validate_impl_substitution_bounds (resolved_args, locus,
753 : false /*emit_error*/))
754 0 : return TyTy::SubstitutionArgumentMappings::error ();
755 :
756 3918 : return TyTy::SubstitutionArgumentMappings (std::move (resolved_args),
757 : {} /*binding_args*/,
758 3918 : TyTy::RegionParamList (0)
759 : /*regions*/,
760 7836 : locus);
761 7843 : }
762 :
763 : TyTy::SubstitutionArgumentMappings
764 2969 : AssociatedImplTrait::bind_impl_for_bound (TyTy::BaseType *receiver,
765 : const TyTy::TypeBoundPredicate &bound,
766 : location_t locus, bool emit_error)
767 : {
768 : // Same shape as bind_impl_for_projection but the receiver/trait-args are
769 : // taken from the (binding, bound) pair instead of a ProjectionType.
770 2969 : std::vector<TyTy::SubstitutionParamMapping> impl_substitutions;
771 3341 : for (auto &generic_param : impl->get_generic_params ())
772 : {
773 372 : if (generic_param->get_kind () != HIR::GenericParam::GenericKind::TYPE
774 372 : && generic_param->get_kind ()
775 : != HIR::GenericParam::GenericKind::CONST)
776 23 : continue;
777 349 : TyTy::BaseType *l = nullptr;
778 349 : bool ok
779 349 : = context->lookup_type (generic_param->get_mappings ().get_hirid (),
780 : &l);
781 349 : if (!ok)
782 0 : continue;
783 :
784 349 : TyTy::BaseGeneric *param = nullptr;
785 349 : if (l->get_kind () == TyTy::TypeKind::PARAM)
786 343 : param = static_cast<TyTy::ParamType *> (l);
787 6 : else if (l->get_kind () == TyTy::TypeKind::CONST
788 6 : && l->as_const_type ()->const_kind ()
789 : == TyTy::BaseConstType::ConstKind::Decl)
790 6 : param = static_cast<TyTy::ConstParamType *> (l);
791 349 : if (param != nullptr)
792 349 : impl_substitutions.emplace_back (*generic_param, param);
793 : }
794 :
795 2969 : std::vector<TyTy::SubstitutionArg> infer_arg_vec;
796 2969 : std::map<std::string, HirId> param_mappings;
797 3318 : for (auto &p : impl_substitutions)
798 : {
799 349 : const std::string &symbol = p.get_param_ty ()->get_symbol ();
800 349 : TyTy::TyVar infer_var
801 349 : = p.get_generic_param ().get_kind ()
802 : == HIR::GenericParam::GenericKind::CONST
803 349 : ? TyTy::TyVar::get_implicit_const_infer_var (locus)
804 343 : : TyTy::TyVar::get_implicit_infer_var (locus);
805 349 : TyTy::BaseType *resolved = infer_var.get_tyty ();
806 349 : infer_arg_vec.emplace_back (&p, resolved);
807 349 : param_mappings[symbol] = resolved->get_ref ();
808 349 : }
809 2969 : TyTy::SubstitutionArgumentMappings infer_arguments (
810 : std::move (infer_arg_vec), {} /*binding_args*/,
811 2969 : TyTy::RegionParamList (0) /*regions*/, locus);
812 :
813 2969 : TyTy::BaseType *impl_self_infer
814 2969 : = !self->is_concrete ()
815 2969 : ? SubstMapperInternal::Resolve (self->clone (), infer_arguments)
816 2748 : : self->clone ();
817 :
818 2969 : const TyTy::TypeBoundPredicate &impl_predicate = predicate;
819 2969 : std::vector<TyTy::BaseType *> impl_predicate_args;
820 4176 : for (size_t i = 1; i < impl_predicate.get_substs ().size (); i++)
821 : {
822 1207 : auto p = impl_predicate.get_substs ().at (i).get_param_ty ();
823 1207 : TyTy::BaseType *r = p->resolve ();
824 1207 : if (!r->is_concrete ())
825 250 : r = SubstMapperInternal::Resolve (r, infer_arguments);
826 1207 : impl_predicate_args.push_back (r);
827 : }
828 :
829 : // Trait args from the bound predicate the caller passed in (skip Self).
830 : // Apply the bound's own used_arguments so any stale impl-coord params
831 : // inside follow through to their current bindings.
832 2969 : TyTy::SubstitutionArgumentMappings bound_used
833 2969 : = bound.get_substitution_arguments ();
834 2969 : std::vector<TyTy::BaseType *> bound_args;
835 4176 : for (size_t i = 1; i < bound.get_substs ().size (); i++)
836 : {
837 1207 : auto p = bound.get_substs ().at (i).get_param_ty ();
838 1207 : TyTy::BaseType *r = p->resolve ();
839 1207 : if (!r->is_concrete () && !bound_used.is_empty ())
840 7 : r = SubstMapperInternal::Resolve (r, bound_used);
841 1207 : bound_args.push_back (r);
842 : }
843 :
844 2969 : if (impl_predicate_args.size () != bound_args.size ())
845 0 : return TyTy::SubstitutionArgumentMappings::error ();
846 :
847 4176 : for (size_t i = 0; i < bound_args.size (); i++)
848 : {
849 2414 : auto *res = unify_site_and (0 /*id*/,
850 1207 : TyTy::TyWithLocation (impl_predicate_args[i]),
851 1207 : TyTy::TyWithLocation (bound_args[i]), locus,
852 : false /*emit_errors*/, true /*commit_if_ok*/,
853 : true /*infer*/, true /*cleanup_on_fail*/,
854 : false /*check_bounds*/);
855 1207 : if (res->get_kind () == TyTy::TypeKind::ERROR)
856 0 : return TyTy::SubstitutionArgumentMappings::error ();
857 : }
858 :
859 2969 : auto *res = unify_site_and (impl->get_mappings ().get_hirid (),
860 2969 : TyTy::TyWithLocation (receiver),
861 2969 : TyTy::TyWithLocation (impl_self_infer), locus,
862 : false /*emit_errors*/, true /*commit_if_ok*/,
863 : true /*infer*/, true /*cleanup_on_fail*/,
864 : false /*check_bounds*/);
865 2969 : if (res->get_kind () == TyTy::TypeKind::ERROR)
866 0 : return TyTy::SubstitutionArgumentMappings::error ();
867 :
868 2969 : std::vector<TyTy::SubstitutionArg> resolved_args;
869 3318 : for (auto &p : impl_substitutions)
870 : {
871 349 : const std::string &symbol = p.get_param_ty ()->get_symbol ();
872 349 : auto it = param_mappings.find (symbol);
873 349 : if (it == param_mappings.end ())
874 0 : continue;
875 349 : TyTy::BaseType *r = nullptr;
876 349 : bool ok = context->lookup_type (it->second, &r);
877 349 : if (!ok)
878 0 : continue;
879 349 : resolved_args.emplace_back (&p, r);
880 349 : }
881 :
882 2969 : if (!validate_impl_substitution_bounds (resolved_args, locus, emit_error))
883 1 : return TyTy::SubstitutionArgumentMappings::error ();
884 :
885 2968 : return TyTy::SubstitutionArgumentMappings (std::move (resolved_args),
886 : {} /*binding_args*/,
887 2968 : TyTy::RegionParamList (0)
888 : /*regions*/,
889 5936 : locus);
890 5938 : }
891 :
892 : location_t
893 0 : AssociatedImplTrait::get_locus () const
894 : {
895 0 : return impl->get_locus ();
896 : }
897 :
898 : Analysis::NodeMapping
899 0 : TraitItemReference::get_parent_trait_mappings () const
900 : {
901 0 : auto &mappings = Analysis::Mappings::get ();
902 :
903 0 : HIR::Trait *trait
904 0 : = mappings.lookup_trait_item_mapping (get_mappings ().get_hirid ());
905 0 : rust_assert (trait != nullptr);
906 :
907 0 : return trait->get_mappings ();
908 : }
909 :
910 : bool
911 4085 : TraitItemReference::is_object_safe () const
912 : {
913 : // https://doc.rust-lang.org/reference/items/traits.html#object-safety
914 4085 : switch (get_trait_item_type ())
915 : {
916 3949 : case TraitItemReference::TraitItemType::FN:
917 3949 : {
918 : // lets be boring and just check that this is indeed a method will do
919 : // for now
920 3949 : const HIR::TraitItem *item = get_hir_trait_item ();
921 3949 : const HIR::TraitItemFunc *fn
922 : = static_cast<const HIR::TraitItemFunc *> (item);
923 3949 : return fn->get_decl ().is_method ();
924 : }
925 :
926 : // constants are not available via dyn dispatch and so is not object safe
927 : case TraitItemReference::TraitItemType::CONST:
928 : return false;
929 :
930 : // types are object safe since they are not available via dyn dispatch
931 134 : case TraitItemReference::TraitItemType::TYPE:
932 134 : return true;
933 :
934 : // this is just an error so lets just fail it
935 : case TraitItemReference::TraitItemType::ERROR:
936 : return false;
937 : }
938 : return false;
939 : }
940 :
941 : } // namespace Resolver
942 : } // namespace Rust
|