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