1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use smallvec::SmallVec;
14 use std::iter::FromIterator;
15 use syntax::source_map::Span;
17 use ty::{self, Ty, TyCtxt};
19 impl<'cx, 'gcx, 'tcx> At<'cx, 'gcx, 'tcx> {
20 /// Given a type `ty` of some value being dropped, computes a set
21 /// of "kinds" (types, regions) that must be outlive the execution
22 /// of the destructor. These basically correspond to data that the
23 /// destructor might access. This is used during regionck to
24 /// impose "outlives" constraints on any lifetimes referenced
27 /// The rules here are given by the "dropck" RFCs, notably [#1238]
28 /// and [#1327]. This is a fixed-point computation, where we
29 /// explore all the data that will be dropped (transitively) when
30 /// a value of type `ty` is dropped. For each type T that will be
31 /// dropped and which has a destructor, we must assume that all
32 /// the types/regions of T are live during the destructor, unless
33 /// they are marked with a special attribute (`#[may_dangle]`).
35 /// [#1238]: https://github.com/rust-lang/rfcs/blob/master/text/1238-nonparametric-dropck.md
36 /// [#1327]: https://github.com/rust-lang/rfcs/blob/master/text/1327-dropck-param-eyepatch.md
37 pub fn dropck_outlives(&self, ty: Ty<'tcx>) -> InferOk<'tcx, Vec<Kind<'tcx>>> {
39 "dropck_outlives(ty={:?}, param_env={:?})",
43 // Quick check: there are a number of cases that we know do not require
45 let tcx = self.infcx.tcx;
46 if trivial_dropck_outlives(tcx, ty) {
53 let gcx = tcx.global_tcx();
54 let mut orig_values = SmallVec::new();
55 let c_ty = self.infcx.canonicalize_query(&self.param_env.and(ty), &mut orig_values);
56 let span = self.cause.span;
57 debug!("c_ty = {:?}", c_ty);
58 match &gcx.dropck_outlives(c_ty) {
59 Ok(result) if result.is_proven() => {
60 match self.infcx.instantiate_query_result_and_region_obligations(
66 Ok(InferOk { value, obligations }) => {
67 let ty = self.infcx.resolve_type_vars_if_possible(&ty);
68 let kinds = value.into_kinds_reporting_overflows(tcx, span, ty);
75 Err(_) => { /* fallthrough to error-handling code below */ }
79 _ => { /* fallthrough to error-handling code below */ }
82 // Errors and ambiuity in dropck occur in two cases:
83 // - unresolved inference variables at the end of typeck
84 // - non well-formed types where projections cannot be resolved
85 // Either of these should have created an error before.
87 .delay_span_bug(span, "dtorck encountered internal error");
95 #[derive(Clone, Debug, Default)]
96 pub struct DropckOutlivesResult<'tcx> {
97 pub kinds: Vec<Kind<'tcx>>,
98 pub overflows: Vec<Ty<'tcx>>,
101 impl<'tcx> DropckOutlivesResult<'tcx> {
102 pub fn report_overflows(
104 tcx: TyCtxt<'_, '_, 'tcx>,
108 for overflow_ty in self.overflows.iter().take(1) {
109 let mut err = struct_span_err!(
113 "overflow while adding drop-check rules for {}",
116 err.note(&format!("overflowed on {}", overflow_ty));
121 pub fn into_kinds_reporting_overflows(
123 tcx: TyCtxt<'_, '_, 'tcx>,
126 ) -> Vec<Kind<'tcx>> {
127 self.report_overflows(tcx, span, ty);
128 let DropckOutlivesResult { kinds, overflows: _ } = self;
133 /// A set of constraints that need to be satisfied in order for
134 /// a type to be valid for destruction.
135 #[derive(Clone, Debug)]
136 pub struct DtorckConstraint<'tcx> {
137 /// Types that are required to be alive in order for this
138 /// type to be valid for destruction.
139 pub outlives: Vec<ty::subst::Kind<'tcx>>,
141 /// Types that could not be resolved: projections and params.
142 pub dtorck_types: Vec<Ty<'tcx>>,
144 /// If, during the computation of the dtorck constraint, we
145 /// overflow, that gets recorded here. The caller is expected to
147 pub overflows: Vec<Ty<'tcx>>,
150 impl<'tcx> DtorckConstraint<'tcx> {
151 pub fn empty() -> DtorckConstraint<'tcx> {
154 dtorck_types: vec![],
160 impl<'tcx> FromIterator<DtorckConstraint<'tcx>> for DtorckConstraint<'tcx> {
161 fn from_iter<I: IntoIterator<Item = DtorckConstraint<'tcx>>>(iter: I) -> Self {
162 let mut result = Self::empty();
164 for DtorckConstraint {
170 result.outlives.extend(outlives);
171 result.dtorck_types.extend(dtorck_types);
172 result.overflows.extend(overflows);
178 BraceStructTypeFoldableImpl! {
179 impl<'tcx> TypeFoldable<'tcx> for DropckOutlivesResult<'tcx> {
184 BraceStructLiftImpl! {
185 impl<'a, 'tcx> Lift<'tcx> for DropckOutlivesResult<'a> {
186 type Lifted = DropckOutlivesResult<'tcx>;
191 impl_stable_hash_for!(struct DropckOutlivesResult<'tcx> {
195 impl_stable_hash_for!(struct DtorckConstraint<'tcx> {
201 /// This returns true if the type `ty` is "trivial" for
202 /// dropck-outlives -- that is, if it doesn't require any types to
203 /// outlive. This is similar but not *quite* the same as the
204 /// `needs_drop` test in the compiler already -- that is, for every
205 /// type T for which this function return true, needs-drop would
206 /// return false. But the reverse does not hold: in particular,
207 /// `needs_drop` returns false for `PhantomData`, but it is not
208 /// trivial for dropck-outlives.
210 /// Note also that `needs_drop` requires a "global" type (i.e., one
211 /// with erased regions), but this funtcion does not.
212 pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool {
214 // None of these types have a destructor and hence they do not
215 // require anything in particular to outlive the dtor's
217 ty::Infer(ty::FreshIntTy(_))
218 | ty::Infer(ty::FreshFloatTy(_))
227 | ty::GeneratorWitness(..)
234 // [T; N] and [T] have same properties as T.
235 ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, ty),
237 // (T1..Tn) and closures have same properties as T1..Tn --
238 // check if *any* of those are trivial.
239 ty::Tuple(ref tys) => tys.iter().cloned().all(|t| trivial_dropck_outlives(tcx, t)),
240 ty::Closure(def_id, ref substs) => substs
241 .upvar_tys(def_id, tcx)
242 .all(|t| trivial_dropck_outlives(tcx, t)),
245 if Some(def.did) == tcx.lang_items().manually_drop() {
246 // `ManuallyDrop` never has a dtor.
249 // Other types might. Moreover, PhantomData doesn't
250 // have a dtor, but it is considered to own its
251 // content, so it is non-trivial. Unions can have `impl Drop`,
252 // and hence are non-trivial as well.
257 // The following *might* require a destructor: it would deeper inspection to tell.
263 | ty::Generator(..) => false,