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::GenericArg;
7 use crate::ty::{self, Ty, TyCtxt};
9 use rustc_error_codes::*;
11 impl<'cx, 'tcx> At<'cx, 'tcx> {
12 /// Given a type `ty` of some value being dropped, computes a set
13 /// of "kinds" (types, regions) that must be outlive the execution
14 /// of the destructor. These basically correspond to data that the
15 /// destructor might access. This is used during regionck to
16 /// impose "outlives" constraints on any lifetimes referenced
19 /// The rules here are given by the "dropck" RFCs, notably [#1238]
20 /// and [#1327]. This is a fixed-point computation, where we
21 /// explore all the data that will be dropped (transitively) when
22 /// a value of type `ty` is dropped. For each type T that will be
23 /// dropped and which has a destructor, we must assume that all
24 /// the types/regions of T are live during the destructor, unless
25 /// they are marked with a special attribute (`#[may_dangle]`).
27 /// [#1238]: https://github.com/rust-lang/rfcs/blob/master/text/1238-nonparametric-dropck.md
28 /// [#1327]: https://github.com/rust-lang/rfcs/blob/master/text/1327-dropck-param-eyepatch.md
29 pub fn dropck_outlives(&self, ty: Ty<'tcx>) -> InferOk<'tcx, Vec<GenericArg<'tcx>>> {
31 "dropck_outlives(ty={:?}, param_env={:?})",
35 // Quick check: there are a number of cases that we know do not require
37 let tcx = self.infcx.tcx;
38 if trivial_dropck_outlives(tcx, ty) {
45 let mut orig_values = OriginalQueryValues::default();
46 let c_ty = self.infcx.canonicalize_query(&self.param_env.and(ty), &mut orig_values);
47 let span = self.cause.span;
48 debug!("c_ty = {:?}", c_ty);
49 if let Ok(result) = &tcx.dropck_outlives(c_ty) {
50 if result.is_proven() {
51 if let Ok(InferOk { value, obligations }) =
52 self.infcx.instantiate_query_response_and_region_obligations(
58 let ty = self.infcx.resolve_vars_if_possible(&ty);
59 let kinds = value.into_kinds_reporting_overflows(tcx, span, ty);
68 // Errors and ambiuity in dropck occur in two cases:
69 // - unresolved inference variables at the end of typeck
70 // - non well-formed types where projections cannot be resolved
71 // Either of these should have created an error before.
73 .delay_span_bug(span, "dtorck encountered internal error");
82 #[derive(Clone, Debug, Default)]
83 pub struct DropckOutlivesResult<'tcx> {
84 pub kinds: Vec<GenericArg<'tcx>>,
85 pub overflows: Vec<Ty<'tcx>>,
88 impl<'tcx> DropckOutlivesResult<'tcx> {
89 pub fn report_overflows(&self, tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
90 if let Some(overflow_ty) = self.overflows.iter().next() {
91 let mut err = struct_span_err!(
95 "overflow while adding drop-check rules for {}",
98 err.note(&format!("overflowed on {}", overflow_ty));
103 pub fn into_kinds_reporting_overflows(
108 ) -> Vec<GenericArg<'tcx>> {
109 self.report_overflows(tcx, span, ty);
110 let DropckOutlivesResult { kinds, overflows: _ } = self;
115 /// A set of constraints that need to be satisfied in order for
116 /// a type to be valid for destruction.
117 #[derive(Clone, Debug)]
118 pub struct DtorckConstraint<'tcx> {
119 /// Types that are required to be alive in order for this
120 /// type to be valid for destruction.
121 pub outlives: Vec<ty::subst::GenericArg<'tcx>>,
123 /// Types that could not be resolved: projections and params.
124 pub dtorck_types: Vec<Ty<'tcx>>,
126 /// If, during the computation of the dtorck constraint, we
127 /// overflow, that gets recorded here. The caller is expected to
129 pub overflows: Vec<Ty<'tcx>>,
132 impl<'tcx> DtorckConstraint<'tcx> {
133 pub fn empty() -> DtorckConstraint<'tcx> {
136 dtorck_types: vec![],
142 impl<'tcx> FromIterator<DtorckConstraint<'tcx>> for DtorckConstraint<'tcx> {
143 fn from_iter<I: IntoIterator<Item = DtorckConstraint<'tcx>>>(iter: I) -> Self {
144 let mut result = Self::empty();
146 for DtorckConstraint { outlives, dtorck_types, overflows } in iter {
147 result.outlives.extend(outlives);
148 result.dtorck_types.extend(dtorck_types);
149 result.overflows.extend(overflows);
155 BraceStructTypeFoldableImpl! {
156 impl<'tcx> TypeFoldable<'tcx> for DropckOutlivesResult<'tcx> {
161 BraceStructLiftImpl! {
162 impl<'a, 'tcx> Lift<'tcx> for DropckOutlivesResult<'a> {
163 type Lifted = DropckOutlivesResult<'tcx>;
168 impl_stable_hash_for!(struct DropckOutlivesResult<'tcx> {
172 impl_stable_hash_for!(struct DtorckConstraint<'tcx> {
178 /// This returns true if the type `ty` is "trivial" for
179 /// dropck-outlives -- that is, if it doesn't require any types to
180 /// outlive. This is similar but not *quite* the same as the
181 /// `needs_drop` test in the compiler already -- that is, for every
182 /// type T for which this function return true, needs-drop would
183 /// return `false`. But the reverse does not hold: in particular,
184 /// `needs_drop` returns false for `PhantomData`, but it is not
185 /// trivial for dropck-outlives.
187 /// Note also that `needs_drop` requires a "global" type (i.e., one
188 /// with erased regions), but this function does not.
189 pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
191 // None of these types have a destructor and hence they do not
192 // require anything in particular to outlive the dtor's
194 ty::Infer(ty::FreshIntTy(_))
195 | ty::Infer(ty::FreshFloatTy(_))
204 | ty::GeneratorWitness(..)
211 // [T; N] and [T] have same properties as T.
212 ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, ty),
214 // (T1..Tn) and closures have same properties as T1..Tn --
215 // check if *any* of those are trivial.
216 ty::Tuple(ref tys) => tys.iter().all(|t| trivial_dropck_outlives(tcx, t.expect_ty())),
217 ty::Closure(def_id, ref substs) => substs
219 .upvar_tys(def_id, tcx)
220 .all(|t| trivial_dropck_outlives(tcx, t)),
223 if Some(def.did) == tcx.lang_items().manually_drop() {
224 // `ManuallyDrop` never has a dtor.
227 // Other types might. Moreover, PhantomData doesn't
228 // have a dtor, but it is considered to own its
229 // content, so it is non-trivial. Unions can have `impl Drop`,
230 // and hence are non-trivial as well.
235 // The following *might* require a destructor: needs deeper inspection.
240 | ty::Placeholder(..)
243 | ty::Generator(..) => false,
245 ty::UnnormalizedProjection(..) => bug!("only used with chalk-engine"),