Skip to main content

rustc_trait_selection/traits/
vtable.rs

1use std::fmt::Debug;
2use std::ops::ControlFlow;
3
4use rustc_hir::def_id::DefId;
5use rustc_infer::traits::util::PredicateSet;
6use rustc_middle::bug;
7use rustc_middle::query::Providers;
8use rustc_middle::ty::{
9    self, GenericArgs, GenericParamDefKind, Ty, TyCtxt, TypeVisitableExt, Unnormalized, Upcast,
10    VtblEntry,
11};
12use rustc_span::DUMMY_SP;
13use smallvec::{SmallVec, smallvec};
14use tracing::debug;
15
16use crate::traits::{impossible_predicates, is_vtable_safe_method};
17
18#[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)]
19pub enum VtblSegment<'tcx> {
20    MetadataDSA,
21    TraitOwnEntries { trait_ref: ty::TraitRef<'tcx>, emit_vptr: bool },
22}
23
24/// Prepare the segments for a vtable
25// FIXME: This should take a `PolyExistentialTraitRef`, since we don't care
26// about our `Self` type here.
27pub fn prepare_vtable_segments<'tcx, T>(
28    tcx: TyCtxt<'tcx>,
29    trait_ref: ty::TraitRef<'tcx>,
30    segment_visitor: impl FnMut(VtblSegment<'tcx>) -> ControlFlow<T>,
31) -> Option<T> {
32    prepare_vtable_segments_inner(tcx, trait_ref, segment_visitor).break_value()
33}
34
35/// Helper for [`prepare_vtable_segments`] that returns `ControlFlow`,
36/// such that we can use `?` in the body.
37fn prepare_vtable_segments_inner<'tcx, T>(
38    tcx: TyCtxt<'tcx>,
39    trait_ref: ty::TraitRef<'tcx>,
40    mut segment_visitor: impl FnMut(VtblSegment<'tcx>) -> ControlFlow<T>,
41) -> ControlFlow<T> {
42    // The following constraints holds for the final arrangement.
43    // 1. The whole virtual table of the first direct super trait is included as the
44    //    the prefix. If this trait doesn't have any super traits, then this step
45    //    consists of the dsa metadata.
46    // 2. Then comes the proper pointer metadata(vptr) and all own methods for all
47    //    other super traits except those already included as part of the first
48    //    direct super trait virtual table.
49    // 3. finally, the own methods of this trait.
50
51    // This has the advantage that trait upcasting to the first direct super trait on each level
52    // is zero cost, and to another trait includes only replacing the pointer with one level indirection,
53    // while not using too much extra memory.
54
55    // For a single inheritance relationship like this,
56    //   D --> C --> B --> A
57    // The resulting vtable will consists of these segments:
58    //  DSA, A, B, C, D
59
60    // For a multiple inheritance relationship like this,
61    //   D --> C --> A
62    //           \-> B
63    // The resulting vtable will consists of these segments:
64    //  DSA, A, B, B-vptr, C, D
65
66    // For a diamond inheritance relationship like this,
67    //   D --> B --> A
68    //     \-> C -/
69    // The resulting vtable will consists of these segments:
70    //  DSA, A, B, C, C-vptr, D
71
72    // For a more complex inheritance relationship like this:
73    //   O --> G --> C --> A
74    //     \     \     \-> B
75    //     |     |-> F --> D
76    //     |           \-> E
77    //     |-> N --> J --> H
78    //           \     \-> I
79    //           |-> M --> K
80    //                 \-> L
81    // The resulting vtable will consists of these segments:
82    //  DSA, A, B, B-vptr, C, D, D-vptr, E, E-vptr, F, F-vptr, G,
83    //  H, H-vptr, I, I-vptr, J, J-vptr, K, K-vptr, L, L-vptr, M, M-vptr,
84    //  N, N-vptr, O
85
86    // emit dsa segment first.
87    segment_visitor(VtblSegment::MetadataDSA)?;
88
89    let mut emit_vptr_on_new_entry = false;
90    let mut visited = PredicateSet::new(tcx);
91    let predicate = trait_ref.upcast(tcx);
92    let mut stack: SmallVec<[(ty::TraitRef<'tcx>, _, _); 5]> =
93        {
    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(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [(trait_ref, emit_vptr_on_new_entry, maybe_iter(None))])))
    }
}smallvec![(trait_ref, emit_vptr_on_new_entry, maybe_iter(None))];
94    visited.insert(predicate);
95
96    // the main traversal loop:
97    // basically we want to cut the inheritance directed graph into a few non-overlapping slices of nodes
98    // such that each node is emitted after all its descendants have been emitted.
99    // so we convert the directed graph into a tree by skipping all previously visited nodes using a visited set.
100    // this is done on the fly.
101    // Each loop run emits a slice - it starts by find a "childless" unvisited node, backtracking upwards, and it
102    // stops after it finds a node that has a next-sibling node.
103    // This next-sibling node will used as the starting point of next slice.
104
105    // Example:
106    // For a diamond inheritance relationship like this,
107    //   D#1 --> B#0 --> A#0
108    //     \-> C#1 -/
109
110    // Starting point 0 stack [D]
111    // Loop run #0: Stack after diving in is [D B A], A is "childless"
112    // after this point, all newly visited nodes won't have a vtable that equals to a prefix of this one.
113    // Loop run #0: Emitting the slice [B A] (in reverse order), B has a next-sibling node, so this slice stops here.
114    // Loop run #0: Stack after exiting out is [D C], C is the next starting point.
115    // Loop run #1: Stack after diving in is [D C], C is "childless", since its child A is skipped(already emitted).
116    // Loop run #1: Emitting the slice [D C] (in reverse order). No one has a next-sibling node.
117    // Loop run #1: Stack after exiting out is []. Now the function exits.
118
119    'outer: loop {
120        // dive deeper into the stack, recording the path
121        'diving_in: loop {
122            let &(inner_most_trait_ref, _, _) = stack.last().unwrap();
123
124            let mut direct_super_traits_iter = tcx
125                .explicit_super_predicates_of(inner_most_trait_ref.def_id)
126                .iter_identity_copied()
127                .map(Unnormalized::skip_norm_wip)
128                .filter_map(move |(pred, _)| {
129                    pred.instantiate_supertrait(tcx, ty::Binder::dummy(inner_most_trait_ref))
130                        .as_trait_clause()
131                })
132                .map(move |pred| {
133                    tcx.normalize_erasing_late_bound_regions(
134                        ty::TypingEnv::fully_monomorphized(),
135                        pred,
136                    )
137                    .trait_ref
138                });
139
140            // Find an unvisited supertrait
141            match direct_super_traits_iter
142                .find(|&super_trait| visited.insert(super_trait.upcast(tcx)))
143            {
144                // Push it to the stack for the next iteration of 'diving_in to pick up
145                Some(next_super_trait) => stack.push((
146                    next_super_trait,
147                    emit_vptr_on_new_entry,
148                    maybe_iter(Some(direct_super_traits_iter)),
149                )),
150
151                // There are no more unvisited direct super traits, dive-in finished
152                None => break 'diving_in,
153            }
154        }
155
156        // emit innermost item, move to next sibling and stop there if possible, otherwise jump to outer level.
157        while let Some((inner_most_trait_ref, emit_vptr, mut siblings)) = stack.pop() {
158            // We don't need to emit a vptr for "truly-empty" supertraits, but we *do* need to emit a
159            // vptr for supertraits that have no methods, but that themselves have supertraits
160            // with methods, so we check if any transitive supertrait has entries here (this includes
161            // the trait itself).
162            let has_entries = ty::elaborate::supertrait_def_ids(tcx, inner_most_trait_ref.def_id)
163                .any(|def_id| has_own_existential_vtable_entries(tcx, def_id));
164
165            segment_visitor(VtblSegment::TraitOwnEntries {
166                trait_ref: inner_most_trait_ref,
167                emit_vptr: emit_vptr && has_entries && !tcx.sess.opts.unstable_opts.no_trait_vptr,
168            })?;
169
170            // If we've emitted (fed to `segment_visitor`) a trait that has methods present in the vtable,
171            // we'll need to emit vptrs from now on.
172            emit_vptr_on_new_entry |= has_entries;
173
174            if let Some(next_inner_most_trait_ref) =
175                siblings.find(|&sibling| visited.insert(sibling.upcast(tcx)))
176            {
177                stack.push((next_inner_most_trait_ref, emit_vptr_on_new_entry, siblings));
178
179                // just pushed a new trait onto the stack, so we need to go through its super traits
180                continue 'outer;
181            }
182        }
183
184        // the stack is empty, all done
185        return ControlFlow::Continue(());
186    }
187}
188
189/// Turns option of iterator into an iterator (this is just flatten)
190fn maybe_iter<I: Iterator>(i: Option<I>) -> impl Iterator<Item = I::Item> {
191    // Flatten is bad perf-vise, we could probably implement a special case here that is better
192    i.into_iter().flatten()
193}
194
195fn has_own_existential_vtable_entries(tcx: TyCtxt<'_>, trait_def_id: DefId) -> bool {
196    own_existential_vtable_entries_iter(tcx, trait_def_id).next().is_some()
197}
198
199fn own_existential_vtable_entries(tcx: TyCtxt<'_>, trait_def_id: DefId) -> &[DefId] {
200    tcx.arena.alloc_from_iter(own_existential_vtable_entries_iter(tcx, trait_def_id))
201}
202
203fn own_existential_vtable_entries_iter(
204    tcx: TyCtxt<'_>,
205    trait_def_id: DefId,
206) -> impl Iterator<Item = DefId> {
207    let trait_methods =
208        tcx.associated_items(trait_def_id).in_definition_order().filter(|item| item.is_fn());
209
210    // Now list each method's DefId (for within its trait).
211    let own_entries = trait_methods.filter_map(move |&trait_method| {
212        {
    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:212",
                        "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(212u32),
                        ::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);
213        let def_id = trait_method.def_id;
214
215        // Final methods should not be included in the vtable.
216        if trait_method.defaultness(tcx).is_final() {
217            return None;
218        }
219
220        // Some methods cannot be called on an object; skip those.
221        if !is_vtable_safe_method(tcx, trait_def_id, trait_method) {
222            {
    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:222",
                        "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(222u32),
                        ::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");
223            return None;
224        }
225
226        Some(def_id)
227    });
228
229    own_entries
230}
231
232/// Given a trait `trait_ref`, iterates the vtable entries
233/// that come from `trait_ref`, including its supertraits.
234fn vtable_entries<'tcx>(
235    tcx: TyCtxt<'tcx>,
236    trait_ref: ty::TraitRef<'tcx>,
237) -> &'tcx [VtblEntry<'tcx>] {
238    if 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());
239    if true {
    match (&tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
                    Unnormalized::new_wip(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!(
240        tcx.normalize_erasing_regions(
241            ty::TypingEnv::fully_monomorphized(),
242            Unnormalized::new_wip(trait_ref)
243        ),
244        trait_ref,
245        "vtable trait ref should be normalized"
246    );
247
248    {
    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:248",
                        "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(248u32),
                        ::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);
249
250    let mut entries = ::alloc::vec::Vec::new()vec![];
251
252    let vtable_segment_callback = |segment| -> ControlFlow<()> {
253        match segment {
254            VtblSegment::MetadataDSA => {
255                entries.extend(TyCtxt::COMMON_VTABLE_ENTRIES);
256            }
257            VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
258                let existential_trait_ref = ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref);
259
260                // Lookup the shape of vtable for the trait.
261                let own_existential_entries =
262                    tcx.own_existential_vtable_entries(existential_trait_ref.def_id);
263
264                let own_entries = own_existential_entries.iter().copied().map(|def_id| {
265                    {
    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:265",
                        "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(265u32),
                        ::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);
266
267                    // The method may have some early-bound lifetimes; add regions for those.
268                    // FIXME: Is this normalize needed?
269                    let args = tcx.normalize_erasing_regions(
270                        ty::TypingEnv::fully_monomorphized(),
271                        Unnormalized::new_wip(GenericArgs::for_item(tcx, def_id, |param, _| {
272                            match param.kind {
273                                GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
274                                GenericParamDefKind::Type { .. }
275                                | GenericParamDefKind::Const { .. } => {
276                                    trait_ref.args[param.index as usize]
277                                }
278                            }
279                        })),
280                    );
281
282                    // It's possible that the method relies on where-clauses that
283                    // do not hold for this particular set of type parameters.
284                    // Note that this method could then never be called, so we
285                    // do not want to try and codegen it, in that case (see #23435).
286                    let predicates = tcx.predicates_of(def_id).instantiate_own(tcx, args);
287                    if impossible_predicates(
288                        tcx,
289                        predicates.map(|(predicate, _)| predicate.skip_norm_wip()).collect(),
290                    ) {
291                        {
    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:291",
                        "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(291u32),
                        ::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");
292                        return VtblEntry::Vacant;
293                    }
294
295                    let instance = ty::Instance::expect_resolve_for_vtable(
296                        tcx,
297                        ty::TypingEnv::fully_monomorphized(),
298                        def_id,
299                        args,
300                        DUMMY_SP,
301                    );
302
303                    VtblEntry::Method(instance)
304                });
305
306                entries.extend(own_entries);
307
308                if emit_vptr {
309                    entries.push(VtblEntry::TraitVPtr(trait_ref));
310                }
311            }
312        }
313
314        ControlFlow::Continue(())
315    };
316
317    let _ = prepare_vtable_segments(tcx, trait_ref, vtable_segment_callback);
318
319    tcx.arena.alloc_from_iter(entries)
320}
321
322// Given a `dyn Subtrait: Supertrait` trait ref, find corresponding first slot
323// for `Supertrait`'s methods in the vtable of `Subtrait`.
324pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRef<'tcx>) -> usize {
325    if 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());
326    if true {
    match (&tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
                    Unnormalized::new_wip(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!(
327        tcx.normalize_erasing_regions(
328            ty::TypingEnv::fully_monomorphized(),
329            Unnormalized::new_wip(key)
330        ),
331        key,
332        "vtable trait ref should be normalized"
333    );
334
335    let ty::Dynamic(source, _) = *key.self_ty().kind() else {
336        ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
337    };
338    let source_principal = tcx.instantiate_bound_regions_with_erased(
339        source.principal().unwrap().with_self_ty(tcx, key.self_ty()),
340    );
341
342    // We're monomorphizing a call to a dyn trait object that can never be constructed.
343    if tcx.instantiate_and_check_impossible_predicates((
344        source_principal.def_id,
345        source_principal.args,
346    )) {
347        return 0;
348    }
349
350    let target_principal = ty::ExistentialTraitRef::erase_self_ty(tcx, key);
351
352    let vtable_segment_callback = {
353        let mut vptr_offset = 0;
354        move |segment| {
355            match segment {
356                VtblSegment::MetadataDSA => {
357                    vptr_offset += TyCtxt::COMMON_VTABLE_ENTRIES.len();
358                }
359                VtblSegment::TraitOwnEntries { trait_ref: vtable_principal, emit_vptr } => {
360                    if ty::ExistentialTraitRef::erase_self_ty(tcx, vtable_principal)
361                        == target_principal
362                    {
363                        return ControlFlow::Break(vptr_offset);
364                    }
365
366                    vptr_offset +=
367                        tcx.own_existential_vtable_entries(vtable_principal.def_id).len();
368
369                    if emit_vptr {
370                        vptr_offset += 1;
371                    }
372                }
373            }
374            ControlFlow::Continue(())
375        }
376    };
377
378    prepare_vtable_segments(tcx, source_principal, vtable_segment_callback).unwrap()
379}
380
381/// Given a `dyn Subtrait` and `dyn Supertrait` trait object, find the slot of
382/// the trait vptr in the subtrait's vtable.
383///
384/// A return value of `None` means that the original vtable can be reused.
385pub(crate) fn supertrait_vtable_slot<'tcx>(
386    tcx: TyCtxt<'tcx>,
387    key: (
388        Ty<'tcx>, // Source -- `dyn Subtrait`.
389        Ty<'tcx>, // Target -- `dyn Supertrait` being coerced to.
390    ),
391) -> Option<usize> {
392    if 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());
393    if true {
    match (&tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
                    Unnormalized::new_wip(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!(
394        tcx.normalize_erasing_regions(
395            ty::TypingEnv::fully_monomorphized(),
396            Unnormalized::new_wip(key)
397        ),
398        key,
399        "upcasting trait refs should be normalized"
400    );
401
402    let (source, target) = key;
403
404    // If the target principal is `None`, we can just return `None`.
405    let ty::Dynamic(target_data, _) = *target.kind() else {
406        ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
407    };
408    let target_principal = tcx.instantiate_bound_regions_with_erased(target_data.principal()?);
409
410    // Given that we have a target principal, it is a bug for there not to be a source principal.
411    let ty::Dynamic(source_data, _) = *source.kind() else {
412        ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
413    };
414    let source_principal = tcx.instantiate_bound_regions_with_erased(
415        source_data.principal().unwrap().with_self_ty(tcx, source),
416    );
417
418    // We're monomorphizing a dyn trait object upcast that can never be constructed.
419    if tcx.instantiate_and_check_impossible_predicates((
420        source_principal.def_id,
421        source_principal.args,
422    )) {
423        return None;
424    }
425
426    let vtable_segment_callback = {
427        let mut vptr_offset = 0;
428        move |segment| {
429            match segment {
430                VtblSegment::MetadataDSA => {
431                    vptr_offset += TyCtxt::COMMON_VTABLE_ENTRIES.len();
432                }
433                VtblSegment::TraitOwnEntries { trait_ref: vtable_principal, emit_vptr } => {
434                    vptr_offset +=
435                        tcx.own_existential_vtable_entries(vtable_principal.def_id).len();
436                    if ty::ExistentialTraitRef::erase_self_ty(tcx, vtable_principal)
437                        == target_principal
438                    {
439                        if emit_vptr {
440                            return ControlFlow::Break(Some(vptr_offset));
441                        } else {
442                            return ControlFlow::Break(None);
443                        }
444                    }
445
446                    if emit_vptr {
447                        vptr_offset += 1;
448                    }
449                }
450            }
451            ControlFlow::Continue(())
452        }
453    };
454
455    prepare_vtable_segments(tcx, source_principal, vtable_segment_callback).unwrap_or_else(|| {
456        // This can happen if the trait hierarchy is malformed (e.g., due to
457        // missing generics on a supertrait bound). There should already be an error
458        // emitted for this, so we just delay the ICE.
459        tcx.dcx().delayed_bug(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("could not find the supertrait vtable slot for `{0}` -> `{1}`",
                source, target))
    })format!(
460            "could not find the supertrait vtable slot for `{}` -> `{}`",
461            source, target
462        ));
463        None
464    })
465}
466
467pub(super) fn provide(providers: &mut Providers) {
468    *providers = Providers {
469        own_existential_vtable_entries,
470        vtable_entries,
471        first_method_vtable_slot,
472        supertrait_vtable_slot,
473        ..*providers
474    };
475}