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
32pub(crate) fn adt_dtorck_constraint(
34 tcx: TyCtxt<'_>,
35 def_id: DefId,
36) -> Result<&DropckConstraint<'_>, NoSolution> {
37 let def = tcx.adt_def(def_id);
38 let span = tcx.def_span(def_id);
39 let typing_env = ty::TypingEnv::non_body_analysis(tcx, def_id);
40 debug!("dtorck_constraint: {:?}", def);
41
42 if def.is_manually_drop() {
43 bug!("`ManuallyDrop` should have been handled by `trivial_dropck_outlives`");
44 } else if def.is_phantom_data() {
45 let args = GenericArgs::identity_for_item(tcx, def_id);
48 assert_eq!(args.len(), 1);
49 let result = DropckConstraint {
50 outlives: vec![],
51 dtorck_types: vec![args.type_at(0)],
52 overflows: vec![],
53 };
54 debug!("dtorck_constraint: {:?} => {:?}", def, result);
55 return Ok(tcx.arena.alloc(result));
56 }
57
58 let mut result = DropckConstraint::empty();
59 for field in def.all_fields() {
60 let fty = tcx.type_of(field.did).instantiate_identity();
61 dtorck_constraint_for_ty_inner(tcx, typing_env, span, 0, fty, &mut result)?;
62 }
63 result.outlives.extend(tcx.destructor_constraints(def));
64 dedup_dtorck_constraint(&mut result);
65
66 debug!("dtorck_constraint: {:?} => {:?}", def, result);
67
68 Ok(tcx.arena.alloc(result))
69}
70
71fn dedup_dtorck_constraint(c: &mut DropckConstraint<'_>) {
72 let mut outlives = FxHashSet::default();
73 let mut dtorck_types = FxHashSet::default();
74
75 c.outlives.retain(|&val| outlives.replace(val).is_none());
76 c.dtorck_types.retain(|&val| dtorck_types.replace(val).is_none());
77}