]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/mod.rs
Rollup merge of #97857 - ChayimFriedman2:box-identifier-help, r=compiler-errors
[rust.git] / compiler / rustc_trait_selection / src / traits / mod.rs
1 //! Trait Resolution. See the [rustc dev guide] for more information on how this works.
2 //!
3 //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html
4
5 pub mod auto_trait;
6 mod chalk_fulfill;
7 pub mod codegen;
8 mod coherence;
9 pub mod const_evaluatable;
10 mod engine;
11 pub mod error_reporting;
12 mod fulfill;
13 pub mod misc;
14 mod object_safety;
15 mod on_unimplemented;
16 mod project;
17 pub mod query;
18 pub(crate) mod relationships;
19 mod select;
20 mod specialize;
21 mod structural_match;
22 mod util;
23 pub mod wf;
24
25 use crate::infer::outlives::env::OutlivesEnvironment;
26 use crate::infer::{InferCtxt, RegionckMode, TyCtxtInferExt};
27 use crate::traits::error_reporting::InferCtxtExt as _;
28 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
29 use rustc_errors::ErrorGuaranteed;
30 use rustc_hir as hir;
31 use rustc_hir::def_id::DefId;
32 use rustc_hir::lang_items::LangItem;
33 use rustc_middle::ty::fold::TypeFoldable;
34 use rustc_middle::ty::subst::{InternalSubsts, SubstsRef};
35 use rustc_middle::ty::{self, GenericParamDefKind, ToPredicate, Ty, TyCtxt, VtblEntry};
36 use rustc_span::{sym, Span};
37 use smallvec::SmallVec;
38
39 use std::fmt::Debug;
40 use std::ops::ControlFlow;
41
42 pub use self::FulfillmentErrorCode::*;
43 pub use self::ImplSource::*;
44 pub use self::ObligationCauseCode::*;
45 pub use self::SelectionError::*;
46
47 pub use self::coherence::{add_placeholder_note, orphan_check, overlapping_impls};
48 pub use self::coherence::{OrphanCheckErr, OverlapResult};
49 pub use self::engine::TraitEngineExt;
50 pub use self::fulfill::{FulfillmentContext, PendingPredicateObligation};
51 pub use self::object_safety::astconv_object_safety_violations;
52 pub use self::object_safety::is_vtable_safe_method;
53 pub use self::object_safety::MethodViolationCode;
54 pub use self::object_safety::ObjectSafetyViolation;
55 pub use self::on_unimplemented::{OnUnimplementedDirective, OnUnimplementedNote};
56 pub use self::project::{normalize, normalize_projection_type, normalize_to};
57 pub use self::select::{EvaluationCache, SelectionCache, SelectionContext};
58 pub use self::select::{EvaluationResult, IntercrateAmbiguityCause, OverflowError};
59 pub use self::specialize::specialization_graph::FutureCompatOverlapError;
60 pub use self::specialize::specialization_graph::FutureCompatOverlapErrorKind;
61 pub use self::specialize::{specialization_graph, translate_substs, OverlapError};
62 pub use self::structural_match::search_for_structural_match_violation;
63 pub use self::structural_match::{NonStructuralMatchTy, NonStructuralMatchTyKind};
64 pub use self::util::{
65     elaborate_obligations, elaborate_predicates, elaborate_predicates_with_span,
66     elaborate_trait_ref, elaborate_trait_refs,
67 };
68 pub use self::util::{expand_trait_aliases, TraitAliasExpander};
69 pub use self::util::{
70     get_vtable_index_of_object_method, impl_item_is_final, predicate_for_trait_def, upcast_choices,
71 };
72 pub use self::util::{
73     supertrait_def_ids, supertraits, transitive_bounds, transitive_bounds_that_define_assoc_type,
74     SupertraitDefIds, Supertraits,
75 };
76
77 pub use self::chalk_fulfill::FulfillmentContext as ChalkFulfillmentContext;
78
79 pub use rustc_infer::traits::*;
80
81 /// Whether to skip the leak check, as part of a future compatibility warning step.
82 ///
83 /// The "default" for skip-leak-check corresponds to the current
84 /// behavior (do not skip the leak check) -- not the behavior we are
85 /// transitioning into.
86 #[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
87 pub enum SkipLeakCheck {
88     Yes,
89     #[default]
90     No,
91 }
92
93 impl SkipLeakCheck {
94     fn is_yes(self) -> bool {
95         self == SkipLeakCheck::Yes
96     }
97 }
98
99 /// The mode that trait queries run in.
100 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
101 pub enum TraitQueryMode {
102     /// Standard/un-canonicalized queries get accurate
103     /// spans etc. passed in and hence can do reasonable
104     /// error reporting on their own.
105     Standard,
106     /// Canonicalized queries get dummy spans and hence
107     /// must generally propagate errors to
108     /// pre-canonicalization callsites.
109     Canonical,
110 }
111
112 /// Creates predicate obligations from the generic bounds.
113 pub fn predicates_for_generics<'tcx>(
114     cause: ObligationCause<'tcx>,
115     param_env: ty::ParamEnv<'tcx>,
116     generic_bounds: ty::InstantiatedPredicates<'tcx>,
117 ) -> impl Iterator<Item = PredicateObligation<'tcx>> {
118     util::predicates_for_generics(cause, 0, param_env, generic_bounds)
119 }
120
121 /// Determines whether the type `ty` is known to meet `bound` and
122 /// returns true if so. Returns false if `ty` either does not meet
123 /// `bound` or is not known to meet bound (note that this is
124 /// conservative towards *no impl*, which is the opposite of the
125 /// `evaluate` methods).
126 pub fn type_known_to_meet_bound_modulo_regions<'a, 'tcx>(
127     infcx: &InferCtxt<'a, 'tcx>,
128     param_env: ty::ParamEnv<'tcx>,
129     ty: Ty<'tcx>,
130     def_id: DefId,
131     span: Span,
132 ) -> bool {
133     debug!(
134         "type_known_to_meet_bound_modulo_regions(ty={:?}, bound={:?})",
135         ty,
136         infcx.tcx.def_path_str(def_id)
137     );
138
139     let trait_ref =
140         ty::Binder::dummy(ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) });
141     let obligation = Obligation {
142         param_env,
143         cause: ObligationCause::misc(span, hir::CRATE_HIR_ID),
144         recursion_depth: 0,
145         predicate: trait_ref.without_const().to_predicate(infcx.tcx),
146     };
147
148     let result = infcx.predicate_must_hold_modulo_regions(&obligation);
149     debug!(
150         "type_known_to_meet_ty={:?} bound={} => {:?}",
151         ty,
152         infcx.tcx.def_path_str(def_id),
153         result
154     );
155
156     if result && ty.has_infer_types_or_consts() {
157         // Because of inference "guessing", selection can sometimes claim
158         // to succeed while the success requires a guess. To ensure
159         // this function's result remains infallible, we must confirm
160         // that guess. While imperfect, I believe this is sound.
161
162         // The handling of regions in this area of the code is terrible,
163         // see issue #29149. We should be able to improve on this with
164         // NLL.
165         let mut fulfill_cx = FulfillmentContext::new_ignoring_regions();
166
167         // We can use a dummy node-id here because we won't pay any mind
168         // to region obligations that arise (there shouldn't really be any
169         // anyhow).
170         let cause = ObligationCause::misc(span, hir::CRATE_HIR_ID);
171
172         fulfill_cx.register_bound(infcx, param_env, ty, def_id, cause);
173
174         // Note: we only assume something is `Copy` if we can
175         // *definitively* show that it implements `Copy`. Otherwise,
176         // assume it is move; linear is always ok.
177         match fulfill_cx.select_all_or_error(infcx).as_slice() {
178             [] => {
179                 debug!(
180                     "type_known_to_meet_bound_modulo_regions: ty={:?} bound={} success",
181                     ty,
182                     infcx.tcx.def_path_str(def_id)
183                 );
184                 true
185             }
186             errors => {
187                 debug!(
188                     ?ty,
189                     bound = %infcx.tcx.def_path_str(def_id),
190                     ?errors,
191                     "type_known_to_meet_bound_modulo_regions"
192                 );
193                 false
194             }
195         }
196     } else {
197         result
198     }
199 }
200
201 fn do_normalize_predicates<'tcx>(
202     tcx: TyCtxt<'tcx>,
203     region_context: DefId,
204     cause: ObligationCause<'tcx>,
205     elaborated_env: ty::ParamEnv<'tcx>,
206     predicates: Vec<ty::Predicate<'tcx>>,
207 ) -> Result<Vec<ty::Predicate<'tcx>>, ErrorGuaranteed> {
208     debug!(
209         "do_normalize_predicates(predicates={:?}, region_context={:?}, cause={:?})",
210         predicates, region_context, cause,
211     );
212     let span = cause.span;
213     tcx.infer_ctxt().enter(|infcx| {
214         // FIXME. We should really... do something with these region
215         // obligations. But this call just continues the older
216         // behavior (i.e., doesn't cause any new bugs), and it would
217         // take some further refactoring to actually solve them. In
218         // particular, we would have to handle implied bounds
219         // properly, and that code is currently largely confined to
220         // regionck (though I made some efforts to extract it
221         // out). -nmatsakis
222         //
223         // @arielby: In any case, these obligations are checked
224         // by wfcheck anyway, so I'm not sure we have to check
225         // them here too, and we will remove this function when
226         // we move over to lazy normalization *anyway*.
227         let fulfill_cx = FulfillmentContext::new_ignoring_regions();
228         let predicates =
229             match fully_normalize(&infcx, fulfill_cx, cause, elaborated_env, predicates) {
230                 Ok(predicates) => predicates,
231                 Err(errors) => {
232                     let reported = infcx.report_fulfillment_errors(&errors, None, false);
233                     return Err(reported);
234                 }
235             };
236
237         debug!("do_normalize_predictes: normalized predicates = {:?}", predicates);
238
239         // We can use the `elaborated_env` here; the region code only
240         // cares about declarations like `'a: 'b`.
241         let outlives_env = OutlivesEnvironment::new(elaborated_env);
242
243         infcx.resolve_regions_and_report_errors(
244             region_context,
245             &outlives_env,
246             RegionckMode::default(),
247         );
248
249         let predicates = match infcx.fully_resolve(predicates) {
250             Ok(predicates) => predicates,
251             Err(fixup_err) => {
252                 // If we encounter a fixup error, it means that some type
253                 // variable wound up unconstrained. I actually don't know
254                 // if this can happen, and I certainly don't expect it to
255                 // happen often, but if it did happen it probably
256                 // represents a legitimate failure due to some kind of
257                 // unconstrained variable, and it seems better not to ICE,
258                 // all things considered.
259                 let reported = tcx.sess.span_err(span, &fixup_err.to_string());
260                 return Err(reported);
261             }
262         };
263         if predicates.needs_infer() {
264             let reported = tcx
265                 .sess
266                 .delay_span_bug(span, "encountered inference variables after `fully_resolve`");
267             Err(reported)
268         } else {
269             Ok(predicates)
270         }
271     })
272 }
273
274 // FIXME: this is gonna need to be removed ...
275 /// Normalizes the parameter environment, reporting errors if they occur.
276 pub fn normalize_param_env_or_error<'tcx>(
277     tcx: TyCtxt<'tcx>,
278     region_context: DefId,
279     unnormalized_env: ty::ParamEnv<'tcx>,
280     cause: ObligationCause<'tcx>,
281 ) -> ty::ParamEnv<'tcx> {
282     // I'm not wild about reporting errors here; I'd prefer to
283     // have the errors get reported at a defined place (e.g.,
284     // during typeck). Instead I have all parameter
285     // environments, in effect, going through this function
286     // and hence potentially reporting errors. This ensures of
287     // course that we never forget to normalize (the
288     // alternative seemed like it would involve a lot of
289     // manual invocations of this fn -- and then we'd have to
290     // deal with the errors at each of those sites).
291     //
292     // In any case, in practice, typeck constructs all the
293     // parameter environments once for every fn as it goes,
294     // and errors will get reported then; so outside of type inference we
295     // can be sure that no errors should occur.
296
297     debug!(
298         "normalize_param_env_or_error(region_context={:?}, unnormalized_env={:?}, cause={:?})",
299         region_context, unnormalized_env, cause
300     );
301
302     let mut predicates: Vec<_> =
303         util::elaborate_predicates(tcx, unnormalized_env.caller_bounds().into_iter())
304             .map(|obligation| obligation.predicate)
305             .collect();
306
307     debug!("normalize_param_env_or_error: elaborated-predicates={:?}", predicates);
308
309     let elaborated_env = ty::ParamEnv::new(
310         tcx.intern_predicates(&predicates),
311         unnormalized_env.reveal(),
312         unnormalized_env.constness(),
313     );
314
315     // HACK: we are trying to normalize the param-env inside *itself*. The problem is that
316     // normalization expects its param-env to be already normalized, which means we have
317     // a circularity.
318     //
319     // The way we handle this is by normalizing the param-env inside an unnormalized version
320     // of the param-env, which means that if the param-env contains unnormalized projections,
321     // we'll have some normalization failures. This is unfortunate.
322     //
323     // Lazy normalization would basically handle this by treating just the
324     // normalizing-a-trait-ref-requires-itself cycles as evaluation failures.
325     //
326     // Inferred outlives bounds can create a lot of `TypeOutlives` predicates for associated
327     // types, so to make the situation less bad, we normalize all the predicates *but*
328     // the `TypeOutlives` predicates first inside the unnormalized parameter environment, and
329     // then we normalize the `TypeOutlives` bounds inside the normalized parameter environment.
330     //
331     // This works fairly well because trait matching  does not actually care about param-env
332     // TypeOutlives predicates - these are normally used by regionck.
333     let outlives_predicates: Vec<_> = predicates
334         .drain_filter(|predicate| {
335             matches!(predicate.kind().skip_binder(), ty::PredicateKind::TypeOutlives(..))
336         })
337         .collect();
338
339     debug!(
340         "normalize_param_env_or_error: predicates=(non-outlives={:?}, outlives={:?})",
341         predicates, outlives_predicates
342     );
343     let Ok(non_outlives_predicates) = do_normalize_predicates(
344         tcx,
345         region_context,
346         cause.clone(),
347         elaborated_env,
348         predicates,
349     ) else {
350         // An unnormalized env is better than nothing.
351         debug!("normalize_param_env_or_error: errored resolving non-outlives predicates");
352         return elaborated_env;
353     };
354
355     debug!("normalize_param_env_or_error: non-outlives predicates={:?}", non_outlives_predicates);
356
357     // Not sure whether it is better to include the unnormalized TypeOutlives predicates
358     // here. I believe they should not matter, because we are ignoring TypeOutlives param-env
359     // predicates here anyway. Keeping them here anyway because it seems safer.
360     let outlives_env: Vec<_> =
361         non_outlives_predicates.iter().chain(&outlives_predicates).cloned().collect();
362     let outlives_env = ty::ParamEnv::new(
363         tcx.intern_predicates(&outlives_env),
364         unnormalized_env.reveal(),
365         unnormalized_env.constness(),
366     );
367     let Ok(outlives_predicates) = do_normalize_predicates(
368         tcx,
369         region_context,
370         cause,
371         outlives_env,
372         outlives_predicates,
373     ) else {
374         // An unnormalized env is better than nothing.
375         debug!("normalize_param_env_or_error: errored resolving outlives predicates");
376         return elaborated_env;
377     };
378     debug!("normalize_param_env_or_error: outlives predicates={:?}", outlives_predicates);
379
380     let mut predicates = non_outlives_predicates;
381     predicates.extend(outlives_predicates);
382     debug!("normalize_param_env_or_error: final predicates={:?}", predicates);
383     ty::ParamEnv::new(
384         tcx.intern_predicates(&predicates),
385         unnormalized_env.reveal(),
386         unnormalized_env.constness(),
387     )
388 }
389
390 pub fn fully_normalize<'a, 'tcx, T>(
391     infcx: &InferCtxt<'a, 'tcx>,
392     mut fulfill_cx: FulfillmentContext<'tcx>,
393     cause: ObligationCause<'tcx>,
394     param_env: ty::ParamEnv<'tcx>,
395     value: T,
396 ) -> Result<T, Vec<FulfillmentError<'tcx>>>
397 where
398     T: TypeFoldable<'tcx>,
399 {
400     debug!("fully_normalize_with_fulfillcx(value={:?})", value);
401     let selcx = &mut SelectionContext::new(infcx);
402     let Normalized { value: normalized_value, obligations } =
403         project::normalize(selcx, param_env, cause, value);
404     debug!(
405         "fully_normalize: normalized_value={:?} obligations={:?}",
406         normalized_value, obligations
407     );
408     for obligation in obligations {
409         fulfill_cx.register_predicate_obligation(selcx.infcx(), obligation);
410     }
411
412     debug!("fully_normalize: select_all_or_error start");
413     let errors = fulfill_cx.select_all_or_error(infcx);
414     if !errors.is_empty() {
415         return Err(errors);
416     }
417     debug!("fully_normalize: select_all_or_error complete");
418     let resolved_value = infcx.resolve_vars_if_possible(normalized_value);
419     debug!("fully_normalize: resolved_value={:?}", resolved_value);
420     Ok(resolved_value)
421 }
422
423 /// Normalizes the predicates and checks whether they hold in an empty environment. If this
424 /// returns true, then either normalize encountered an error or one of the predicates did not
425 /// hold. Used when creating vtables to check for unsatisfiable methods.
426 pub fn impossible_predicates<'tcx>(
427     tcx: TyCtxt<'tcx>,
428     predicates: Vec<ty::Predicate<'tcx>>,
429 ) -> bool {
430     debug!("impossible_predicates(predicates={:?})", predicates);
431
432     let result = tcx.infer_ctxt().enter(|infcx| {
433         // HACK: Set tainted by errors to gracefully exit in case of overflow.
434         infcx.set_tainted_by_errors();
435
436         let param_env = ty::ParamEnv::reveal_all();
437         let mut selcx = SelectionContext::new(&infcx);
438         let mut fulfill_cx = FulfillmentContext::new();
439         let cause = ObligationCause::dummy();
440         let Normalized { value: predicates, obligations } =
441             normalize(&mut selcx, param_env, cause.clone(), predicates);
442         for obligation in obligations {
443             fulfill_cx.register_predicate_obligation(&infcx, obligation);
444         }
445         for predicate in predicates {
446             let obligation = Obligation::new(cause.clone(), param_env, predicate);
447             fulfill_cx.register_predicate_obligation(&infcx, obligation);
448         }
449
450         let errors = fulfill_cx.select_all_or_error(&infcx);
451
452         // Clean up after ourselves
453         let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
454
455         !errors.is_empty()
456     });
457     debug!("impossible_predicates = {:?}", result);
458     result
459 }
460
461 fn subst_and_check_impossible_predicates<'tcx>(
462     tcx: TyCtxt<'tcx>,
463     key: (DefId, SubstsRef<'tcx>),
464 ) -> bool {
465     debug!("subst_and_check_impossible_predicates(key={:?})", key);
466
467     let mut predicates = tcx.predicates_of(key.0).instantiate(tcx, key.1).predicates;
468
469     // Specifically check trait fulfillment to avoid an error when trying to resolve
470     // associated items.
471     if let Some(trait_def_id) = tcx.trait_of_item(key.0) {
472         let trait_ref = ty::TraitRef::from_method(tcx, trait_def_id, key.1);
473         predicates.push(ty::Binder::dummy(trait_ref).to_poly_trait_predicate().to_predicate(tcx));
474     }
475
476     predicates.retain(|predicate| !predicate.needs_subst());
477     let result = impossible_predicates(tcx, predicates);
478
479     debug!("subst_and_check_impossible_predicates(key={:?}) = {:?}", key, result);
480     result
481 }
482
483 #[derive(Clone, Debug)]
484 enum VtblSegment<'tcx> {
485     MetadataDSA,
486     TraitOwnEntries { trait_ref: ty::PolyTraitRef<'tcx>, emit_vptr: bool },
487 }
488
489 /// Prepare the segments for a vtable
490 fn prepare_vtable_segments<'tcx, T>(
491     tcx: TyCtxt<'tcx>,
492     trait_ref: ty::PolyTraitRef<'tcx>,
493     mut segment_visitor: impl FnMut(VtblSegment<'tcx>) -> ControlFlow<T>,
494 ) -> Option<T> {
495     // The following constraints holds for the final arrangement.
496     // 1. The whole virtual table of the first direct super trait is included as the
497     //    the prefix. If this trait doesn't have any super traits, then this step
498     //    consists of the dsa metadata.
499     // 2. Then comes the proper pointer metadata(vptr) and all own methods for all
500     //    other super traits except those already included as part of the first
501     //    direct super trait virtual table.
502     // 3. finally, the own methods of this trait.
503
504     // This has the advantage that trait upcasting to the first direct super trait on each level
505     // is zero cost, and to another trait includes only replacing the pointer with one level indirection,
506     // while not using too much extra memory.
507
508     // For a single inheritance relationship like this,
509     //   D --> C --> B --> A
510     // The resulting vtable will consists of these segments:
511     //  DSA, A, B, C, D
512
513     // For a multiple inheritance relationship like this,
514     //   D --> C --> A
515     //           \-> B
516     // The resulting vtable will consists of these segments:
517     //  DSA, A, B, B-vptr, C, D
518
519     // For a diamond inheritance relationship like this,
520     //   D --> B --> A
521     //     \-> C -/
522     // The resulting vtable will consists of these segments:
523     //  DSA, A, B, C, C-vptr, D
524
525     // For a more complex inheritance relationship like this:
526     //   O --> G --> C --> A
527     //     \     \     \-> B
528     //     |     |-> F --> D
529     //     |           \-> E
530     //     |-> N --> J --> H
531     //           \     \-> I
532     //           |-> M --> K
533     //                 \-> L
534     // The resulting vtable will consists of these segments:
535     //  DSA, A, B, B-vptr, C, D, D-vptr, E, E-vptr, F, F-vptr, G,
536     //  H, H-vptr, I, I-vptr, J, J-vptr, K, K-vptr, L, L-vptr, M, M-vptr,
537     //  N, N-vptr, O
538
539     // emit dsa segment first.
540     if let ControlFlow::Break(v) = (segment_visitor)(VtblSegment::MetadataDSA) {
541         return Some(v);
542     }
543
544     let mut emit_vptr_on_new_entry = false;
545     let mut visited = util::PredicateSet::new(tcx);
546     let predicate = trait_ref.without_const().to_predicate(tcx);
547     let mut stack: SmallVec<[(ty::PolyTraitRef<'tcx>, _, _); 5]> =
548         smallvec![(trait_ref, emit_vptr_on_new_entry, None)];
549     visited.insert(predicate);
550
551     // the main traversal loop:
552     // basically we want to cut the inheritance directed graph into a few non-overlapping slices of nodes
553     // that each node is emitted after all its descendents have been emitted.
554     // so we convert the directed graph into a tree by skipping all previously visited nodes using a visited set.
555     // this is done on the fly.
556     // Each loop run emits a slice - it starts by find a "childless" unvisited node, backtracking upwards, and it
557     // stops after it finds a node that has a next-sibling node.
558     // This next-sibling node will used as the starting point of next slice.
559
560     // Example:
561     // For a diamond inheritance relationship like this,
562     //   D#1 --> B#0 --> A#0
563     //     \-> C#1 -/
564
565     // Starting point 0 stack [D]
566     // Loop run #0: Stack after diving in is [D B A], A is "childless"
567     // after this point, all newly visited nodes won't have a vtable that equals to a prefix of this one.
568     // Loop run #0: Emitting the slice [B A] (in reverse order), B has a next-sibling node, so this slice stops here.
569     // Loop run #0: Stack after exiting out is [D C], C is the next starting point.
570     // Loop run #1: Stack after diving in is [D C], C is "childless", since its child A is skipped(already emitted).
571     // Loop run #1: Emitting the slice [D C] (in reverse order). No one has a next-sibling node.
572     // Loop run #1: Stack after exiting out is []. Now the function exits.
573
574     loop {
575         // dive deeper into the stack, recording the path
576         'diving_in: loop {
577             if let Some((inner_most_trait_ref, _, _)) = stack.last() {
578                 let inner_most_trait_ref = *inner_most_trait_ref;
579                 let mut direct_super_traits_iter = tcx
580                     .super_predicates_of(inner_most_trait_ref.def_id())
581                     .predicates
582                     .into_iter()
583                     .filter_map(move |(pred, _)| {
584                         pred.subst_supertrait(tcx, &inner_most_trait_ref).to_opt_poly_trait_pred()
585                     });
586
587                 'diving_in_skip_visited_traits: loop {
588                     if let Some(next_super_trait) = direct_super_traits_iter.next() {
589                         if visited.insert(next_super_trait.to_predicate(tcx)) {
590                             // We're throwing away potential constness of super traits here.
591                             // FIXME: handle ~const super traits
592                             let next_super_trait = next_super_trait.map_bound(|t| t.trait_ref);
593                             stack.push((
594                                 next_super_trait,
595                                 emit_vptr_on_new_entry,
596                                 Some(direct_super_traits_iter),
597                             ));
598                             break 'diving_in_skip_visited_traits;
599                         } else {
600                             continue 'diving_in_skip_visited_traits;
601                         }
602                     } else {
603                         break 'diving_in;
604                     }
605                 }
606             }
607         }
608
609         // Other than the left-most path, vptr should be emitted for each trait.
610         emit_vptr_on_new_entry = true;
611
612         // emit innermost item, move to next sibling and stop there if possible, otherwise jump to outer level.
613         'exiting_out: loop {
614             if let Some((inner_most_trait_ref, emit_vptr, siblings_opt)) = stack.last_mut() {
615                 if let ControlFlow::Break(v) = (segment_visitor)(VtblSegment::TraitOwnEntries {
616                     trait_ref: *inner_most_trait_ref,
617                     emit_vptr: *emit_vptr,
618                 }) {
619                     return Some(v);
620                 }
621
622                 'exiting_out_skip_visited_traits: loop {
623                     if let Some(siblings) = siblings_opt {
624                         if let Some(next_inner_most_trait_ref) = siblings.next() {
625                             if visited.insert(next_inner_most_trait_ref.to_predicate(tcx)) {
626                                 // We're throwing away potential constness of super traits here.
627                                 // FIXME: handle ~const super traits
628                                 let next_inner_most_trait_ref =
629                                     next_inner_most_trait_ref.map_bound(|t| t.trait_ref);
630                                 *inner_most_trait_ref = next_inner_most_trait_ref;
631                                 *emit_vptr = emit_vptr_on_new_entry;
632                                 break 'exiting_out;
633                             } else {
634                                 continue 'exiting_out_skip_visited_traits;
635                             }
636                         }
637                     }
638                     stack.pop();
639                     continue 'exiting_out;
640                 }
641             }
642             // all done
643             return None;
644         }
645     }
646 }
647
648 fn dump_vtable_entries<'tcx>(
649     tcx: TyCtxt<'tcx>,
650     sp: Span,
651     trait_ref: ty::PolyTraitRef<'tcx>,
652     entries: &[VtblEntry<'tcx>],
653 ) {
654     let msg = format!("vtable entries for `{}`: {:#?}", trait_ref, entries);
655     tcx.sess.struct_span_err(sp, &msg).emit();
656 }
657
658 fn own_existential_vtable_entries<'tcx>(
659     tcx: TyCtxt<'tcx>,
660     trait_ref: ty::PolyExistentialTraitRef<'tcx>,
661 ) -> &'tcx [DefId] {
662     let trait_methods = tcx
663         .associated_items(trait_ref.def_id())
664         .in_definition_order()
665         .filter(|item| item.kind == ty::AssocKind::Fn);
666     // Now list each method's DefId (for within its trait).
667     let own_entries = trait_methods.filter_map(move |trait_method| {
668         debug!("own_existential_vtable_entry: trait_method={:?}", trait_method);
669         let def_id = trait_method.def_id;
670
671         // Some methods cannot be called on an object; skip those.
672         if !is_vtable_safe_method(tcx, trait_ref.def_id(), &trait_method) {
673             debug!("own_existential_vtable_entry: not vtable safe");
674             return None;
675         }
676
677         Some(def_id)
678     });
679
680     tcx.arena.alloc_from_iter(own_entries.into_iter())
681 }
682
683 /// Given a trait `trait_ref`, iterates the vtable entries
684 /// that come from `trait_ref`, including its supertraits.
685 fn vtable_entries<'tcx>(
686     tcx: TyCtxt<'tcx>,
687     trait_ref: ty::PolyTraitRef<'tcx>,
688 ) -> &'tcx [VtblEntry<'tcx>] {
689     debug!("vtable_entries({:?})", trait_ref);
690
691     let mut entries = vec![];
692
693     let vtable_segment_callback = |segment| -> ControlFlow<()> {
694         match segment {
695             VtblSegment::MetadataDSA => {
696                 entries.extend(TyCtxt::COMMON_VTABLE_ENTRIES);
697             }
698             VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
699                 let existential_trait_ref = trait_ref
700                     .map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
701
702                 // Lookup the shape of vtable for the trait.
703                 let own_existential_entries =
704                     tcx.own_existential_vtable_entries(existential_trait_ref);
705
706                 let own_entries = own_existential_entries.iter().copied().map(|def_id| {
707                     debug!("vtable_entries: trait_method={:?}", def_id);
708
709                     // The method may have some early-bound lifetimes; add regions for those.
710                     let substs = trait_ref.map_bound(|trait_ref| {
711                         InternalSubsts::for_item(tcx, def_id, |param, _| match param.kind {
712                             GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
713                             GenericParamDefKind::Type { .. }
714                             | GenericParamDefKind::Const { .. } => {
715                                 trait_ref.substs[param.index as usize]
716                             }
717                         })
718                     });
719
720                     // The trait type may have higher-ranked lifetimes in it;
721                     // erase them if they appear, so that we get the type
722                     // at some particular call site.
723                     let substs = tcx
724                         .normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), substs);
725
726                     // It's possible that the method relies on where-clauses that
727                     // do not hold for this particular set of type parameters.
728                     // Note that this method could then never be called, so we
729                     // do not want to try and codegen it, in that case (see #23435).
730                     let predicates = tcx.predicates_of(def_id).instantiate_own(tcx, substs);
731                     if impossible_predicates(tcx, predicates.predicates) {
732                         debug!("vtable_entries: predicates do not hold");
733                         return VtblEntry::Vacant;
734                     }
735
736                     let instance = ty::Instance::resolve_for_vtable(
737                         tcx,
738                         ty::ParamEnv::reveal_all(),
739                         def_id,
740                         substs,
741                     )
742                     .expect("resolution failed during building vtable representation");
743                     VtblEntry::Method(instance)
744                 });
745
746                 entries.extend(own_entries);
747
748                 if emit_vptr {
749                     entries.push(VtblEntry::TraitVPtr(trait_ref));
750                 }
751             }
752         }
753
754         ControlFlow::Continue(())
755     };
756
757     let _ = prepare_vtable_segments(tcx, trait_ref, vtable_segment_callback);
758
759     if tcx.has_attr(trait_ref.def_id(), sym::rustc_dump_vtable) {
760         let sp = tcx.def_span(trait_ref.def_id());
761         dump_vtable_entries(tcx, sp, trait_ref, &entries);
762     }
763
764     tcx.arena.alloc_from_iter(entries.into_iter())
765 }
766
767 /// Find slot base for trait methods within vtable entries of another trait
768 fn vtable_trait_first_method_offset<'tcx>(
769     tcx: TyCtxt<'tcx>,
770     key: (
771         ty::PolyTraitRef<'tcx>, // trait_to_be_found
772         ty::PolyTraitRef<'tcx>, // trait_owning_vtable
773     ),
774 ) -> usize {
775     let (trait_to_be_found, trait_owning_vtable) = key;
776
777     // #90177
778     let trait_to_be_found_erased = tcx.erase_regions(trait_to_be_found);
779
780     let vtable_segment_callback = {
781         let mut vtable_base = 0;
782
783         move |segment| {
784             match segment {
785                 VtblSegment::MetadataDSA => {
786                     vtable_base += TyCtxt::COMMON_VTABLE_ENTRIES.len();
787                 }
788                 VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
789                     if tcx.erase_regions(trait_ref) == trait_to_be_found_erased {
790                         return ControlFlow::Break(vtable_base);
791                     }
792                     vtable_base += util::count_own_vtable_entries(tcx, trait_ref);
793                     if emit_vptr {
794                         vtable_base += 1;
795                     }
796                 }
797             }
798             ControlFlow::Continue(())
799         }
800     };
801
802     if let Some(vtable_base) =
803         prepare_vtable_segments(tcx, trait_owning_vtable, vtable_segment_callback)
804     {
805         vtable_base
806     } else {
807         bug!("Failed to find info for expected trait in vtable");
808     }
809 }
810
811 /// Find slot offset for trait vptr within vtable entries of another trait
812 pub fn vtable_trait_upcasting_coercion_new_vptr_slot<'tcx>(
813     tcx: TyCtxt<'tcx>,
814     key: (
815         Ty<'tcx>, // trait object type whose trait owning vtable
816         Ty<'tcx>, // trait object for supertrait
817     ),
818 ) -> Option<usize> {
819     let (source, target) = key;
820     assert!(matches!(&source.kind(), &ty::Dynamic(..)) && !source.needs_infer());
821     assert!(matches!(&target.kind(), &ty::Dynamic(..)) && !target.needs_infer());
822
823     // this has been typecked-before, so diagnostics is not really needed.
824     let unsize_trait_did = tcx.require_lang_item(LangItem::Unsize, None);
825
826     let trait_ref = ty::TraitRef {
827         def_id: unsize_trait_did,
828         substs: tcx.mk_substs_trait(source, &[target.into()]),
829     };
830     let obligation = Obligation::new(
831         ObligationCause::dummy(),
832         ty::ParamEnv::reveal_all(),
833         ty::Binder::dummy(ty::TraitPredicate {
834             trait_ref,
835             constness: ty::BoundConstness::NotConst,
836             polarity: ty::ImplPolarity::Positive,
837         }),
838     );
839
840     let implsrc = tcx.infer_ctxt().enter(|infcx| {
841         let mut selcx = SelectionContext::new(&infcx);
842         selcx.select(&obligation).unwrap()
843     });
844
845     let Some(ImplSource::TraitUpcasting(implsrc_traitcasting)) = implsrc else {
846         bug!();
847     };
848
849     implsrc_traitcasting.vtable_vptr_slot
850 }
851
852 pub fn provide(providers: &mut ty::query::Providers) {
853     object_safety::provide(providers);
854     structural_match::provide(providers);
855     *providers = ty::query::Providers {
856         specialization_graph_of: specialize::specialization_graph_provider,
857         specializes: specialize::specializes,
858         codegen_fulfill_obligation: codegen::codegen_fulfill_obligation,
859         own_existential_vtable_entries,
860         vtable_entries,
861         vtable_trait_upcasting_coercion_new_vptr_slot,
862         subst_and_check_impossible_predicates,
863         thir_abstract_const: |tcx, def_id| {
864             let def_id = def_id.expect_local();
865             if let Some(def) = ty::WithOptConstParam::try_lookup(def_id, tcx) {
866                 tcx.thir_abstract_const_of_const_arg(def)
867             } else {
868                 const_evaluatable::thir_abstract_const(tcx, ty::WithOptConstParam::unknown(def_id))
869             }
870         },
871         thir_abstract_const_of_const_arg: |tcx, (did, param_did)| {
872             const_evaluatable::thir_abstract_const(
873                 tcx,
874                 ty::WithOptConstParam { did, const_param_did: Some(param_did) },
875             )
876         },
877         try_unify_abstract_consts: |tcx, param_env_and| {
878             let (param_env, (a, b)) = param_env_and.into_parts();
879             const_evaluatable::try_unify_abstract_consts(tcx, (a, b), param_env)
880         },
881         ..*providers
882     };
883 }