2018-02-21 09:55:16 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use rustc::hir::def_id::DefId;
|
2018-09-24 14:27:47 -05:00
|
|
|
use rustc::infer::canonical::{Canonical, QueryResponse};
|
2018-06-08 08:29:41 -05:00
|
|
|
use rustc::traits::query::dropck_outlives::{DropckOutlivesResult, DtorckConstraint};
|
2018-02-21 09:55:16 -06:00
|
|
|
use rustc::traits::query::{CanonicalTyGoal, NoSolution};
|
2018-06-27 15:04:32 -05:00
|
|
|
use rustc::traits::{FulfillmentContext, Normalized, ObligationCause, TraitEngineExt};
|
2018-06-27 08:42:00 -05:00
|
|
|
use rustc::ty::query::Providers;
|
2018-05-15 07:48:04 -05:00
|
|
|
use rustc::ty::subst::{Subst, Substs};
|
2018-06-08 08:29:41 -05:00
|
|
|
use rustc::ty::{self, ParamEnvAnd, Ty, TyCtxt};
|
2018-02-21 09:55:16 -06:00
|
|
|
use rustc::util::nodemap::FxHashSet;
|
2018-03-14 14:11:23 -05:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2018-08-18 05:14:03 -05:00
|
|
|
use syntax::source_map::{Span, DUMMY_SP};
|
2018-02-21 09:55:16 -06:00
|
|
|
|
2018-06-27 08:42:00 -05:00
|
|
|
crate fn provide(p: &mut Providers) {
|
|
|
|
*p = Providers {
|
|
|
|
dropck_outlives,
|
|
|
|
adt_dtorck_constraint,
|
|
|
|
..*p
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dropck_outlives<'tcx>(
|
2018-02-21 09:55:16 -06:00
|
|
|
tcx: TyCtxt<'_, 'tcx, 'tcx>,
|
2018-09-24 14:15:25 -05:00
|
|
|
canonical_goal: CanonicalTyGoal<'tcx>,
|
2018-09-24 14:27:47 -05:00
|
|
|
) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, DropckOutlivesResult<'tcx>>>>, NoSolution> {
|
2018-09-24 14:15:25 -05:00
|
|
|
debug!("dropck_outlives(goal={:#?})", canonical_goal);
|
2018-02-21 09:55:16 -06:00
|
|
|
|
2018-09-24 14:15:25 -05:00
|
|
|
tcx.infer_ctxt().enter_with_canonical(DUMMY_SP, &canonical_goal, |ref infcx, goal, canonical_inference_vars| {
|
2018-02-21 09:55:16 -06:00
|
|
|
let tcx = infcx.tcx;
|
2018-09-24 14:15:25 -05:00
|
|
|
let ParamEnvAnd {
|
|
|
|
param_env,
|
|
|
|
value: for_ty,
|
|
|
|
} = goal;
|
2018-02-21 09:55:16 -06:00
|
|
|
|
2018-06-08 08:29:41 -05:00
|
|
|
let mut result = DropckOutlivesResult {
|
|
|
|
kinds: vec![],
|
|
|
|
overflows: vec![],
|
|
|
|
};
|
2018-02-21 09:55:16 -06: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
|
2018-08-19 08:30:23 -05:00
|
|
|
// infinite recursion, we notice that `A` was already pushed
|
2018-02-21 09:55:16 -06:00
|
|
|
// once and stop.
|
|
|
|
let mut ty_stack = vec![(for_ty, 0)];
|
|
|
|
|
|
|
|
// Set used to detect infinite recursion.
|
|
|
|
let mut ty_set = FxHashSet();
|
|
|
|
|
|
|
|
let fulfill_cx = &mut FulfillmentContext::new();
|
|
|
|
|
|
|
|
let cause = ObligationCause::dummy();
|
|
|
|
while let Some((ty, depth)) = ty_stack.pop() {
|
|
|
|
let DtorckConstraint {
|
|
|
|
dtorck_types,
|
|
|
|
outlives,
|
|
|
|
overflows,
|
|
|
|
} = dtorck_constraint_for_ty(tcx, DUMMY_SP, for_ty, depth, ty)?;
|
|
|
|
|
|
|
|
// "outlives" represent types/regions that may be touched
|
|
|
|
// by a destructor.
|
|
|
|
result.kinds.extend(outlives);
|
|
|
|
result.overflows.extend(overflows);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
for ty in dtorck_types {
|
|
|
|
match infcx.at(&cause, param_env).normalize(&ty) {
|
|
|
|
Ok(Normalized {
|
|
|
|
value: ty,
|
|
|
|
obligations,
|
|
|
|
}) => {
|
|
|
|
fulfill_cx.register_predicate_obligations(infcx, obligations);
|
|
|
|
|
|
|
|
debug!("dropck_outlives: ty from dtorck_types = {:?}", ty);
|
|
|
|
|
|
|
|
match ty.sty {
|
|
|
|
// All parameters live for the duration of the
|
|
|
|
// function.
|
2018-08-21 19:35:29 -05:00
|
|
|
ty::Param(..) => {}
|
2018-02-21 09:55:16 -06:00
|
|
|
|
|
|
|
// A projection that we couldn't resolve - it
|
|
|
|
// might have a destructor.
|
2018-08-23 14:51:32 -05:00
|
|
|
ty::Projection(..) | ty::Opaque(..) => {
|
2018-02-21 09:55:16 -06:00
|
|
|
result.kinds.push(ty.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
if ty_set.insert(ty) {
|
|
|
|
ty_stack.push((ty, depth + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't actually expect to fail to normalize.
|
|
|
|
// That implies a WF error somewhere else.
|
|
|
|
Err(NoSolution) => {
|
|
|
|
return Err(NoSolution);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("dropck_outlives: result = {:#?}", result);
|
|
|
|
|
2018-09-24 14:27:47 -05:00
|
|
|
infcx.make_canonicalized_query_response(canonical_inference_vars, result, fulfill_cx)
|
2018-02-21 09:55:16 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a set of constraints that needs to be satisfied in
|
|
|
|
/// order for `ty` to be valid for destruction.
|
|
|
|
fn dtorck_constraint_for_ty<'a, 'gcx, 'tcx>(
|
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
span: Span,
|
|
|
|
for_ty: Ty<'tcx>,
|
|
|
|
depth: usize,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Result<DtorckConstraint<'tcx>, NoSolution> {
|
|
|
|
debug!(
|
|
|
|
"dtorck_constraint_for_ty({:?}, {:?}, {:?}, {:?})",
|
|
|
|
span, for_ty, depth, ty
|
|
|
|
);
|
|
|
|
|
2018-04-01 01:15:59 -05:00
|
|
|
if depth >= *tcx.sess.recursion_limit.get() {
|
2018-02-21 09:55:16 -06:00
|
|
|
return Ok(DtorckConstraint {
|
|
|
|
outlives: vec![],
|
|
|
|
dtorck_types: vec![],
|
|
|
|
overflows: vec![ty],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = match ty.sty {
|
2018-08-21 19:35:55 -05:00
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Str
|
2018-08-21 19:35:02 -05:00
|
|
|
| ty::Never
|
2018-08-21 19:35:29 -05:00
|
|
|
| ty::Foreign(..)
|
2018-08-21 19:35:02 -05:00
|
|
|
| ty::RawPtr(..)
|
|
|
|
| ty::Ref(..)
|
|
|
|
| ty::FnDef(..)
|
|
|
|
| ty::FnPtr(_)
|
|
|
|
| ty::GeneratorWitness(..) => {
|
2018-02-21 09:55:16 -06:00
|
|
|
// these types never have a destructor
|
|
|
|
Ok(DtorckConstraint::empty())
|
|
|
|
}
|
|
|
|
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Array(ety, _) | ty::Slice(ety) => {
|
2018-02-21 09:55:16 -06:00
|
|
|
// single-element containers, behave like their element
|
|
|
|
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ety)
|
|
|
|
}
|
|
|
|
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Tuple(tys) => tys
|
2018-06-08 08:29:41 -05:00
|
|
|
.iter()
|
2018-02-21 09:55:16 -06:00
|
|
|
.map(|ty| dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty))
|
|
|
|
.collect(),
|
|
|
|
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Closure(def_id, substs) => substs
|
2018-02-21 09:55:16 -06:00
|
|
|
.upvar_tys(def_id, tcx)
|
|
|
|
.map(|ty| dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty))
|
|
|
|
.collect(),
|
|
|
|
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Generator(def_id, substs, _movability) => {
|
2018-04-13 10:11:53 -05: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-21 19:35:02 -05:00
|
|
|
// GeneratorWitness that explicitly assume they are not
|
2018-04-13 10:11:53 -05: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
|
|
|
|
// derived from lifetimes attached to the upvars, and we
|
|
|
|
// *do* incorporate the upvars here.
|
|
|
|
|
|
|
|
let constraint = DtorckConstraint {
|
|
|
|
outlives: substs.upvar_tys(def_id, tcx).map(|t| t.into()).collect(),
|
|
|
|
dtorck_types: vec![],
|
|
|
|
overflows: vec![],
|
|
|
|
};
|
2018-06-08 08:29:41 -05:00
|
|
|
debug!(
|
|
|
|
"dtorck_constraint: generator {:?} => {:?}",
|
|
|
|
def_id, constraint
|
|
|
|
);
|
2018-04-13 10:11:53 -05:00
|
|
|
|
|
|
|
Ok(constraint)
|
2018-02-21 09:55:16 -06:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Adt(def, substs) => {
|
2018-02-21 09:55:16 -06:00
|
|
|
let DtorckConstraint {
|
|
|
|
dtorck_types,
|
|
|
|
outlives,
|
|
|
|
overflows,
|
|
|
|
} = tcx.at(span).adt_dtorck_constraint(def.did)?;
|
|
|
|
Ok(DtorckConstraint {
|
|
|
|
// FIXME: we can try to recursively `dtorck_constraint_on_ty`
|
|
|
|
// there, but that needs some way to handle cycles.
|
|
|
|
dtorck_types: dtorck_types.subst(tcx, substs),
|
|
|
|
outlives: outlives.subst(tcx, substs),
|
|
|
|
overflows: overflows.subst(tcx, substs),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Objects must be alive in order for their destructor
|
|
|
|
// to be called.
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Dynamic(..) => Ok(DtorckConstraint {
|
2018-02-21 09:55:16 -06:00
|
|
|
outlives: vec![ty.into()],
|
|
|
|
dtorck_types: vec![],
|
|
|
|
overflows: vec![],
|
|
|
|
}),
|
|
|
|
|
|
|
|
// Types that can't be resolved. Pass them forward.
|
2018-08-23 14:51:32 -05:00
|
|
|
ty::Projection(..) | ty::Opaque(..) | ty::Param(..) => Ok(DtorckConstraint {
|
2018-02-21 09:55:16 -06:00
|
|
|
outlives: vec![],
|
|
|
|
dtorck_types: vec![ty],
|
|
|
|
overflows: vec![],
|
|
|
|
}),
|
|
|
|
|
2018-10-03 10:06:28 -05:00
|
|
|
ty::UnnormalizedProjection(..) => bug!("only used with chalk-engine"),
|
|
|
|
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Infer(..) | ty::Error => {
|
2018-02-21 09:55:16 -06:00
|
|
|
// By the time this code runs, all type variables ought to
|
|
|
|
// be fully resolved.
|
|
|
|
Err(NoSolution)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("dtorck_constraint_for_ty({:?}) = {:?}", ty, result);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculates the dtorck constraint for a type.
|
|
|
|
crate fn adt_dtorck_constraint<'a, 'tcx>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> Result<DtorckConstraint<'tcx>, NoSolution> {
|
|
|
|
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 07:35:53 -05:00
|
|
|
// The first generic parameter here is guaranteed to be a type because it's
|
|
|
|
// `PhantomData`.
|
2018-05-15 07:48:04 -05:00
|
|
|
let substs = Substs::identity_for_item(tcx, def_id);
|
|
|
|
assert_eq!(substs.len(), 1);
|
2018-02-21 09:55:16 -06:00
|
|
|
let result = DtorckConstraint {
|
|
|
|
outlives: vec![],
|
2018-05-15 07:48:04 -05:00
|
|
|
dtorck_types: vec![substs.type_at(0)],
|
2018-02-21 09:55:16 -06:00
|
|
|
overflows: vec![],
|
|
|
|
};
|
|
|
|
debug!("dtorck_constraint: {:?} => {:?}", def, result);
|
|
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:29:41 -05:00
|
|
|
let mut result = def
|
|
|
|
.all_fields()
|
2018-02-21 09:55:16 -06:00
|
|
|
.map(|field| tcx.type_of(field.did))
|
|
|
|
.map(|fty| dtorck_constraint_for_ty(tcx, span, fty, 0, fty))
|
|
|
|
.collect::<Result<DtorckConstraint, NoSolution>>()?;
|
|
|
|
result.outlives.extend(tcx.destructor_constraints(def));
|
|
|
|
dedup_dtorck_constraint(&mut result);
|
|
|
|
|
|
|
|
debug!("dtorck_constraint: {:?} => {:?}", def, result);
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dedup_dtorck_constraint<'tcx>(c: &mut DtorckConstraint<'tcx>) {
|
|
|
|
let mut outlives = FxHashSet();
|
|
|
|
let mut dtorck_types = FxHashSet();
|
|
|
|
|
|
|
|
c.outlives.retain(|&val| outlives.replace(val).is_none());
|
|
|
|
c.dtorck_types
|
|
|
|
.retain(|&val| dtorck_types.replace(val).is_none());
|
|
|
|
}
|