1 use crate::infer::at::At;
2 use crate::infer::InferOk;
3 use crate::infer::canonical::OriginalQueryValues;
4 use std::iter::FromIterator;
5 use syntax::source_map::Span;
6 use crate::ty::subst::Kind;
7 use crate::ty::{self, Ty, TyCtxt};
9 impl<'cx, 'gcx, 'tcx> At<'cx, 'gcx, 'tcx> {
10 /// Given a type `ty` of some value being dropped, computes a set
11 /// of "kinds" (types, regions) that must be outlive the execution
12 /// of the destructor. These basically correspond to data that the
13 /// destructor might access. This is used during regionck to
14 /// impose "outlives" constraints on any lifetimes referenced
17 /// The rules here are given by the "dropck" RFCs, notably [#1238]
18 /// and [#1327]. This is a fixed-point computation, where we
19 /// explore all the data that will be dropped (transitively) when
20 /// a value of type `ty` is dropped. For each type T that will be
21 /// dropped and which has a destructor, we must assume that all
22 /// the types/regions of T are live during the destructor, unless
23 /// they are marked with a special attribute (`#[may_dangle]`).
25 /// [#1238]: https://github.com/rust-lang/rfcs/blob/master/text/1238-nonparametric-dropck.md
26 /// [#1327]: https://github.com/rust-lang/rfcs/blob/master/text/1327-dropck-param-eyepatch.md
27 pub fn dropck_outlives(&self, ty: Ty<'tcx>) -> InferOk<'tcx, Vec<Kind<'tcx>>> {
29 "dropck_outlives(ty={:?}, param_env={:?})",
33 // Quick check: there are a number of cases that we know do not require
35 let tcx = self.infcx.tcx;
36 if trivial_dropck_outlives(tcx, ty) {
43 let gcx = tcx.global_tcx();
44 let mut orig_values = OriginalQueryValues::default();
45 let c_ty = self.infcx.canonicalize_query(&self.param_env.and(ty), &mut orig_values);
46 let span = self.cause.span;
47 debug!("c_ty = {:?}", c_ty);
48 if let Ok(result) = &gcx.dropck_outlives(c_ty) {
49 if result.is_proven() {
50 if let Ok(InferOk { value, obligations }) =
51 self.infcx.instantiate_query_response_and_region_obligations(
57 let ty = self.infcx.resolve_vars_if_possible(&ty);
58 let kinds = value.into_kinds_reporting_overflows(tcx, span, ty);
67 // Errors and ambiuity in dropck occur in two cases:
68 // - unresolved inference variables at the end of typeck
69 // - non well-formed types where projections cannot be resolved
70 // Either of these should have created an error before.
72 .delay_span_bug(span, "dtorck encountered internal error");
81 #[derive(Clone, Debug, Default)]
82 pub struct DropckOutlivesResult<'tcx> {
83 pub kinds: Vec<Kind<'tcx>>,
84 pub overflows: Vec<Ty<'tcx>>,
87 impl<'tcx> DropckOutlivesResult<'tcx> {
88 pub fn report_overflows(&self, tcx: TyCtxt<'_, 'tcx>, span: Span, ty: Ty<'tcx>) {
89 if let Some(overflow_ty) = self.overflows.iter().next() {
90 let mut err = struct_span_err!(
94 "overflow while adding drop-check rules for {}",
97 err.note(&format!("overflowed on {}", overflow_ty));
102 pub fn into_kinds_reporting_overflows(
104 tcx: TyCtxt<'_, 'tcx>,
107 ) -> Vec<Kind<'tcx>> {
108 self.report_overflows(tcx, span, ty);
109 let DropckOutlivesResult { kinds, overflows: _ } = self;
114 /// A set of constraints that need to be satisfied in order for
115 /// a type to be valid for destruction.
116 #[derive(Clone, Debug)]
117 pub struct DtorckConstraint<'tcx> {
118 /// Types that are required to be alive in order for this
119 /// type to be valid for destruction.
120 pub outlives: Vec<ty::subst::Kind<'tcx>>,
122 /// Types that could not be resolved: projections and params.
123 pub dtorck_types: Vec<Ty<'tcx>>,
125 /// If, during the computation of the dtorck constraint, we
126 /// overflow, that gets recorded here. The caller is expected to
128 pub overflows: Vec<Ty<'tcx>>,
131 impl<'tcx> DtorckConstraint<'tcx> {
132 pub fn empty() -> DtorckConstraint<'tcx> {
135 dtorck_types: vec![],
141 impl<'tcx> FromIterator<DtorckConstraint<'tcx>> for DtorckConstraint<'tcx> {
142 fn from_iter<I: IntoIterator<Item = DtorckConstraint<'tcx>>>(iter: I) -> Self {
143 let mut result = Self::empty();
145 for DtorckConstraint { outlives, dtorck_types, overflows } in iter {
146 result.outlives.extend(outlives);
147 result.dtorck_types.extend(dtorck_types);
148 result.overflows.extend(overflows);
154 BraceStructTypeFoldableImpl! {
155 impl<'tcx> TypeFoldable<'tcx> for DropckOutlivesResult<'tcx> {
160 BraceStructLiftImpl! {
161 impl<'a, 'tcx> Lift<'tcx> for DropckOutlivesResult<'a> {
162 type Lifted = DropckOutlivesResult<'tcx>;
167 impl_stable_hash_for!(struct DropckOutlivesResult<'tcx> {
171 impl_stable_hash_for!(struct DtorckConstraint<'tcx> {
177 /// This returns true if the type `ty` is "trivial" for
178 /// dropck-outlives -- that is, if it doesn't require any types to
179 /// outlive. This is similar but not *quite* the same as the
180 /// `needs_drop` test in the compiler already -- that is, for every
181 /// type T for which this function return true, needs-drop would
182 /// return `false`. But the reverse does not hold: in particular,
183 /// `needs_drop` returns false for `PhantomData`, but it is not
184 /// trivial for dropck-outlives.
186 /// Note also that `needs_drop` requires a "global" type (i.e., one
187 /// with erased regions), but this function does not.
188 pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
190 // None of these types have a destructor and hence they do not
191 // require anything in particular to outlive the dtor's
193 ty::Infer(ty::FreshIntTy(_))
194 | ty::Infer(ty::FreshFloatTy(_))
203 | ty::GeneratorWitness(..)
210 // [T; N] and [T] have same properties as T.
211 ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, ty),
213 // (T1..Tn) and closures have same properties as T1..Tn --
214 // check if *any* of those are trivial.
215 ty::Tuple(ref tys) => tys.iter().all(|t| trivial_dropck_outlives(tcx, t.expect_ty())),
216 ty::Closure(def_id, ref substs) => substs
217 .upvar_tys(def_id, tcx)
218 .all(|t| trivial_dropck_outlives(tcx, t)),
221 if Some(def.did) == tcx.lang_items().manually_drop() {
222 // `ManuallyDrop` never has a dtor.
225 // Other types might. Moreover, PhantomData doesn't
226 // have a dtor, but it is considered to own its
227 // content, so it is non-trivial. Unions can have `impl Drop`,
228 // and hence are non-trivial as well.
233 // The following *might* require a destructor: needs deeper inspection.
238 | ty::Placeholder(..)
241 | ty::Generator(..) => false,
243 ty::UnnormalizedProjection(..) => bug!("only used with chalk-engine"),