2019-12-24 04:02:53 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-01-06 22:28:45 +00:00
|
|
|
use rustc_infer::infer::canonical::{Canonical, QueryResponse};
|
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
2023-05-15 04:24:45 +00:00
|
|
|
use rustc_middle::query::Providers;
|
2023-05-25 18:25:44 +00:00
|
|
|
use rustc_middle::traits::query::{DropckConstraint, DropckOutlivesResult};
|
2022-09-16 13:31:10 +00:00
|
|
|
use rustc_middle::ty::InternalSubsts;
|
2023-05-25 18:25:44 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2022-11-09 18:46:57 +00:00
|
|
|
use rustc_trait_selection::infer::InferCtxtBuilderExt;
|
2020-02-11 20:19:40 +00:00
|
|
|
use rustc_trait_selection::traits::query::dropck_outlives::{
|
2023-05-25 18:25:44 +00:00
|
|
|
compute_dropck_outlives_inner, dtorck_constraint_for_ty_inner,
|
2020-02-11 20:19:40 +00:00
|
|
|
};
|
|
|
|
use rustc_trait_selection::traits::query::{CanonicalTyGoal, NoSolution};
|
2018-02-21 15:55:16 +00:00
|
|
|
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) fn provide(p: &mut Providers) {
|
2018-06-27 13:42:00 +00:00
|
|
|
*p = Providers { dropck_outlives, adt_dtorck_constraint, ..*p };
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dropck_outlives<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-09-24 19:15:25 +00:00
|
|
|
canonical_goal: CanonicalTyGoal<'tcx>,
|
2018-11-30 22:37:18 +00:00
|
|
|
) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, DropckOutlivesResult<'tcx>>>, NoSolution> {
|
2018-09-24 19:15:25 +00:00
|
|
|
debug!("dropck_outlives(goal={:#?})", canonical_goal);
|
2018-02-21 15:55:16 +00:00
|
|
|
|
2022-11-09 18:46:57 +00:00
|
|
|
tcx.infer_ctxt().enter_canonical_trait_query(&canonical_goal, |ocx, goal| {
|
2023-05-25 18:25:44 +00:00
|
|
|
compute_dropck_outlives_inner(ocx, goal)
|
2022-11-09 18:46:57 +00:00
|
|
|
})
|
2018-02-21 15:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculates the dtorck constraint for a type.
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) fn adt_dtorck_constraint(
|
2019-06-21 21:49:03 +00:00
|
|
|
tcx: TyCtxt<'_>,
|
2018-02-21 15:55:16 +00:00
|
|
|
def_id: DefId,
|
2022-03-24 18:21:41 +00:00
|
|
|
) -> Result<&DropckConstraint<'_>, NoSolution> {
|
2018-02-21 15:55:16 +00:00
|
|
|
let def = tcx.adt_def(def_id);
|
|
|
|
let span = tcx.def_span(def_id);
|
|
|
|
debug!("dtorck_constraint: {:?}", def);
|
|
|
|
|
2023-04-14 13:44:05 +00:00
|
|
|
if def.is_manually_drop() {
|
|
|
|
bug!("`ManuallyDrop` should have been handled by `trivial_dropck_outlives`");
|
|
|
|
} else if def.is_phantom_data() {
|
2018-05-15 12:35:53 +00:00
|
|
|
// The first generic parameter here is guaranteed to be a type because it's
|
|
|
|
// `PhantomData`.
|
2019-02-26 01:30:34 +00:00
|
|
|
let substs = InternalSubsts::identity_for_item(tcx, def_id);
|
2018-05-15 12:48:04 +00:00
|
|
|
assert_eq!(substs.len(), 1);
|
2022-03-24 18:21:41 +00:00
|
|
|
let result = DropckConstraint {
|
2018-02-21 15:55:16 +00:00
|
|
|
outlives: vec![],
|
2018-05-15 12:48:04 +00:00
|
|
|
dtorck_types: vec![substs.type_at(0)],
|
2018-02-21 15:55:16 +00:00
|
|
|
overflows: vec![],
|
|
|
|
};
|
|
|
|
debug!("dtorck_constraint: {:?} => {:?}", def, result);
|
2022-01-31 18:55:34 +00:00
|
|
|
return Ok(tcx.arena.alloc(result));
|
2018-02-21 15:55:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 18:21:41 +00:00
|
|
|
let mut result = DropckConstraint::empty();
|
2019-09-19 01:48:46 +00:00
|
|
|
for field in def.all_fields() {
|
2023-02-07 08:29:48 +00:00
|
|
|
let fty = tcx.type_of(field.did).subst_identity();
|
2023-05-25 18:25:44 +00:00
|
|
|
dtorck_constraint_for_ty_inner(tcx, span, fty, 0, fty, &mut result)?;
|
2019-09-19 01:48:46 +00:00
|
|
|
}
|
2018-02-21 15:55:16 +00:00
|
|
|
result.outlives.extend(tcx.destructor_constraints(def));
|
|
|
|
dedup_dtorck_constraint(&mut result);
|
|
|
|
|
|
|
|
debug!("dtorck_constraint: {:?} => {:?}", def, result);
|
|
|
|
|
2022-01-31 18:55:34 +00:00
|
|
|
Ok(tcx.arena.alloc(result))
|
2018-02-21 15:55:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 18:21:41 +00:00
|
|
|
fn dedup_dtorck_constraint(c: &mut DropckConstraint<'_>) {
|
2018-10-16 08:44:26 +00:00
|
|
|
let mut outlives = FxHashSet::default();
|
|
|
|
let mut dtorck_types = FxHashSet::default();
|
2018-02-21 15:55:16 +00:00
|
|
|
|
|
|
|
c.outlives.retain(|&val| outlives.replace(val).is_none());
|
|
|
|
c.dtorck_types.retain(|&val| dtorck_types.replace(val).is_none());
|
|
|
|
}
|