]> git.lizzy.rs Git - rust.git/blob - src/librustc/traits/query/dropck_outlives.rs
Ignore new test on Windows
[rust.git] / src / librustc / traits / query / dropck_outlives.rs
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.
4 //
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.
10
11 use infer::at::At;
12 use infer::InferOk;
13 use smallvec::SmallVec;
14 use std::iter::FromIterator;
15 use syntax::source_map::Span;
16 use ty::subst::Kind;
17 use ty::{self, Ty, TyCtxt};
18
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
25     /// within.
26     ///
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]`).
34     ///
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>>> {
38         debug!(
39             "dropck_outlives(ty={:?}, param_env={:?})",
40             ty, self.param_env,
41         );
42
43         // Quick check: there are a number of cases that we know do not require
44         // any destructor.
45         let tcx = self.infcx.tcx;
46         if trivial_dropck_outlives(tcx, ty) {
47             return InferOk {
48                 value: vec![],
49                 obligations: vec![],
50             };
51         }
52
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(
61                     self.cause,
62                     self.param_env,
63                     &orig_values,
64                     result,
65                 ) {
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);
69                         return InferOk {
70                             value: kinds,
71                             obligations,
72                         };
73                     }
74
75                     Err(_) => { /* fallthrough to error-handling code below */ }
76                 }
77             }
78
79             _ => { /* fallthrough to error-handling code below */ }
80         }
81
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.
86         tcx.sess
87             .delay_span_bug(span, "dtorck encountered internal error");
88         return InferOk {
89             value: vec![],
90             obligations: vec![],
91         };
92     }
93 }
94
95 #[derive(Clone, Debug, Default)]
96 pub struct DropckOutlivesResult<'tcx> {
97     pub kinds: Vec<Kind<'tcx>>,
98     pub overflows: Vec<Ty<'tcx>>,
99 }
100
101 impl<'tcx> DropckOutlivesResult<'tcx> {
102     pub fn report_overflows(
103         &self,
104         tcx: TyCtxt<'_, '_, 'tcx>,
105         span: Span,
106         ty: Ty<'tcx>,
107     ) {
108         for overflow_ty in self.overflows.iter().take(1) {
109             let mut err = struct_span_err!(
110                 tcx.sess,
111                 span,
112                 E0320,
113                 "overflow while adding drop-check rules for {}",
114                 ty,
115             );
116             err.note(&format!("overflowed on {}", overflow_ty));
117             err.emit();
118         }
119     }
120
121     pub fn into_kinds_reporting_overflows(
122         self,
123         tcx: TyCtxt<'_, '_, 'tcx>,
124         span: Span,
125         ty: Ty<'tcx>,
126     ) -> Vec<Kind<'tcx>> {
127         self.report_overflows(tcx, span, ty);
128         let DropckOutlivesResult { kinds, overflows: _ } = self;
129         kinds
130     }
131 }
132
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>>,
140
141     /// Types that could not be resolved: projections and params.
142     pub dtorck_types: Vec<Ty<'tcx>>,
143
144     /// If, during the computation of the dtorck constraint, we
145     /// overflow, that gets recorded here. The caller is expected to
146     /// report an error.
147     pub overflows: Vec<Ty<'tcx>>,
148 }
149
150 impl<'tcx> DtorckConstraint<'tcx> {
151     pub fn empty() -> DtorckConstraint<'tcx> {
152         DtorckConstraint {
153             outlives: vec![],
154             dtorck_types: vec![],
155             overflows: vec![],
156         }
157     }
158 }
159
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();
163
164         for DtorckConstraint {
165             outlives,
166             dtorck_types,
167             overflows,
168         } in iter
169         {
170             result.outlives.extend(outlives);
171             result.dtorck_types.extend(dtorck_types);
172             result.overflows.extend(overflows);
173         }
174
175         result
176     }
177 }
178 BraceStructTypeFoldableImpl! {
179     impl<'tcx> TypeFoldable<'tcx> for DropckOutlivesResult<'tcx> {
180         kinds, overflows
181     }
182 }
183
184 BraceStructLiftImpl! {
185     impl<'a, 'tcx> Lift<'tcx> for DropckOutlivesResult<'a> {
186         type Lifted = DropckOutlivesResult<'tcx>;
187         kinds, overflows
188     }
189 }
190
191 impl_stable_hash_for!(struct DropckOutlivesResult<'tcx> {
192     kinds, overflows
193 });
194
195 impl_stable_hash_for!(struct DtorckConstraint<'tcx> {
196     outlives,
197     dtorck_types,
198     overflows
199 });
200
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.
209 ///
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 {
213     match ty.sty {
214         // None of these types have a destructor and hence they do not
215         // require anything in particular to outlive the dtor's
216         // execution.
217         ty::Infer(ty::FreshIntTy(_))
218         | ty::Infer(ty::FreshFloatTy(_))
219         | ty::Bool
220         | ty::Int(_)
221         | ty::Uint(_)
222         | ty::Float(_)
223         | ty::Never
224         | ty::FnDef(..)
225         | ty::FnPtr(_)
226         | ty::Char
227         | ty::GeneratorWitness(..)
228         | ty::RawPtr(_)
229         | ty::Ref(..)
230         | ty::Str
231         | ty::Foreign(..)
232         | ty::Error => true,
233
234         // [T; N] and [T] have same properties as T.
235         ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, ty),
236
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)),
243
244         ty::Adt(def, _) => {
245             if Some(def.did) == tcx.lang_items().manually_drop() {
246                 // `ManuallyDrop` never has a dtor.
247                 true
248             } else {
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.
253                 false
254             }
255         }
256
257         // The following *might* require a destructor: it would deeper inspection to tell.
258         ty::Dynamic(..)
259         | ty::Projection(..)
260         | ty::Param(_)
261         | ty::Opaque(..)
262         | ty::Infer(_)
263         | ty::Generator(..) => false,
264     }
265 }