Skip to main content

rustc_traits/
dropck_outlives.rs

1use rustc_data_structures::fx::FxHashSet;
2use rustc_infer::infer::TyCtxtInferExt;
3use rustc_infer::infer::canonical::{Canonical, QueryResponse};
4use rustc_middle::bug;
5use rustc_middle::query::Providers;
6use rustc_middle::traits::query::{DropckConstraint, DropckOutlivesResult};
7use rustc_middle::ty::{self, GenericArgs, TyCtxt};
8use rustc_span::DUMMY_SP;
9use rustc_span::def_id::DefId;
10use rustc_trait_selection::infer::InferCtxtBuilderExt;
11use rustc_trait_selection::traits::query::dropck_outlives::{
12    compute_dropck_outlives_inner, dtorck_constraint_for_ty_inner,
13};
14use rustc_trait_selection::traits::query::{CanonicalDropckOutlivesGoal, NoSolution};
15use tracing::debug;
16
17pub(crate) fn provide(p: &mut Providers) {
18    *p = Providers { dropck_outlives, adt_dtorck_constraint, ..*p };
19}
20
21fn dropck_outlives<'tcx>(
22    tcx: TyCtxt<'tcx>,
23    canonical_goal: CanonicalDropckOutlivesGoal<'tcx>,
24) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, DropckOutlivesResult<'tcx>>>, NoSolution> {
25    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_traits/src/dropck_outlives.rs:25",
                        "rustc_traits::dropck_outlives", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_traits/src/dropck_outlives.rs"),
                        ::tracing_core::__macro_support::Option::Some(25u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_traits::dropck_outlives"),
                        ::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!("dropck_outlives(goal={0:#?})",
                                                    canonical_goal) as &dyn Value))])
            });
    } else { ; }
};debug!("dropck_outlives(goal={:#?})", canonical_goal);
26
27    tcx.infer_ctxt().enter_canonical_trait_query(&canonical_goal, |ocx, goal| {
28        compute_dropck_outlives_inner(ocx, goal, DUMMY_SP)
29    })
30}
31
32/// Calculates the dtorck constraint for a type.
33pub(crate) fn adt_dtorck_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &DropckConstraint<'_> {
34    let def = tcx.adt_def(def_id);
35    let span = tcx.def_span(def_id);
36    let typing_env = ty::TypingEnv::non_body_analysis(tcx, def_id);
37    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_traits/src/dropck_outlives.rs:37",
                        "rustc_traits::dropck_outlives", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_traits/src/dropck_outlives.rs"),
                        ::tracing_core::__macro_support::Option::Some(37u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_traits::dropck_outlives"),
                        ::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!("dtorck_constraint: {0:?}",
                                                    def) as &dyn Value))])
            });
    } else { ; }
};debug!("dtorck_constraint: {:?}", def);
38
39    if def.is_manually_drop() {
40        ::rustc_middle::util::bug::bug_fmt(format_args!("`ManuallyDrop` should have been handled by `trivial_dropck_outlives`"));bug!("`ManuallyDrop` should have been handled by `trivial_dropck_outlives`");
41    } else if def.is_phantom_data() {
42        // The first generic parameter here is guaranteed to be a type because it's
43        // `PhantomData`.
44        let args = GenericArgs::identity_for_item(tcx, def_id);
45        match (&args.len(), &1) {
    (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::None);
        }
    }
};assert_eq!(args.len(), 1);
46        let result = DropckConstraint {
47            outlives: ::alloc::vec::Vec::new()vec![],
48            dtorck_types: <[_]>::into_vec(::alloc::boxed::box_new([args.type_at(0)]))vec![args.type_at(0)],
49            overflows: ::alloc::vec::Vec::new()vec![],
50        };
51        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_traits/src/dropck_outlives.rs:51",
                        "rustc_traits::dropck_outlives", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_traits/src/dropck_outlives.rs"),
                        ::tracing_core::__macro_support::Option::Some(51u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_traits::dropck_outlives"),
                        ::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!("dtorck_constraint: {0:?} => {1:?}",
                                                    def, result) as &dyn Value))])
            });
    } else { ; }
};debug!("dtorck_constraint: {:?} => {:?}", def, result);
52        return tcx.arena.alloc(result);
53    }
54
55    let mut result = DropckConstraint::empty();
56    for field in def.all_fields() {
57        let fty = tcx.type_of(field.did).instantiate_identity();
58        dtorck_constraint_for_ty_inner(tcx, typing_env, span, 0, fty, &mut result);
59    }
60    result.outlives.extend(tcx.destructor_constraints(def));
61    dedup_dtorck_constraint(&mut result);
62
63    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_traits/src/dropck_outlives.rs:63",
                        "rustc_traits::dropck_outlives", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_traits/src/dropck_outlives.rs"),
                        ::tracing_core::__macro_support::Option::Some(63u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_traits::dropck_outlives"),
                        ::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!("dtorck_constraint: {0:?} => {1:?}",
                                                    def, result) as &dyn Value))])
            });
    } else { ; }
};debug!("dtorck_constraint: {:?} => {:?}", def, result);
64
65    tcx.arena.alloc(result)
66}
67
68fn dedup_dtorck_constraint(c: &mut DropckConstraint<'_>) {
69    let mut outlives = FxHashSet::default();
70    let mut dtorck_types = FxHashSet::default();
71
72    c.outlives.retain(|&val| outlives.replace(val).is_none());
73    c.dtorck_types.retain(|&val| dtorck_types.replace(val).is_none());
74}