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-full-decls.h"
20 : #include "rust-hir-type-bounds.h"
21 : #include "rust-hir-trait-resolve.h"
22 : #include "rust-substitution-mapper.h"
23 : #include "rust-hir-trait-resolve.h"
24 : #include "rust-type-util.h"
25 : #include "rust-tyty.h"
26 :
27 : namespace Rust {
28 : namespace Resolver {
29 :
30 297049 : TypeBoundsProbe::TypeBoundsProbe (TyTy::BaseType *receiver,
31 : const HIR::Trait *specified_trait)
32 297049 : : TypeCheckBase (), receiver (receiver), specified_trait (specified_trait)
33 297049 : {}
34 :
35 : std::vector<std::pair<TraitReference *, HIR::ImplBlock *>>
36 297049 : TypeBoundsProbe::Probe (TyTy::BaseType *receiver,
37 : const HIR::Trait *specified_trait)
38 : {
39 297049 : TypeBoundsProbe probe (receiver, specified_trait);
40 297049 : probe.scan ();
41 297016 : return probe.trait_references;
42 297016 : }
43 :
44 : bool
45 82 : TypeBoundsProbe::is_bound_satisfied_for_type (TyTy::BaseType *receiver,
46 : TraitReference *ref)
47 : {
48 82 : for (auto &bound : receiver->get_specified_bounds ())
49 : {
50 0 : const TraitReference *b = bound.get ();
51 0 : if (b->is_equal (*ref))
52 82 : return true;
53 : }
54 :
55 82 : std::vector<std::pair<TraitReference *, HIR::ImplBlock *>> bounds
56 82 : = Probe (receiver, ref->get_hir_trait_ref ());
57 82 : for (auto &bound : bounds)
58 : {
59 80 : const TraitReference *b = bound.first;
60 80 : if (b->is_equal (*ref))
61 82 : return true;
62 : }
63 :
64 : return false;
65 82 : }
66 :
67 : bool
68 7456596 : TypeBoundsProbe::process_impl_block (
69 : HirId id, HIR::ImplBlock *impl,
70 : std::vector<std::pair<HIR::TypePath *, HIR::ImplBlock *>>
71 : &possible_trait_paths)
72 : {
73 : // we are filtering for trait-impl-blocks
74 7456596 : if (!impl->has_trait_ref ())
75 : return true;
76 :
77 : // can be recursive trait resolution
78 7456596 : HIR::Trait *t = TraitResolver::ResolveHirItem (impl->get_trait_ref ());
79 7456596 : if (t == nullptr)
80 : return true;
81 :
82 7456596 : HirId impl_ty_id = impl->get_type ().get_mappings ().get_hirid ();
83 7456596 : TyTy::BaseType *impl_type = nullptr;
84 7456596 : if (!query_type (impl_ty_id, &impl_type))
85 : return true;
86 :
87 7455551 : if (!types_compatable (TyTy::TyWithLocation (receiver),
88 7455551 : TyTy::TyWithLocation (impl_type), impl->get_locus (),
89 : false /*emit_errors*/, false /*check-bounds*/))
90 : return true;
91 :
92 120257 : possible_trait_paths.emplace_back (&impl->get_trait_ref (), impl);
93 120257 : return true;
94 : }
95 :
96 : void
97 297049 : TypeBoundsProbe::scan ()
98 : {
99 297049 : std::vector<std::pair<HIR::TypePath *, HIR::ImplBlock *>>
100 297049 : possible_trait_paths;
101 7753645 : auto process_impl = [&] (HirId id, HIR::ImplBlock *impl) mutable -> bool {
102 7456596 : return process_impl_block (id, impl, possible_trait_paths);
103 297049 : };
104 :
105 297049 : if (specified_trait == nullptr)
106 491 : mappings.iterate_trait_impl_blocks (process_impl);
107 : else
108 296558 : mappings.iterate_trait_impl_blocks (
109 593094 : specified_trait->get_mappings ().get_defid (), process_impl);
110 :
111 415194 : for (auto &path : possible_trait_paths)
112 : {
113 118178 : HIR::TypePath *trait_path = path.first;
114 118178 : TraitReference *trait_ref = TraitResolver::Resolve (*trait_path);
115 :
116 118178 : if (!trait_ref->is_error ())
117 118178 : trait_references.emplace_back (trait_ref, path.second);
118 : }
119 :
120 : // marker traits...
121 297016 : assemble_marker_builtins ();
122 :
123 : // add auto trait bounds, only the requested one when probing for a trait
124 1063582 : for (auto *auto_trait : mappings.get_auto_traits ())
125 766566 : if (specified_trait == nullptr || specified_trait == auto_trait)
126 172 : add_trait_bound (auto_trait);
127 297016 : }
128 :
129 : void
130 297016 : TypeBoundsProbe::assemble_marker_builtins ()
131 : {
132 297016 : const TyTy::BaseType *raw = receiver->destructure ();
133 :
134 : // https://runrust.miraheze.org/wiki/Dynamically_Sized_Type
135 : // everything is sized except for:
136 : //
137 : // 1. dyn traits
138 : // 2. slices
139 : // 3. str
140 : // 4. ADT's which contain any of the above
141 : // t. tuples which contain any of the above
142 297016 : switch (raw->get_kind ())
143 : {
144 194780 : case TyTy::ARRAY:
145 194780 : case TyTy::REF:
146 194780 : case TyTy::POINTER:
147 194780 : case TyTy::PARAM:
148 194780 : case TyTy::FNDEF:
149 194780 : case TyTy::BOOL:
150 194780 : case TyTy::CHAR:
151 194780 : case TyTy::INT:
152 194780 : case TyTy::UINT:
153 194780 : case TyTy::FLOAT:
154 194780 : case TyTy::USIZE:
155 194780 : case TyTy::ISIZE:
156 194780 : case TyTy::INFER:
157 194780 : case TyTy::NEVER:
158 194780 : case TyTy::PLACEHOLDER:
159 194780 : case TyTy::PROJECTION:
160 194780 : case TyTy::OPAQUE:
161 194780 : assemble_builtin_candidate (LangItem::Kind::SIZED);
162 194780 : break;
163 :
164 340 : case TyTy::FNPTR:
165 340 : case TyTy::CLOSURE:
166 340 : assemble_builtin_candidate (LangItem::Kind::SIZED);
167 340 : assemble_builtin_candidate (LangItem::Kind::FN_ONCE);
168 340 : assemble_builtin_candidate (LangItem::Kind::FN);
169 340 : assemble_builtin_candidate (LangItem::Kind::FN_MUT);
170 340 : break;
171 :
172 : // FIXME str and slice need to be moved and test cases updated
173 8071 : case TyTy::SLICE:
174 8071 : case TyTy::STR:
175 8071 : case TyTy::TUPLE:
176 : // FIXME add extra checks
177 8071 : assemble_builtin_candidate (LangItem::Kind::SIZED);
178 8071 : break;
179 :
180 93557 : case TyTy::ADT:
181 93557 : {
182 93557 : const auto &adt = *static_cast<const TyTy::ADTType *> (raw);
183 93557 : if (adt.get_adt_kind () != TyTy::ADTType::ADTKind::EXTERN)
184 93556 : assemble_builtin_candidate (LangItem::Kind::SIZED);
185 : }
186 : break;
187 :
188 : case TyTy::CONST:
189 : case TyTy::DYNAMIC:
190 : case TyTy::ERROR:
191 : break;
192 : }
193 297016 : }
194 :
195 : void
196 297001 : TypeBoundsProbe::add_trait_bound (HIR::Trait *trait)
197 : {
198 297001 : auto trait_ref = TraitResolver::Resolve (*trait);
199 :
200 415442 : for (const auto &existing : trait_references)
201 118445 : if (existing.first->is_equal (*trait_ref))
202 14 : return;
203 :
204 296997 : if (receiver->get_kind () == TyTy::TypeKind::ADT)
205 : {
206 93539 : TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (receiver);
207 193474 : for (auto &variant : adt->get_variants ())
208 : {
209 198703 : for (auto &field : variant->get_fields ())
210 : {
211 98768 : TyTy::BaseType *field_ty = field->get_field_type ();
212 :
213 : // TODO: A loop guard is needed here to prevent infinite
214 : // recursion, but self-referential types currently crash due to
215 : // issue Rust-GCC/gccrs#4709. Therefore, I avoided adding an
216 : // untested guard for now.
217 :
218 98768 : if (!field_ty->satisfies_bound (
219 98768 : TyTy::TypeBoundPredicate (*trait_ref,
220 : BoundPolarity::RegularBound,
221 98768 : UNDEF_LOCATION),
222 : false))
223 14 : return;
224 : }
225 : }
226 : }
227 :
228 296987 : trait_references.emplace_back (trait_ref, mappings.lookup_builtin_marker ());
229 : }
230 :
231 : void
232 297767 : TypeBoundsProbe::assemble_builtin_candidate (LangItem::Kind lang_item)
233 : {
234 297767 : auto lang_item_defined = mappings.lookup_lang_item (lang_item);
235 297767 : if (!lang_item_defined)
236 938 : return;
237 296829 : DefId &id = lang_item_defined.value ();
238 :
239 296829 : auto defid = mappings.lookup_defid (id);
240 296829 : if (!defid)
241 : return;
242 296829 : auto item = defid.value ();
243 :
244 296829 : rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
245 296829 : HIR::Trait *trait = static_cast<HIR::Trait *> (item);
246 296829 : const TyTy::BaseType *raw = receiver->destructure ();
247 :
248 296829 : add_trait_bound (trait);
249 :
250 296829 : rust_debug ("Added builtin lang_item: %s for %s",
251 : LangItem::ToString (lang_item).c_str (),
252 : raw->get_name ().c_str ());
253 : }
254 :
255 : TraitReference *
256 132977 : TypeCheckBase::resolve_trait_path (HIR::TypePath &path)
257 : {
258 132977 : return TraitResolver::Resolve (path);
259 : }
260 :
261 : TyTy::TypeBoundPredicate
262 192234 : TypeCheckBase::get_predicate_from_bound (
263 : HIR::TypePath &type_path,
264 : tl::optional<std::reference_wrapper<HIR::Type>> associated_self,
265 : BoundPolarity polarity, bool is_qualified_type_path, bool is_super_trait,
266 : bool defer_bindings)
267 : {
268 192234 : TyTy::TypeBoundPredicate lookup = TyTy::TypeBoundPredicate::error ();
269 192234 : bool already_resolved
270 192234 : = context->lookup_predicate (type_path.get_mappings ().get_hirid (),
271 : &lookup);
272 192234 : if (already_resolved)
273 59257 : return lookup;
274 :
275 132977 : TraitReference *trait = resolve_trait_path (type_path);
276 132977 : if (trait->is_error ())
277 9 : return TyTy::TypeBoundPredicate::error ();
278 :
279 132968 : TyTy::TypeBoundPredicate predicate (*trait, polarity, type_path.get_locus ());
280 132968 : HIR::GenericArgs args
281 132968 : = HIR::GenericArgs::create_empty (type_path.get_locus ());
282 :
283 132968 : auto &final_seg = type_path.get_final_segment ();
284 132968 : switch (final_seg.get_type ())
285 : {
286 67446 : case HIR::TypePathSegment::SegmentType::GENERIC:
287 67446 : {
288 67446 : auto &final_generic_seg
289 : = static_cast<HIR::TypePathSegmentGeneric &> (final_seg);
290 67446 : if (final_generic_seg.has_generic_args ())
291 : {
292 67446 : args = final_generic_seg.get_generic_args ();
293 67446 : if (args.get_binding_args ().size () > 0
294 67446 : && associated_self.has_value () && is_qualified_type_path)
295 : {
296 1 : auto &binding_args = args.get_binding_args ();
297 :
298 1 : rich_location r (line_table, args.get_locus ());
299 2 : for (auto it = binding_args.begin (); it != binding_args.end ();
300 1 : it++)
301 : {
302 1 : auto &arg = *it;
303 1 : r.add_fixit_remove (arg.get_locus ());
304 : }
305 1 : rust_error_at (r, ErrorCode::E0229,
306 : "associated type bindings are not allowed here");
307 1 : }
308 : }
309 : }
310 : break;
311 :
312 3244 : case HIR::TypePathSegment::SegmentType::FUNCTION:
313 3244 : {
314 3244 : auto &final_function_seg
315 : = static_cast<HIR::TypePathSegmentFunction &> (final_seg);
316 3244 : auto &fn = final_function_seg.get_function_path ();
317 :
318 : // we need to make implicit generic args which must be an implicit
319 : // Tuple
320 3244 : auto crate_num = mappings.crate.get_current_crate ();
321 3244 : HirId implicit_args_id = mappings.get_next_hir_id ();
322 3244 : Analysis::NodeMapping mapping (crate_num,
323 3244 : final_seg.get_mappings ().get_nodeid (),
324 3244 : implicit_args_id, UNKNOWN_LOCAL_DEFID);
325 :
326 3244 : std::vector<std::unique_ptr<HIR::Type>> params_copy;
327 6786 : for (auto &p : fn.get_params ())
328 : {
329 3542 : params_copy.push_back (p->clone_type ());
330 : }
331 :
332 3244 : std::vector<std::unique_ptr<HIR::Type>> inputs;
333 3244 : inputs.push_back (
334 3244 : std::make_unique<HIR::TupleType> (mapping, std::move (params_copy),
335 3244 : final_seg.get_locus ()));
336 :
337 3244 : HIR::TraitItem *trait_item
338 3244 : = mappings
339 3244 : .lookup_trait_item_lang_item (LangItem::Kind::FN_ONCE_OUTPUT,
340 : final_seg.get_locus ())
341 3244 : .value ();
342 :
343 3244 : std::vector<HIR::GenericArgsBinding> bindings;
344 :
345 3244 : if (fn.has_return_type () && !defer_bindings)
346 : {
347 1598 : TypeCheckType::Resolve (fn.get_return_type ());
348 :
349 1598 : location_t output_locus = fn.get_return_type ().get_locus ();
350 4794 : bindings.emplace_back (Identifier (trait_item->trait_identifier ()),
351 3196 : fn.get_return_type ().clone_type (),
352 : output_locus);
353 : }
354 :
355 6488 : args = HIR::GenericArgs ({} /* lifetimes */,
356 : std::move (inputs) /* type_args*/,
357 : std::move (bindings) /* binding_args*/,
358 6488 : {} /* const_args */, final_seg.get_locus ());
359 3244 : }
360 3244 : break;
361 :
362 : default:
363 : /* nothing to do */
364 : break;
365 : }
366 :
367 132968 : if (associated_self.has_value ())
368 : {
369 130164 : std::vector<std::unique_ptr<HIR::Type>> type_args;
370 130164 : type_args.push_back (std::unique_ptr<HIR::Type> (
371 130164 : associated_self.value ().get ().clone_type ()));
372 197217 : for (auto &arg : args.get_type_args ())
373 : {
374 67053 : type_args.push_back (std::unique_ptr<HIR::Type> (arg->clone_type ()));
375 : }
376 :
377 390492 : args = HIR::GenericArgs (args.get_lifetime_args (), std::move (type_args),
378 130164 : args.get_binding_args (), args.get_const_args (),
379 260328 : args.get_locus ());
380 130164 : }
381 :
382 132968 : if (defer_bindings)
383 6729 : args.get_binding_args ().clear ();
384 :
385 : // we try to apply generic arguments when they are non empty and or when the
386 : // predicate requires them so that we get the relevant Foo expects x number
387 : // arguments but got zero see test case rust/compile/traits12.rs
388 132968 : if (!args.is_empty () || predicate.requires_generic_args ())
389 : {
390 : // this is applying generic arguments to a trait reference
391 131069 : predicate.apply_generic_arguments (&args, associated_self.has_value (),
392 : is_super_trait);
393 : }
394 :
395 132934 : if (!defer_bindings)
396 126205 : context->insert_resolved_predicate (type_path.get_mappings ().get_hirid (),
397 : predicate);
398 :
399 132934 : return predicate;
400 192200 : }
401 :
402 : } // namespace Resolver
403 :
404 : namespace TyTy {
405 :
406 314056 : TypeBoundPredicate::TypeBoundPredicate (
407 : const Resolver::TraitReference &trait_reference, BoundPolarity polarity,
408 : location_t locus)
409 314056 : : SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}),
410 314056 : reference (trait_reference.get_mappings ().get_defid ()), locus (locus),
411 314056 : error_flag (false), polarity (polarity),
412 942168 : super_traits (trait_reference.get_super_traits ())
413 : {
414 314056 : rust_assert (!trait_reference.get_trait_substs ().empty ());
415 :
416 314056 : substitutions.clear ();
417 708689 : for (const auto &p : trait_reference.get_trait_substs ())
418 394633 : substitutions.push_back (p.clone ());
419 :
420 : // we setup a dummy implict self argument
421 314056 : SubstitutionArg placeholder_self (&get_substs ().front (), nullptr);
422 314056 : used_arguments.get_mappings ().push_back (placeholder_self);
423 314056 : }
424 :
425 5850 : TypeBoundPredicate::TypeBoundPredicate (
426 : DefId reference, std::vector<SubstitutionParamMapping> subst,
427 : BoundPolarity polarity, location_t locus)
428 5850 : : SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}),
429 5850 : reference (reference), locus (locus), error_flag (false),
430 11700 : polarity (polarity)
431 : {
432 5850 : rust_assert (!subst.empty ());
433 :
434 5850 : substitutions.clear ();
435 13035 : for (const auto &p : subst)
436 7185 : substitutions.push_back (p.clone ());
437 :
438 : // we setup a dummy implict self argument
439 5850 : SubstitutionArg placeholder_self (&get_substs ().front (), nullptr);
440 5850 : used_arguments.get_mappings ().push_back (placeholder_self);
441 5850 : }
442 :
443 336445 : TypeBoundPredicate::TypeBoundPredicate (mark_is_error)
444 336445 : : SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}),
445 336445 : reference (UNKNOWN_DEFID), locus (UNDEF_LOCATION), error_flag (true),
446 336445 : polarity (BoundPolarity::RegularBound)
447 336445 : {}
448 :
449 10545817 : TypeBoundPredicate::TypeBoundPredicate (const TypeBoundPredicate &other)
450 10545817 : : SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}),
451 10545817 : reference (other.reference), locus (other.locus),
452 10545817 : error_flag (other.error_flag), polarity (other.polarity),
453 21091634 : super_traits (other.super_traits)
454 : {
455 10545817 : substitutions.clear ();
456 23053603 : for (const auto &p : other.get_substs ())
457 12507786 : substitutions.push_back (p.clone ());
458 :
459 : // we need to remap the argument mappings based on this copied constructor
460 10545817 : std::vector<SubstitutionArg> copied_arg_mappings;
461 10545817 : size_t i = 0;
462 21984667 : for (const auto &m : other.used_arguments.get_mappings ())
463 : {
464 11438850 : TyTy::BaseType *argument
465 11438850 : = m.get_tyty () == nullptr ? nullptr : m.get_tyty ()->clone ();
466 11438850 : SubstitutionArg c (&substitutions.at (i++), argument);
467 11438850 : copied_arg_mappings.push_back (std::move (c));
468 : }
469 :
470 10545817 : used_arguments = SubstitutionArgumentMappings (
471 : copied_arg_mappings, other.used_arguments.get_binding_args (),
472 : other.used_arguments.get_regions (), other.used_arguments.get_locus (),
473 21091634 : false, false, other.used_arguments.get_constraint_args ());
474 10545817 : }
475 :
476 : TypeBoundPredicate &
477 194242 : TypeBoundPredicate::operator= (const TypeBoundPredicate &other)
478 : {
479 194242 : reference = other.reference;
480 194242 : locus = other.locus;
481 194242 : error_flag = other.error_flag;
482 194242 : polarity = other.polarity;
483 194242 : used_arguments = SubstitutionArgumentMappings::empty ();
484 :
485 194242 : substitutions.clear ();
486 503212 : for (const auto &p : other.get_substs ())
487 308970 : substitutions.push_back (p.clone ());
488 :
489 194242 : if (other.is_error ())
490 : return *this;
491 :
492 : // we need to remap the argument mappings based on this copied constructor
493 181127 : std::vector<SubstitutionArg> copied_arg_mappings;
494 181127 : size_t i = 0;
495 490080 : for (const auto &m : other.used_arguments.get_mappings ())
496 : {
497 308953 : TyTy::BaseType *argument
498 308953 : = m.get_tyty () == nullptr ? nullptr : m.get_tyty ()->clone ();
499 :
500 308953 : copied_arg_mappings.emplace_back (&substitutions.at (i++), argument);
501 : }
502 :
503 181127 : used_arguments = SubstitutionArgumentMappings (
504 : copied_arg_mappings, other.used_arguments.get_binding_args (),
505 : other.used_arguments.get_regions (), other.used_arguments.get_locus (),
506 362254 : false, false, other.used_arguments.get_constraint_args ());
507 181127 : super_traits = other.super_traits;
508 :
509 181127 : return *this;
510 181127 : }
511 :
512 : TypeBoundPredicate
513 336445 : TypeBoundPredicate::error ()
514 : {
515 336445 : return TypeBoundPredicate (mark_is_error ());
516 : }
517 :
518 : std::string
519 227 : TypeBoundPredicate::as_string () const
520 : {
521 227 : return get ()->as_string () + subst_as_string ();
522 : }
523 :
524 : std::string
525 913 : TypeBoundPredicate::as_name () const
526 : {
527 913 : return get ()->get_name () + subst_as_string ();
528 : }
529 :
530 : const Resolver::TraitReference *
531 848568 : TypeBoundPredicate::get () const
532 : {
533 848568 : auto context = Resolver::TypeCheckContext::get ();
534 :
535 848568 : Resolver::TraitReference *ref = nullptr;
536 848568 : bool ok = context->lookup_trait_reference (reference, &ref);
537 848568 : rust_assert (ok);
538 :
539 848568 : return ref;
540 : }
541 :
542 : std::string
543 7427 : TypeBoundPredicate::get_name () const
544 : {
545 7427 : return get ()->get_name ();
546 : }
547 :
548 : bool
549 1089 : TypeBoundPredicate::is_object_safe (bool emit_error, location_t locus) const
550 : {
551 1089 : const Resolver::TraitReference *trait = get ();
552 1089 : rust_assert (trait != nullptr);
553 1089 : return trait->is_object_safe (emit_error, locus);
554 : }
555 :
556 : void
557 131135 : TypeBoundPredicate::apply_generic_arguments (HIR::GenericArgs *generic_args,
558 : bool has_associated_self,
559 : bool is_super_trait)
560 : {
561 131135 : rust_assert (!substitutions.empty ());
562 131135 : if (has_associated_self)
563 : {
564 130164 : used_arguments = SubstitutionArgumentMappings::empty ();
565 : }
566 : else
567 : {
568 : // we need to get the substitutions argument mappings but also remember
569 : // that we have an implicit Self argument which we must be careful to
570 : // respect
571 971 : rust_assert (!used_arguments.is_empty ());
572 : }
573 :
574 : // now actually perform a substitution
575 131135 : auto args = get_mappings_from_generic_args (
576 : *generic_args,
577 131135 : Resolver::TypeCheckContext::get ()->regions_from_generic_args (
578 262270 : *generic_args));
579 :
580 131101 : apply_argument_mappings (args, is_super_trait);
581 131101 : }
582 :
583 : void
584 183120 : TypeBoundPredicate::apply_argument_mappings (
585 : SubstitutionArgumentMappings &arguments, bool is_super_trait)
586 : {
587 183120 : used_arguments = arguments;
588 183120 : error_flag |= used_arguments.is_error ();
589 183120 : auto &subst_mappings = used_arguments;
590 :
591 183120 : bool substs_need_bounds_check = !is_super_trait;
592 462114 : for (auto &sub : get_substs ())
593 : {
594 278994 : SubstitutionArg arg = SubstitutionArg::error ();
595 278994 : bool ok
596 278994 : = subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg);
597 278994 : if (ok && arg.get_tyty () != nullptr)
598 276900 : sub.fill_param_ty (subst_mappings, subst_mappings.get_locus (),
599 : substs_need_bounds_check);
600 : }
601 :
602 : // Associated type binding args (Iterator<Item = i32>) are consumed
603 : // by BaseType::satisfies_bound at check time
604 234322 : for (auto &super_trait : super_traits)
605 : {
606 51202 : auto adjusted
607 : = super_trait.adjust_mappings_for_this (used_arguments,
608 51202 : true /*trait mode*/);
609 51202 : super_trait.apply_argument_mappings (adjusted, is_super_trait);
610 51202 : }
611 183120 : }
612 :
613 : bool
614 0 : TypeBoundPredicate::contains_item (const std::string &search) const
615 : {
616 0 : auto trait_ref = get ();
617 0 : const Resolver::TraitItemReference *trait_item_ref = nullptr;
618 0 : return trait_ref->lookup_trait_item (search, &trait_item_ref);
619 : }
620 :
621 : tl::optional<TypeBoundPredicateItem>
622 81211 : TypeBoundPredicate::lookup_associated_item (const std::string &search) const
623 : {
624 81211 : auto trait_ref = get ();
625 81211 : const Resolver::TraitItemReference *trait_item_ref = nullptr;
626 81211 : if (trait_ref->lookup_trait_item (search, &trait_item_ref,
627 : false /*lookup supers*/))
628 72737 : return TypeBoundPredicateItem (this, trait_item_ref);
629 :
630 8689 : for (auto &super_trait : super_traits)
631 : {
632 3277 : auto lookup = super_trait.lookup_associated_item (search);
633 3277 : if (lookup.has_value ())
634 3062 : return lookup;
635 : }
636 :
637 5412 : return tl::nullopt;
638 : }
639 :
640 102556 : TypeBoundPredicateItem::TypeBoundPredicateItem (
641 : const TypeBoundPredicate *parent,
642 : const Resolver::TraitItemReference *trait_item_ref)
643 102556 : : parent (parent), trait_item_ref (trait_item_ref)
644 102556 : {}
645 :
646 : TypeBoundPredicateItem
647 29746 : TypeBoundPredicateItem::error ()
648 : {
649 29746 : return TypeBoundPredicateItem (nullptr, nullptr);
650 : }
651 :
652 : bool
653 32793 : TypeBoundPredicateItem::is_error () const
654 : {
655 32793 : return parent == nullptr || parent->is_error () || trait_item_ref == nullptr;
656 : }
657 :
658 : const TypeBoundPredicate *
659 7130 : TypeBoundPredicateItem::get_parent () const
660 : {
661 7130 : return parent;
662 : }
663 :
664 : tl::optional<TypeBoundPredicateItem>
665 29570 : TypeBoundPredicate::lookup_associated_item (
666 : const Resolver::TraitItemReference *ref) const
667 : {
668 29570 : return lookup_associated_item (ref->get_identifier ());
669 : }
670 :
671 : BaseType *
672 59545 : TypeBoundPredicateItem::get_tyty_for_receiver (const TyTy::BaseType *receiver)
673 : {
674 59545 : auto ctx = Resolver::TypeCheckContext::get ();
675 :
676 59545 : TyTy::BaseType *trait_item_tyty = get_raw_item ()->get_tyty ();
677 59545 : if (parent->get_substitution_arguments ().is_empty ())
678 : return trait_item_tyty;
679 :
680 : // set up the self mapping
681 59545 : SubstitutionArgumentMappings gargs = parent->get_substitution_arguments ();
682 59545 : rust_assert (!gargs.is_empty ());
683 :
684 : // The associated-type projection we are rebasing is stored in trait
685 : // coordinates
686 : //
687 : // Self/X for trait SliceIndex<X>
688 : //
689 : // The predicate's own SubstitutionParamMappings may have already been
690 : // mutated by SubstitutionParamMapping::fill_param_ty when the bound is a
691 : // where-clause like I: SliceIndex<[T]>: substituting Self with the
692 : // ParamType I rebinds the predicate's first param from Self to I.
693 : // Building adjusted_mappings from those renamed mappings would make
694 : // name-based lookup (get_argument_for_symbol) miss the trait's Self/X
695 : // symbols inside the projection.
696 59545 : const auto &trait_substs = parent->get ()->get_trait_substs ();
697 59545 : rust_assert (gargs.get_mappings ().size () <= trait_substs.size ());
698 :
699 59545 : std::vector<SubstitutionArg> adjusted_mappings;
700 169075 : for (size_t i = 0; i < gargs.get_mappings ().size (); i++)
701 : {
702 109530 : auto &mapping = gargs.get_mappings ().at (i);
703 :
704 109530 : bool is_implicit_self = i == 0;
705 109530 : TyTy::BaseType *argument
706 109530 : = is_implicit_self ? receiver->clone () : mapping.get_tyty ();
707 :
708 109530 : adjusted_mappings.emplace_back (&trait_substs.at (i), argument);
709 : }
710 :
711 59545 : SubstitutionArgumentMappings adjusted (adjusted_mappings, {},
712 : gargs.get_regions (),
713 : gargs.get_locus (),
714 59545 : true /* trait-mode-flag */);
715 59545 : TyTy::BaseType *res
716 59545 : = Resolver::SubstMapperInternal::Resolve (trait_item_tyty, adjusted);
717 :
718 59545 : if (res != trait_item_tyty)
719 : {
720 59545 : auto &mappings = Analysis::Mappings::get ();
721 59545 : HirId fresh = mappings.get_next_hir_id ();
722 59545 : res->set_ref (fresh);
723 59545 : res->set_ty_ref (fresh);
724 59545 : ctx->insert_implicit_type (fresh, res);
725 : }
726 :
727 59545 : return res;
728 59545 : }
729 : bool
730 434478 : TypeBoundPredicate::is_error () const
731 : {
732 434478 : auto context = Resolver::TypeCheckContext::get ();
733 :
734 434478 : Resolver::TraitReference *ref = nullptr;
735 434478 : bool ok = context->lookup_trait_reference (reference, &ref);
736 :
737 434478 : return !ok || error_flag;
738 : }
739 :
740 : BaseType *
741 375277 : TypeBoundPredicate::handle_substitions (
742 : SubstitutionArgumentMappings &subst_mappings)
743 : {
744 900377 : for (auto &sub : get_substs ())
745 : {
746 525100 : if (sub.get_param_ty () == nullptr)
747 0 : continue;
748 :
749 525100 : auto p = sub.get_param_ty ();
750 525100 : BaseType *r = p->resolve ();
751 525100 : BaseType *s = Resolver::SubstMapperInternal::Resolve (r, subst_mappings);
752 :
753 525100 : p->set_ty_ref (s->get_ty_ref ());
754 : }
755 :
756 : // FIXME more error handling at some point
757 : // used_arguments = subst_mappings;
758 : // error_flag |= used_arguments.is_error ();
759 :
760 375277 : return nullptr;
761 : }
762 :
763 : bool
764 1959 : TypeBoundPredicate::requires_generic_args () const
765 : {
766 1959 : if (is_error ())
767 : return false;
768 :
769 1959 : return substitutions.size () > 1;
770 : }
771 :
772 : bool
773 0 : TypeBoundPredicate::contains_associated_types () const
774 : {
775 0 : return get_num_associated_bindings () > 0;
776 : }
777 :
778 : size_t
779 6178 : TypeBoundPredicate::get_num_associated_bindings () const
780 : {
781 6178 : size_t count = 0;
782 :
783 6178 : get_trait_hierachy ([&count] (const Resolver::TraitReference &ref) {
784 117828 : for (const auto &trait_item : ref.get_trait_items ())
785 : {
786 102674 : bool is_associated_type
787 102674 : = trait_item.get_trait_item_type ()
788 102674 : == Resolver::TraitItemReference::TraitItemType::TYPE;
789 102674 : if (is_associated_type)
790 10006 : count++;
791 : }
792 15154 : });
793 :
794 6178 : return count;
795 : }
796 :
797 : void
798 10666 : TypeBoundPredicate::get_trait_hierachy (
799 : std::function<void (const Resolver::TraitReference &)> callback) const
800 : {
801 10666 : auto trait_ref = get ();
802 10666 : callback (*trait_ref);
803 :
804 15154 : for (auto &super : super_traits)
805 : {
806 4488 : const auto &super_trait_ref = *super.get ();
807 4488 : callback (super_trait_ref);
808 4488 : super.get_trait_hierachy (callback);
809 : }
810 10666 : }
811 :
812 : TypeBoundPredicateItem
813 3168 : TypeBoundPredicate::lookup_associated_type (const std::string &search)
814 : {
815 3168 : tl::optional<TypeBoundPredicateItem> item = lookup_associated_item (search);
816 :
817 : // only need to check that it is infact an associated type because other
818 : // wise if it was not found it will just be an error node anyway
819 3168 : if (item.has_value ())
820 : {
821 3168 : const auto raw = item->get_raw_item ();
822 3168 : if (raw->get_trait_item_type ()
823 : != Resolver::TraitItemReference::TraitItemType::TYPE)
824 0 : return TypeBoundPredicateItem::error ();
825 : }
826 3168 : return item.value ();
827 : }
828 :
829 : std::vector<TypeBoundPredicateItem>
830 0 : TypeBoundPredicate::get_associated_type_items ()
831 : {
832 0 : std::vector<TypeBoundPredicateItem> items;
833 0 : auto trait_ref = get ();
834 0 : for (const auto &trait_item : trait_ref->get_trait_items ())
835 : {
836 0 : bool is_associated_type
837 0 : = trait_item.get_trait_item_type ()
838 0 : == Resolver::TraitItemReference::TraitItemType::TYPE;
839 0 : if (is_associated_type)
840 0 : items.emplace_back (this, &trait_item);
841 : }
842 0 : return items;
843 : }
844 :
845 : bool
846 1600 : TypeBoundPredicate::is_equal (const TypeBoundPredicate &other) const
847 : {
848 : // check they match the same trait reference
849 1600 : if (reference != other.reference)
850 : return false;
851 :
852 : // check that the generics match
853 1582 : if (get_num_substitutions () != other.get_num_substitutions ())
854 : return false;
855 :
856 : // then match the generics applied
857 3318 : for (size_t i = 0; i < get_num_substitutions (); i++)
858 : {
859 1736 : SubstitutionParamMapping a = substitutions.at (i);
860 1736 : SubstitutionParamMapping b = other.substitutions.at (i);
861 :
862 1736 : auto ap = a.get_param_ty ();
863 1736 : auto bp = b.get_param_ty ();
864 :
865 1736 : BaseType *apd = ap->destructure ();
866 1736 : BaseType *bpd = bp->destructure ();
867 :
868 1736 : if (!Resolver::types_compatable (TyTy::TyWithLocation (apd),
869 1736 : TyTy::TyWithLocation (bpd),
870 : UNKNOWN_LOCATION, false))
871 0 : return false;
872 : }
873 :
874 : return true;
875 : }
876 :
877 : bool
878 15322 : TypeBoundPredicate::validate_type_implements_super_traits (
879 : TyTy::BaseType &self, HIR::Type &impl_type, HIR::Type &trait) const
880 : {
881 15322 : if (get_polarity () != BoundPolarity::RegularBound)
882 : return true;
883 :
884 15314 : auto &ptref = *get ();
885 21747 : for (auto &super : super_traits)
886 : {
887 6434 : if (super.get_polarity () != BoundPolarity::RegularBound)
888 0 : continue;
889 :
890 6434 : if (!super.validate_type_implements_this (self, impl_type, trait))
891 : {
892 1 : auto &sptref = *super.get ();
893 :
894 : // emit error
895 1 : std::string fixit1
896 1 : = "required by this bound in: " + ptref.get_name ();
897 2 : std::string fixit2 = "the trait " + sptref.get_name ()
898 2 : + " is not implemented for "
899 2 : + impl_type.to_string ();
900 :
901 1 : rich_location r (line_table, trait.get_locus ());
902 1 : r.add_fixit_insert_after (super.get_locus (), fixit1.c_str ());
903 1 : r.add_fixit_insert_after (trait.get_locus (), fixit2.c_str ());
904 1 : rust_error_at (r, ErrorCode::E0277,
905 : "the trait bound %<%s: %s%> is not satisfied",
906 2 : impl_type.to_string ().c_str (),
907 1 : sptref.get_name ().c_str ());
908 :
909 1 : return false;
910 1 : }
911 :
912 6433 : if (!super.validate_type_implements_super_traits (self, impl_type, trait))
913 : return false;
914 : }
915 :
916 : return true;
917 : }
918 :
919 : bool
920 6434 : TypeBoundPredicate::validate_type_implements_this (TyTy::BaseType &self,
921 : HIR::Type &impl_type,
922 : HIR::Type &trait) const
923 : {
924 6434 : const auto &ptref = *get ();
925 6434 : auto probed_bounds
926 6434 : = Resolver::TypeBoundsProbe::Probe (&self, ptref.get_hir_trait_ref ());
927 6434 : for (auto &elem : probed_bounds)
928 : {
929 6433 : auto &tref = *(elem.first);
930 6433 : if (ptref.is_equal (tref))
931 6434 : return true;
932 : }
933 :
934 : return false;
935 6434 : }
936 :
937 : // trait item reference
938 :
939 : const Resolver::TraitItemReference *
940 136436 : TypeBoundPredicateItem::get_raw_item () const
941 : {
942 136436 : return trait_item_ref;
943 : }
944 :
945 : bool
946 0 : TypeBoundPredicateItem::needs_implementation () const
947 : {
948 0 : return !get_raw_item ()->is_optional ();
949 : }
950 :
951 : location_t
952 5 : TypeBoundPredicateItem::get_locus () const
953 : {
954 5 : return get_raw_item ()->get_locus ();
955 : }
956 :
957 : // TypeBoundsMappings
958 :
959 22017665 : TypeBoundsMappings::TypeBoundsMappings (
960 : std::vector<TypeBoundPredicate> specified_bounds)
961 22017665 : : specified_bounds (std::move (specified_bounds))
962 22017665 : {}
963 :
964 : std::vector<TypeBoundPredicate> &
965 1007035 : TypeBoundsMappings::get_specified_bounds ()
966 : {
967 1007035 : return specified_bounds;
968 : }
969 :
970 : const std::vector<TypeBoundPredicate> &
971 10538161 : TypeBoundsMappings::get_specified_bounds () const
972 : {
973 10538161 : return specified_bounds;
974 : }
975 :
976 : TypeBoundPredicate
977 0 : TypeBoundsMappings::lookup_predicate (DefId id)
978 : {
979 0 : for (auto &b : specified_bounds)
980 : {
981 0 : if (b.get_id () == id)
982 0 : return b;
983 : }
984 0 : return TypeBoundPredicate::error ();
985 : }
986 :
987 : size_t
988 3463744 : TypeBoundsMappings::num_specified_bounds () const
989 : {
990 3463744 : return specified_bounds.size ();
991 : }
992 :
993 : std::string
994 38836 : TypeBoundsMappings::raw_bounds_as_string () const
995 : {
996 38836 : std::string buf;
997 39063 : for (size_t i = 0; i < specified_bounds.size (); i++)
998 : {
999 227 : const TypeBoundPredicate &b = specified_bounds.at (i);
1000 227 : bool has_next = (i + 1) < specified_bounds.size ();
1001 904 : buf += b.as_string () + (has_next ? " + " : "");
1002 : }
1003 38836 : return buf;
1004 : }
1005 :
1006 : std::string
1007 38673 : TypeBoundsMappings::bounds_as_string () const
1008 : {
1009 77346 : return "bounds:[" + raw_bounds_as_string () + "]";
1010 : }
1011 :
1012 : std::string
1013 902 : TypeBoundsMappings::raw_bounds_as_name () const
1014 : {
1015 902 : std::string buf;
1016 1815 : for (size_t i = 0; i < specified_bounds.size (); i++)
1017 : {
1018 913 : const TypeBoundPredicate &b = specified_bounds.at (i);
1019 913 : bool has_next = (i + 1) < specified_bounds.size ();
1020 3641 : buf += b.as_name () + (has_next ? " + " : "");
1021 : }
1022 :
1023 902 : return buf;
1024 : }
1025 :
1026 : void
1027 323127 : TypeBoundsMappings::add_bound (const TypeBoundPredicate &predicate)
1028 : {
1029 362093 : for (auto &bound : specified_bounds)
1030 : {
1031 70391 : bool same_trait_ref_p = bound.get_id () == predicate.get_id ();
1032 38966 : if (same_trait_ref_p)
1033 323127 : return;
1034 : }
1035 :
1036 291702 : specified_bounds.push_back (predicate);
1037 : }
1038 :
1039 : } // namespace TyTy
1040 : } // namespace Rust
|