2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::subst::{InternalSubsts, Subst};
|
|
|
|
use rustc_middle::ty::{self, ParamEnvAnd, Ty, TyCtxt};
|
2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-01-06 23:28:45 +01:00
|
|
|
use rustc_infer::infer::canonical::{Canonical, QueryResponse};
|
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
2020-02-11 21:19:40 +01:00
|
|
|
use rustc_infer::traits::TraitEngineExt as _;
|
2020-01-01 19:25:28 +01:00
|
|
|
use rustc_span::source_map::{Span, DUMMY_SP};
|
2020-02-11 21:19:40 +01:00
|
|
|
use rustc_trait_selection::traits::query::dropck_outlives::trivial_dropck_outlives;
|
|
|
|
use rustc_trait_selection::traits::query::dropck_outlives::{
|
|
|
|
DropckOutlivesResult, DtorckConstraint,
|
|
|
|
};
|
|
|
|
use rustc_trait_selection::traits::query::normalize::AtExt;
|
|
|
|
use rustc_trait_selection::traits::query::{CanonicalTyGoal, NoSolution};
|
|
|
|
use rustc_trait_selection::traits::{
|
|
|
|
Normalized, ObligationCause, TraitEngine, TraitEngineExt as _,
|
|
|
|
};
|
2018-02-21 10:55:16 -05:00
|
|
|
|
2019-02-07 10:17:48 +09:00
|
|
|
crate fn provide(p: &mut Providers<'_>) {
|
2019-12-22 17:42:04 -05:00
|
|
|
*p = Providers { dropck_outlives, adt_dtorck_constraint, ..*p };
|
2018-06-27 09:42:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dropck_outlives<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-09-24 15:15:25 -04:00
|
|
|
canonical_goal: CanonicalTyGoal<'tcx>,
|
2018-11-30 23:37:18 +01:00
|
|
|
) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, DropckOutlivesResult<'tcx>>>, NoSolution> {
|
2018-09-24 15:15:25 -04:00
|
|
|
debug!("dropck_outlives(goal={:#?})", canonical_goal);
|
2018-02-21 10:55:16 -05:00
|
|
|
|
2018-10-08 06:59:37 -04:00
|
|
|
tcx.infer_ctxt().enter_with_canonical(
|
|
|
|
DUMMY_SP,
|
|
|
|
&canonical_goal,
|
|
|
|
|ref infcx, goal, canonical_inference_vars| {
|
|
|
|
let tcx = infcx.tcx;
|
2019-12-22 17:42:04 -05:00
|
|
|
let ParamEnvAnd { param_env, value: for_ty } = goal;
|
2018-10-08 06:59:37 -04:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut result = DropckOutlivesResult { kinds: vec![], overflows: vec![] };
|
2018-02-21 10:55:16 -05:00
|
|
|
|
2018-10-08 06:59:37 -04:00
|
|
|
// A stack of types left to process. Each round, we pop
|
|
|
|
// something from the stack and invoke
|
|
|
|
// `dtorck_constraint_for_ty`. This may produce new types that
|
|
|
|
// have to be pushed on the stack. This continues until we have explored
|
|
|
|
// all the reachable types from the type `for_ty`.
|
|
|
|
//
|
|
|
|
// Example: Imagine that we have the following code:
|
|
|
|
//
|
|
|
|
// ```rust
|
|
|
|
// struct A {
|
|
|
|
// value: B,
|
|
|
|
// children: Vec<A>,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// struct B {
|
|
|
|
// value: u32
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fn f() {
|
|
|
|
// let a: A = ...;
|
|
|
|
// ..
|
|
|
|
// } // here, `a` is dropped
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// at the point where `a` is dropped, we need to figure out
|
|
|
|
// which types inside of `a` contain region data that may be
|
|
|
|
// accessed by any destructors in `a`. We begin by pushing `A`
|
|
|
|
// onto the stack, as that is the type of `a`. We will then
|
|
|
|
// invoke `dtorck_constraint_for_ty` which will expand `A`
|
|
|
|
// into the types of its fields `(B, Vec<A>)`. These will get
|
|
|
|
// pushed onto the stack. Eventually, expanding `Vec<A>` will
|
|
|
|
// lead to us trying to push `A` a second time -- to prevent
|
|
|
|
// infinite recursion, we notice that `A` was already pushed
|
|
|
|
// once and stop.
|
|
|
|
let mut ty_stack = vec![(for_ty, 0)];
|
|
|
|
|
|
|
|
// Set used to detect infinite recursion.
|
2018-10-16 10:44:26 +02:00
|
|
|
let mut ty_set = FxHashSet::default();
|
2018-10-08 06:59:37 -04:00
|
|
|
|
2018-11-28 23:29:01 +01:00
|
|
|
let mut fulfill_cx = TraitEngine::new(infcx.tcx);
|
2018-10-08 06:59:37 -04:00
|
|
|
|
|
|
|
let cause = ObligationCause::dummy();
|
2019-09-18 21:48:46 -04:00
|
|
|
let mut constraints = DtorckConstraint::empty();
|
2018-10-08 06:59:37 -04:00
|
|
|
while let Some((ty, depth)) = ty_stack.pop() {
|
2019-12-22 17:42:04 -05:00
|
|
|
info!(
|
|
|
|
"{} kinds, {} overflows, {} ty_stack",
|
|
|
|
result.kinds.len(),
|
|
|
|
result.overflows.len(),
|
|
|
|
ty_stack.len()
|
|
|
|
);
|
2019-09-18 21:48:46 -04:00
|
|
|
dtorck_constraint_for_ty(tcx, DUMMY_SP, for_ty, depth, ty, &mut constraints)?;
|
2018-10-08 06:59:37 -04:00
|
|
|
|
|
|
|
// "outlives" represent types/regions that may be touched
|
|
|
|
// by a destructor.
|
2019-09-18 21:48:46 -04:00
|
|
|
result.kinds.extend(constraints.outlives.drain(..));
|
|
|
|
result.overflows.extend(constraints.overflows.drain(..));
|
|
|
|
|
|
|
|
// If we have even one overflow, we should stop trying to evaluate further --
|
|
|
|
// chances are, the subsequent overflows for this evaluation won't provide useful
|
|
|
|
// information and will just decrease the speed at which we can emit these errors
|
|
|
|
// (since we'll be printing for just that much longer for the often enormous types
|
|
|
|
// that result here).
|
2020-02-28 14:20:33 +01:00
|
|
|
if !result.overflows.is_empty() {
|
2019-09-18 21:48:46 -04:00
|
|
|
break;
|
|
|
|
}
|
2018-10-08 06:59:37 -04:00
|
|
|
|
|
|
|
// dtorck types are "types that will get dropped but which
|
|
|
|
// do not themselves define a destructor", more or less. We have
|
|
|
|
// to push them onto the stack to be expanded.
|
2019-09-18 21:48:46 -04:00
|
|
|
for ty in constraints.dtorck_types.drain(..) {
|
2018-10-08 06:59:37 -04:00
|
|
|
match infcx.at(&cause, param_env).normalize(&ty) {
|
2019-12-22 17:42:04 -05:00
|
|
|
Ok(Normalized { value: ty, obligations }) => {
|
2018-10-08 06:59:37 -04:00
|
|
|
fulfill_cx.register_predicate_obligations(infcx, obligations);
|
|
|
|
|
|
|
|
debug!("dropck_outlives: ty from dtorck_types = {:?}", ty);
|
|
|
|
|
2019-09-16 19:08:35 +01:00
|
|
|
match ty.kind {
|
2018-10-08 06:59:37 -04:00
|
|
|
// All parameters live for the duration of the
|
|
|
|
// function.
|
|
|
|
ty::Param(..) => {}
|
|
|
|
|
|
|
|
// A projection that we couldn't resolve - it
|
|
|
|
// might have a destructor.
|
|
|
|
ty::Projection(..) | ty::Opaque(..) => {
|
|
|
|
result.kinds.push(ty.into());
|
|
|
|
}
|
2018-02-21 10:55:16 -05:00
|
|
|
|
2018-10-08 06:59:37 -04:00
|
|
|
_ => {
|
|
|
|
if ty_set.insert(ty) {
|
|
|
|
ty_stack.push((ty, depth + 1));
|
|
|
|
}
|
2018-02-21 10:55:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 06:59:37 -04:00
|
|
|
// We don't actually expect to fail to normalize.
|
|
|
|
// That implies a WF error somewhere else.
|
|
|
|
Err(NoSolution) => {
|
|
|
|
return Err(NoSolution);
|
|
|
|
}
|
2018-02-21 10:55:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 06:59:37 -04:00
|
|
|
debug!("dropck_outlives: result = {:#?}", result);
|
2018-02-21 10:55:16 -05:00
|
|
|
|
2018-11-28 23:29:01 +01:00
|
|
|
infcx.make_canonicalized_query_response(
|
|
|
|
canonical_inference_vars,
|
|
|
|
result,
|
2019-12-22 17:42:04 -05:00
|
|
|
&mut *fulfill_cx,
|
2018-11-28 23:29:01 +01:00
|
|
|
)
|
2018-10-08 06:59:37 -04:00
|
|
|
},
|
|
|
|
)
|
2018-02-21 10:55:16 -05:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns a set of constraints that needs to be satisfied in
|
2018-02-21 10:55:16 -05:00
|
|
|
/// order for `ty` to be valid for destruction.
|
2019-06-14 00:48:52 +03:00
|
|
|
fn dtorck_constraint_for_ty<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-02-21 10:55:16 -05:00
|
|
|
span: Span,
|
|
|
|
for_ty: Ty<'tcx>,
|
|
|
|
depth: usize,
|
|
|
|
ty: Ty<'tcx>,
|
2019-09-18 21:48:46 -04:00
|
|
|
constraints: &mut DtorckConstraint<'tcx>,
|
|
|
|
) -> Result<(), NoSolution> {
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!("dtorck_constraint_for_ty({:?}, {:?}, {:?}, {:?})", span, for_ty, depth, ty);
|
2018-02-21 10:55:16 -05:00
|
|
|
|
2018-04-01 08:15:59 +02:00
|
|
|
if depth >= *tcx.sess.recursion_limit.get() {
|
2019-09-18 21:48:46 -04:00
|
|
|
constraints.overflows.push(ty);
|
|
|
|
return Ok(());
|
2018-02-21 10:55:16 -05:00
|
|
|
}
|
|
|
|
|
2019-11-01 14:48:58 +11:00
|
|
|
if trivial_dropck_outlives(tcx, ty) {
|
2019-09-18 21:48:46 -04:00
|
|
|
return Ok(());
|
2019-09-18 19:33:31 -04:00
|
|
|
}
|
|
|
|
|
2019-09-18 21:48:46 -04:00
|
|
|
match ty.kind {
|
2018-08-22 01:35:55 +01:00
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Str
|
2018-08-22 01:35:02 +01:00
|
|
|
| ty::Never
|
2018-08-22 01:35:29 +01:00
|
|
|
| ty::Foreign(..)
|
2018-08-22 01:35:02 +01:00
|
|
|
| ty::RawPtr(..)
|
|
|
|
| ty::Ref(..)
|
|
|
|
| ty::FnDef(..)
|
|
|
|
| ty::FnPtr(_)
|
|
|
|
| ty::GeneratorWitness(..) => {
|
2018-02-21 10:55:16 -05:00
|
|
|
// these types never have a destructor
|
|
|
|
}
|
|
|
|
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Array(ety, _) | ty::Slice(ety) => {
|
2018-02-21 10:55:16 -05:00
|
|
|
// single-element containers, behave like their element
|
2019-09-18 21:48:46 -04:00
|
|
|
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ety, constraints)?;
|
2018-02-21 10:55:16 -05:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
ty::Tuple(tys) => {
|
|
|
|
for ty in tys.iter() {
|
|
|
|
dtorck_constraint_for_ty(
|
|
|
|
tcx,
|
|
|
|
span,
|
|
|
|
for_ty,
|
|
|
|
depth + 1,
|
|
|
|
ty.expect_ty(),
|
|
|
|
constraints,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
2018-02-21 10:55:16 -05:00
|
|
|
|
2020-03-13 03:23:38 +02:00
|
|
|
ty::Closure(_, substs) => {
|
|
|
|
for ty in substs.as_closure().upvar_tys() {
|
2019-12-22 17:42:04 -05:00
|
|
|
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty, constraints)?;
|
|
|
|
}
|
2019-09-18 21:48:46 -04:00
|
|
|
}
|
2018-02-21 10:55:16 -05:00
|
|
|
|
2020-03-13 03:23:38 +02:00
|
|
|
ty::Generator(_, substs, _movability) => {
|
2018-04-13 17:11:53 +02:00
|
|
|
// rust-lang/rust#49918: types can be constructed, stored
|
|
|
|
// in the interior, and sit idle when generator yields
|
|
|
|
// (and is subsequently dropped).
|
|
|
|
//
|
|
|
|
// It would be nice to descend into interior of a
|
|
|
|
// generator to determine what effects dropping it might
|
|
|
|
// have (by looking at any drop effects associated with
|
|
|
|
// its interior).
|
|
|
|
//
|
|
|
|
// However, the interior's representation uses things like
|
2018-08-22 01:35:02 +01:00
|
|
|
// GeneratorWitness that explicitly assume they are not
|
2018-04-13 17:11:53 +02:00
|
|
|
// traversed in such a manner. So instead, we will
|
|
|
|
// simplify things for now by treating all generators as
|
|
|
|
// if they were like trait objects, where its upvars must
|
|
|
|
// all be alive for the generator's (potential)
|
|
|
|
// destructor.
|
|
|
|
//
|
|
|
|
// In particular, skipping over `_interior` is safe
|
|
|
|
// because any side-effects from dropping `_interior` can
|
|
|
|
// only take place through references with lifetimes
|
2020-01-27 23:26:37 +01:00
|
|
|
// derived from lifetimes attached to the upvars and resume
|
|
|
|
// argument, and we *do* incorporate those here.
|
2018-04-13 17:11:53 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
constraints.outlives.extend(
|
|
|
|
substs
|
|
|
|
.as_generator()
|
2020-03-13 03:23:38 +02:00
|
|
|
.upvar_tys()
|
2019-12-22 17:42:04 -05:00
|
|
|
.map(|t| -> ty::subst::GenericArg<'tcx> { t.into() }),
|
|
|
|
);
|
2020-03-13 03:23:38 +02:00
|
|
|
constraints.outlives.push(substs.as_generator().resume_ty().into());
|
2018-02-21 10:55:16 -05:00
|
|
|
}
|
|
|
|
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Adt(def, substs) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
let DtorckConstraint { dtorck_types, outlives, overflows } =
|
|
|
|
tcx.at(span).adt_dtorck_constraint(def.did)?;
|
2019-09-18 21:48:46 -04:00
|
|
|
// FIXME: we can try to recursively `dtorck_constraint_on_ty`
|
|
|
|
// there, but that needs some way to handle cycles.
|
|
|
|
constraints.dtorck_types.extend(dtorck_types.subst(tcx, substs));
|
|
|
|
constraints.outlives.extend(outlives.subst(tcx, substs));
|
|
|
|
constraints.overflows.extend(overflows.subst(tcx, substs));
|
2018-02-21 10:55:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Objects must be alive in order for their destructor
|
|
|
|
// to be called.
|
2019-09-18 21:48:46 -04:00
|
|
|
ty::Dynamic(..) => {
|
|
|
|
constraints.outlives.push(ty.into());
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-02-21 10:55:16 -05:00
|
|
|
|
|
|
|
// Types that can't be resolved. Pass them forward.
|
2019-09-18 21:48:46 -04:00
|
|
|
ty::Projection(..) | ty::Opaque(..) | ty::Param(..) => {
|
|
|
|
constraints.dtorck_types.push(ty);
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-02-21 10:55:16 -05:00
|
|
|
|
2018-10-03 17:06:28 +02:00
|
|
|
ty::UnnormalizedProjection(..) => bug!("only used with chalk-engine"),
|
|
|
|
|
2018-11-02 18:48:24 +01:00
|
|
|
ty::Placeholder(..) | ty::Bound(..) | ty::Infer(..) | ty::Error => {
|
2018-02-21 10:55:16 -05:00
|
|
|
// By the time this code runs, all type variables ought to
|
|
|
|
// be fully resolved.
|
2019-12-22 17:42:04 -05:00
|
|
|
return Err(NoSolution);
|
2018-02-21 10:55:16 -05:00
|
|
|
}
|
2019-09-18 21:48:46 -04:00
|
|
|
}
|
2018-02-21 10:55:16 -05:00
|
|
|
|
2019-09-18 21:48:46 -04:00
|
|
|
Ok(())
|
2018-02-21 10:55:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculates the dtorck constraint for a type.
|
2019-06-21 23:49:03 +02:00
|
|
|
crate fn adt_dtorck_constraint(
|
|
|
|
tcx: TyCtxt<'_>,
|
2018-02-21 10:55:16 -05:00
|
|
|
def_id: DefId,
|
2019-06-21 23:49:03 +02:00
|
|
|
) -> Result<DtorckConstraint<'_>, NoSolution> {
|
2018-02-21 10:55:16 -05:00
|
|
|
let def = tcx.adt_def(def_id);
|
|
|
|
let span = tcx.def_span(def_id);
|
|
|
|
debug!("dtorck_constraint: {:?}", def);
|
|
|
|
|
|
|
|
if def.is_phantom_data() {
|
2018-05-15 13:35:53 +01:00
|
|
|
// The first generic parameter here is guaranteed to be a type because it's
|
|
|
|
// `PhantomData`.
|
2019-02-26 09:30:34 +08:00
|
|
|
let substs = InternalSubsts::identity_for_item(tcx, def_id);
|
2018-05-15 13:48:04 +01:00
|
|
|
assert_eq!(substs.len(), 1);
|
2018-02-21 10:55:16 -05:00
|
|
|
let result = DtorckConstraint {
|
|
|
|
outlives: vec![],
|
2018-05-15 13:48:04 +01:00
|
|
|
dtorck_types: vec![substs.type_at(0)],
|
2018-02-21 10:55:16 -05:00
|
|
|
overflows: vec![],
|
|
|
|
};
|
|
|
|
debug!("dtorck_constraint: {:?} => {:?}", def, result);
|
|
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
|
2019-09-18 21:48:46 -04:00
|
|
|
let mut result = DtorckConstraint::empty();
|
|
|
|
for field in def.all_fields() {
|
|
|
|
let fty = tcx.type_of(field.did);
|
|
|
|
dtorck_constraint_for_ty(tcx, span, fty, 0, fty, &mut result)?;
|
|
|
|
}
|
2018-02-21 10:55:16 -05:00
|
|
|
result.outlives.extend(tcx.destructor_constraints(def));
|
|
|
|
dedup_dtorck_constraint(&mut result);
|
|
|
|
|
|
|
|
debug!("dtorck_constraint: {:?} => {:?}", def, result);
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2019-06-21 23:49:03 +02:00
|
|
|
fn dedup_dtorck_constraint(c: &mut DtorckConstraint<'_>) {
|
2018-10-16 10:44:26 +02:00
|
|
|
let mut outlives = FxHashSet::default();
|
|
|
|
let mut dtorck_types = FxHashSet::default();
|
2018-02-21 10:55:16 -05:00
|
|
|
|
|
|
|
c.outlives.retain(|&val| outlives.replace(val).is_none());
|
2019-12-22 17:42:04 -05:00
|
|
|
c.dtorck_types.retain(|&val| dtorck_types.replace(val).is_none());
|
2018-02-21 10:55:16 -05:00
|
|
|
}
|