1use std::fmt::Debug;
2use std::ops::ControlFlow;
34use rustc_hir::def_id::DefId;
5use rustc_infer::traits::util::PredicateSet;
6use rustc_middle::bug;
7use rustc_middle::query::Providers;
8use rustc_middle::ty::{
9self, GenericArgs, GenericParamDefKind, Ty, TyCtxt, TypeVisitableExt, Upcast, VtblEntry,
10};
11use rustc_span::DUMMY_SP;
12use smallvec::{SmallVec, smallvec};
13use tracing::debug;
1415use crate::traits::{impossible_predicates, is_vtable_safe_method};
1617#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for VtblSegment<'tcx> {
#[inline]
fn clone(&self) -> VtblSegment<'tcx> {
match self {
VtblSegment::MetadataDSA => VtblSegment::MetadataDSA,
VtblSegment::TraitOwnEntries {
trait_ref: __self_0, emit_vptr: __self_1 } =>
VtblSegment::TraitOwnEntries {
trait_ref: ::core::clone::Clone::clone(__self_0),
emit_vptr: ::core::clone::Clone::clone(__self_1),
},
}
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for VtblSegment<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
VtblSegment::MetadataDSA =>
::core::fmt::Formatter::write_str(f, "MetadataDSA"),
VtblSegment::TraitOwnEntries {
trait_ref: __self_0, emit_vptr: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"TraitOwnEntries", "trait_ref", __self_0, "emit_vptr",
&__self_1),
}
}
}Debug)]
18pub enum VtblSegment<'tcx> {
19 MetadataDSA,
20 TraitOwnEntries { trait_ref: ty::TraitRef<'tcx>, emit_vptr: bool },
21}
2223/// Prepare the segments for a vtable
24// FIXME: This should take a `PolyExistentialTraitRef`, since we don't care
25// about our `Self` type here.
26pub fn prepare_vtable_segments<'tcx, T>(
27 tcx: TyCtxt<'tcx>,
28 trait_ref: ty::TraitRef<'tcx>,
29 segment_visitor: impl FnMut(VtblSegment<'tcx>) -> ControlFlow<T>,
30) -> Option<T> {
31prepare_vtable_segments_inner(tcx, trait_ref, segment_visitor).break_value()
32}
3334/// Helper for [`prepare_vtable_segments`] that returns `ControlFlow`,
35/// such that we can use `?` in the body.
36fn prepare_vtable_segments_inner<'tcx, T>(
37 tcx: TyCtxt<'tcx>,
38 trait_ref: ty::TraitRef<'tcx>,
39mut segment_visitor: impl FnMut(VtblSegment<'tcx>) -> ControlFlow<T>,
40) -> ControlFlow<T> {
41// The following constraints holds for the final arrangement.
42 // 1. The whole virtual table of the first direct super trait is included as the
43 // the prefix. If this trait doesn't have any super traits, then this step
44 // consists of the dsa metadata.
45 // 2. Then comes the proper pointer metadata(vptr) and all own methods for all
46 // other super traits except those already included as part of the first
47 // direct super trait virtual table.
48 // 3. finally, the own methods of this trait.
4950 // This has the advantage that trait upcasting to the first direct super trait on each level
51 // is zero cost, and to another trait includes only replacing the pointer with one level indirection,
52 // while not using too much extra memory.
5354 // For a single inheritance relationship like this,
55 // D --> C --> B --> A
56 // The resulting vtable will consists of these segments:
57 // DSA, A, B, C, D
5859 // For a multiple inheritance relationship like this,
60 // D --> C --> A
61 // \-> B
62 // The resulting vtable will consists of these segments:
63 // DSA, A, B, B-vptr, C, D
6465 // For a diamond inheritance relationship like this,
66 // D --> B --> A
67 // \-> C -/
68 // The resulting vtable will consists of these segments:
69 // DSA, A, B, C, C-vptr, D
7071 // For a more complex inheritance relationship like this:
72 // O --> G --> C --> A
73 // \ \ \-> B
74 // | |-> F --> D
75 // | \-> E
76 // |-> N --> J --> H
77 // \ \-> I
78 // |-> M --> K
79 // \-> L
80 // The resulting vtable will consists of these segments:
81 // DSA, A, B, B-vptr, C, D, D-vptr, E, E-vptr, F, F-vptr, G,
82 // H, H-vptr, I, I-vptr, J, J-vptr, K, K-vptr, L, L-vptr, M, M-vptr,
83 // N, N-vptr, O
8485 // emit dsa segment first.
86segment_visitor(VtblSegment::MetadataDSA)?;
8788let mut emit_vptr_on_new_entry = false;
89let mut visited = PredicateSet::new(tcx);
90let predicate = trait_ref.upcast(tcx);
91let mut stack: SmallVec<[(ty::TraitRef<'tcx>, _, _); 5]> =
92{
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push((trait_ref, emit_vptr_on_new_entry, maybe_iter(None)));
vec
} else {
::smallvec::SmallVec::from_vec(<[_]>::into_vec(::alloc::boxed::box_new([(trait_ref,
emit_vptr_on_new_entry, maybe_iter(None))])))
}
}smallvec![(trait_ref, emit_vptr_on_new_entry, maybe_iter(None))];
93visited.insert(predicate);
9495// the main traversal loop:
96 // basically we want to cut the inheritance directed graph into a few non-overlapping slices of nodes
97 // such that each node is emitted after all its descendants have been emitted.
98 // so we convert the directed graph into a tree by skipping all previously visited nodes using a visited set.
99 // this is done on the fly.
100 // Each loop run emits a slice - it starts by find a "childless" unvisited node, backtracking upwards, and it
101 // stops after it finds a node that has a next-sibling node.
102 // This next-sibling node will used as the starting point of next slice.
103104 // Example:
105 // For a diamond inheritance relationship like this,
106 // D#1 --> B#0 --> A#0
107 // \-> C#1 -/
108109 // Starting point 0 stack [D]
110 // Loop run #0: Stack after diving in is [D B A], A is "childless"
111 // after this point, all newly visited nodes won't have a vtable that equals to a prefix of this one.
112 // Loop run #0: Emitting the slice [B A] (in reverse order), B has a next-sibling node, so this slice stops here.
113 // Loop run #0: Stack after exiting out is [D C], C is the next starting point.
114 // Loop run #1: Stack after diving in is [D C], C is "childless", since its child A is skipped(already emitted).
115 // Loop run #1: Emitting the slice [D C] (in reverse order). No one has a next-sibling node.
116 // Loop run #1: Stack after exiting out is []. Now the function exits.
117118'outer: loop {
119// dive deeper into the stack, recording the path
120'diving_in: loop {
121let &(inner_most_trait_ref, _, _) = stack.last().unwrap();
122123let mut direct_super_traits_iter = tcx124 .explicit_super_predicates_of(inner_most_trait_ref.def_id)
125 .iter_identity_copied()
126 .filter_map(move |(pred, _)| {
127pred.instantiate_supertrait(tcx, ty::Binder::dummy(inner_most_trait_ref))
128 .as_trait_clause()
129 })
130 .map(move |pred| {
131tcx.normalize_erasing_late_bound_regions(
132 ty::TypingEnv::fully_monomorphized(),
133pred,
134 )
135 .trait_ref
136 });
137138// Find an unvisited supertrait
139match direct_super_traits_iter140 .find(|&super_trait| visited.insert(super_trait.upcast(tcx)))
141 {
142// Push it to the stack for the next iteration of 'diving_in to pick up
143Some(next_super_trait) => stack.push((
144next_super_trait,
145emit_vptr_on_new_entry,
146maybe_iter(Some(direct_super_traits_iter)),
147 )),
148149// There are no more unvisited direct super traits, dive-in finished
150None => break 'diving_in,
151 }
152 }
153154// emit innermost item, move to next sibling and stop there if possible, otherwise jump to outer level.
155while let Some((inner_most_trait_ref, emit_vptr, mut siblings)) = stack.pop() {
156// We don't need to emit a vptr for "truly-empty" supertraits, but we *do* need to emit a
157 // vptr for supertraits that have no methods, but that themselves have supertraits
158 // with methods, so we check if any transitive supertrait has entries here (this includes
159 // the trait itself).
160let has_entries = ty::elaborate::supertrait_def_ids(tcx, inner_most_trait_ref.def_id)
161 .any(|def_id| has_own_existential_vtable_entries(tcx, def_id));
162163 segment_visitor(VtblSegment::TraitOwnEntries {
164 trait_ref: inner_most_trait_ref,
165 emit_vptr: emit_vptr && has_entries && !tcx.sess.opts.unstable_opts.no_trait_vptr,
166 })?;
167168// If we've emitted (fed to `segment_visitor`) a trait that has methods present in the vtable,
169 // we'll need to emit vptrs from now on.
170emit_vptr_on_new_entry |= has_entries;
171172if let Some(next_inner_most_trait_ref) =
173 siblings.find(|&sibling| visited.insert(sibling.upcast(tcx)))
174 {
175 stack.push((next_inner_most_trait_ref, emit_vptr_on_new_entry, siblings));
176177// just pushed a new trait onto the stack, so we need to go through its super traits
178continue 'outer;
179 }
180 }
181182// the stack is empty, all done
183return ControlFlow::Continue(());
184 }
185}
186187/// Turns option of iterator into an iterator (this is just flatten)
188fn maybe_iter<I: Iterator>(i: Option<I>) -> impl Iterator<Item = I::Item> {
189// Flatten is bad perf-vise, we could probably implement a special case here that is better
190i.into_iter().flatten()
191}
192193fn has_own_existential_vtable_entries(tcx: TyCtxt<'_>, trait_def_id: DefId) -> bool {
194own_existential_vtable_entries_iter(tcx, trait_def_id).next().is_some()
195}
196197fn own_existential_vtable_entries(tcx: TyCtxt<'_>, trait_def_id: DefId) -> &[DefId] {
198tcx.arena.alloc_from_iter(own_existential_vtable_entries_iter(tcx, trait_def_id))
199}
200201fn own_existential_vtable_entries_iter(
202 tcx: TyCtxt<'_>,
203 trait_def_id: DefId,
204) -> impl Iterator<Item = DefId> {
205let trait_methods =
206tcx.associated_items(trait_def_id).in_definition_order().filter(|item| item.is_fn());
207208// Now list each method's DefId (for within its trait).
209let own_entries = trait_methods.filter_map(move |&trait_method| {
210{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/vtable.rs:210",
"rustc_trait_selection::traits::vtable",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/vtable.rs"),
::tracing_core::__macro_support::Option::Some(210u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::vtable"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("own_existential_vtable_entry: trait_method={0:?}",
trait_method) as &dyn Value))])
});
} else { ; }
};debug!("own_existential_vtable_entry: trait_method={:?}", trait_method);
211let def_id = trait_method.def_id;
212213// Some methods cannot be called on an object; skip those.
214if !is_vtable_safe_method(tcx, trait_def_id, trait_method) {
215{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/vtable.rs:215",
"rustc_trait_selection::traits::vtable",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/vtable.rs"),
::tracing_core::__macro_support::Option::Some(215u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::vtable"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("own_existential_vtable_entry: not vtable safe")
as &dyn Value))])
});
} else { ; }
};debug!("own_existential_vtable_entry: not vtable safe");
216return None;
217 }
218219Some(def_id)
220 });
221222own_entries223}
224225/// Given a trait `trait_ref`, iterates the vtable entries
226/// that come from `trait_ref`, including its supertraits.
227fn vtable_entries<'tcx>(
228 tcx: TyCtxt<'tcx>,
229 trait_ref: ty::TraitRef<'tcx>,
230) -> &'tcx [VtblEntry<'tcx>] {
231if true {
if !(!trait_ref.has_non_region_infer() &&
!trait_ref.has_non_region_param()) {
::core::panicking::panic("assertion failed: !trait_ref.has_non_region_infer() && !trait_ref.has_non_region_param()")
};
};debug_assert!(!trait_ref.has_non_region_infer() && !trait_ref.has_non_region_param());
232if true {
match (&tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
trait_ref), &trait_ref) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val,
::core::option::Option::Some(format_args!("vtable trait ref should be normalized")));
}
}
};
};debug_assert_eq!(
233 tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), trait_ref),
234 trait_ref,
235"vtable trait ref should be normalized"
236);
237238{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/vtable.rs:238",
"rustc_trait_selection::traits::vtable",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/vtable.rs"),
::tracing_core::__macro_support::Option::Some(238u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::vtable"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("vtable_entries({0:?})",
trait_ref) as &dyn Value))])
});
} else { ; }
};debug!("vtable_entries({:?})", trait_ref);
239240let mut entries = ::alloc::vec::Vec::new()vec![];
241242let vtable_segment_callback = |segment| -> ControlFlow<()> {
243match segment {
244 VtblSegment::MetadataDSA => {
245entries.extend(TyCtxt::COMMON_VTABLE_ENTRIES);
246 }
247 VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
248let existential_trait_ref = ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref);
249250// Lookup the shape of vtable for the trait.
251let own_existential_entries =
252tcx.own_existential_vtable_entries(existential_trait_ref.def_id);
253254let own_entries = own_existential_entries.iter().copied().map(|def_id| {
255{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/vtable.rs:255",
"rustc_trait_selection::traits::vtable",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/vtable.rs"),
::tracing_core::__macro_support::Option::Some(255u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::vtable"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("vtable_entries: trait_method={0:?}",
def_id) as &dyn Value))])
});
} else { ; }
};debug!("vtable_entries: trait_method={:?}", def_id);
256257// The method may have some early-bound lifetimes; add regions for those.
258 // FIXME: Is this normalize needed?
259let args = tcx.normalize_erasing_regions(
260 ty::TypingEnv::fully_monomorphized(),
261 GenericArgs::for_item(tcx, def_id, |param, _| match param.kind {
262 GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
263 GenericParamDefKind::Type { .. }
264 | GenericParamDefKind::Const { .. } => {
265trait_ref.args[param.index as usize]
266 }
267 }),
268 );
269270// It's possible that the method relies on where-clauses that
271 // do not hold for this particular set of type parameters.
272 // Note that this method could then never be called, so we
273 // do not want to try and codegen it, in that case (see #23435).
274let predicates = tcx.predicates_of(def_id).instantiate_own(tcx, args);
275if impossible_predicates(
276tcx,
277predicates.map(|(predicate, _)| predicate).collect(),
278 ) {
279{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/vtable.rs:279",
"rustc_trait_selection::traits::vtable",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/vtable.rs"),
::tracing_core::__macro_support::Option::Some(279u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::vtable"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("vtable_entries: predicates do not hold")
as &dyn Value))])
});
} else { ; }
};debug!("vtable_entries: predicates do not hold");
280return VtblEntry::Vacant;
281 }
282283let instance = ty::Instance::expect_resolve_for_vtable(
284tcx,
285 ty::TypingEnv::fully_monomorphized(),
286def_id,
287args,
288DUMMY_SP,
289 );
290291 VtblEntry::Method(instance)
292 });
293294entries.extend(own_entries);
295296if emit_vptr {
297entries.push(VtblEntry::TraitVPtr(trait_ref));
298 }
299 }
300 }
301302 ControlFlow::Continue(())
303 };
304305let _ = prepare_vtable_segments(tcx, trait_ref, vtable_segment_callback);
306307tcx.arena.alloc_from_iter(entries)
308}
309310// Given a `dyn Subtrait: Supertrait` trait ref, find corresponding first slot
311// for `Supertrait`'s methods in the vtable of `Subtrait`.
312pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRef<'tcx>) -> usize {
313if true {
if !(!key.has_non_region_infer() && !key.has_non_region_param()) {
::core::panicking::panic("assertion failed: !key.has_non_region_infer() && !key.has_non_region_param()")
};
};debug_assert!(!key.has_non_region_infer() && !key.has_non_region_param());
314if true {
match (&tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
key), &key) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val,
::core::option::Option::Some(format_args!("vtable trait ref should be normalized")));
}
}
};
};debug_assert_eq!(
315 tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), key),
316 key,
317"vtable trait ref should be normalized"
318);
319320let ty::Dynamic(source, _) = *key.self_ty().kind() else {
321::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
322 };
323let source_principal = tcx.instantiate_bound_regions_with_erased(
324source.principal().unwrap().with_self_ty(tcx, key.self_ty()),
325 );
326327// We're monomorphizing a call to a dyn trait object that can never be constructed.
328if tcx.instantiate_and_check_impossible_predicates((
329source_principal.def_id,
330source_principal.args,
331 )) {
332return 0;
333 }
334335let target_principal = ty::ExistentialTraitRef::erase_self_ty(tcx, key);
336337let vtable_segment_callback = {
338let mut vptr_offset = 0;
339move |segment| {
340match segment {
341 VtblSegment::MetadataDSA => {
342vptr_offset += TyCtxt::COMMON_VTABLE_ENTRIES.len();
343 }
344 VtblSegment::TraitOwnEntries { trait_ref: vtable_principal, emit_vptr } => {
345if ty::ExistentialTraitRef::erase_self_ty(tcx, vtable_principal)
346 == target_principal347 {
348return ControlFlow::Break(vptr_offset);
349 }
350351vptr_offset +=
352tcx.own_existential_vtable_entries(vtable_principal.def_id).len();
353354if emit_vptr {
355vptr_offset += 1;
356 }
357 }
358 }
359 ControlFlow::Continue(())
360 }
361 };
362363prepare_vtable_segments(tcx, source_principal, vtable_segment_callback).unwrap()
364}
365366/// Given a `dyn Subtrait` and `dyn Supertrait` trait object, find the slot of
367/// the trait vptr in the subtrait's vtable.
368///
369/// A return value of `None` means that the original vtable can be reused.
370pub(crate) fn supertrait_vtable_slot<'tcx>(
371 tcx: TyCtxt<'tcx>,
372 key: (
373 Ty<'tcx>, // Source -- `dyn Subtrait`.
374Ty<'tcx>, // Target -- `dyn Supertrait` being coerced to.
375),
376) -> Option<usize> {
377if true {
if !(!key.has_non_region_infer() && !key.has_non_region_param()) {
::core::panicking::panic("assertion failed: !key.has_non_region_infer() && !key.has_non_region_param()")
};
};debug_assert!(!key.has_non_region_infer() && !key.has_non_region_param());
378if true {
match (&tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
key), &key) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val,
::core::option::Option::Some(format_args!("upcasting trait refs should be normalized")));
}
}
};
};debug_assert_eq!(
379 tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), key),
380 key,
381"upcasting trait refs should be normalized"
382);
383384let (source, target) = key;
385386// If the target principal is `None`, we can just return `None`.
387let ty::Dynamic(target_data, _) = *target.kind() else {
388::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
389 };
390let target_principal = tcx.instantiate_bound_regions_with_erased(target_data.principal()?);
391392// Given that we have a target principal, it is a bug for there not to be a source principal.
393let ty::Dynamic(source_data, _) = *source.kind() else {
394::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
395 };
396let source_principal = tcx.instantiate_bound_regions_with_erased(
397source_data.principal().unwrap().with_self_ty(tcx, source),
398 );
399400// We're monomorphizing a dyn trait object upcast that can never be constructed.
401if tcx.instantiate_and_check_impossible_predicates((
402source_principal.def_id,
403source_principal.args,
404 )) {
405return None;
406 }
407408let vtable_segment_callback = {
409let mut vptr_offset = 0;
410move |segment| {
411match segment {
412 VtblSegment::MetadataDSA => {
413vptr_offset += TyCtxt::COMMON_VTABLE_ENTRIES.len();
414 }
415 VtblSegment::TraitOwnEntries { trait_ref: vtable_principal, emit_vptr } => {
416vptr_offset +=
417tcx.own_existential_vtable_entries(vtable_principal.def_id).len();
418if ty::ExistentialTraitRef::erase_self_ty(tcx, vtable_principal)
419 == target_principal420 {
421if emit_vptr {
422return ControlFlow::Break(Some(vptr_offset));
423 } else {
424return ControlFlow::Break(None);
425 }
426 }
427428if emit_vptr {
429vptr_offset += 1;
430 }
431 }
432 }
433 ControlFlow::Continue(())
434 }
435 };
436437prepare_vtable_segments(tcx, source_principal, vtable_segment_callback).unwrap()
438}
439440pub(super) fn provide(providers: &mut Providers) {
441*providers = Providers {
442own_existential_vtable_entries,
443vtable_entries,
444first_method_vtable_slot,
445supertrait_vtable_slot,
446 ..*providers447 };
448}