rustc_traits/
dropck_outlives.rs

1use rustc_data_structures::fx::FxHashSet;
2use rustc_hir::def_id::DefId;
3use rustc_infer::infer::TyCtxtInferExt;
4use rustc_infer::infer::canonical::{Canonical, QueryResponse};
5use rustc_middle::bug;
6use rustc_middle::query::Providers;
7use rustc_middle::traits::query::{DropckConstraint, DropckOutlivesResult};
8use rustc_middle::ty::{self, GenericArgs, TyCtxt};
9use rustc_span::DUMMY_SP;
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    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    debug!("dtorck_constraint: {:?}", def);
38
39    if def.is_manually_drop() {
40        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        assert_eq!(args.len(), 1);
46        let result = DropckConstraint {
47            outlives: vec![],
48            dtorck_types: vec![args.type_at(0)],
49            overflows: vec![],
50        };
51        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    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}