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