]> git.lizzy.rs Git - rust.git/blob - src/librustc/traits/query/dropck_outlives.rs
Run `rustfmt --file-lines ...` for changes from previous commits.
[rust.git] / src / librustc / traits / query / dropck_outlives.rs
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};
8
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
15     /// within.
16     ///
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]`).
24     ///
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>>> {
28         debug!(
29             "dropck_outlives(ty={:?}, param_env={:?})",
30             ty, self.param_env,
31         );
32
33         // Quick check: there are a number of cases that we know do not require
34         // any destructor.
35         let tcx = self.infcx.tcx;
36         if trivial_dropck_outlives(tcx, ty) {
37             return InferOk {
38                 value: vec![],
39                 obligations: vec![],
40             };
41         }
42
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(
52                     self.cause,
53                     self.param_env,
54                     &orig_values,
55                     result)
56                 {
57                     let ty = self.infcx.resolve_vars_if_possible(&ty);
58                     let kinds = value.into_kinds_reporting_overflows(tcx, span, ty);
59                     return InferOk {
60                         value: kinds,
61                         obligations,
62                     };
63                 }
64             }
65         }
66
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.
71         tcx.sess
72             .delay_span_bug(span, "dtorck encountered internal error");
73
74         InferOk {
75             value: vec![],
76             obligations: vec![],
77         }
78     }
79 }
80
81 #[derive(Clone, Debug, Default)]
82 pub struct DropckOutlivesResult<'tcx> {
83     pub kinds: Vec<Kind<'tcx>>,
84     pub overflows: Vec<Ty<'tcx>>,
85 }
86
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!(
91                 tcx.sess,
92                 span,
93                 E0320,
94                 "overflow while adding drop-check rules for {}",
95                 ty,
96             );
97             err.note(&format!("overflowed on {}", overflow_ty));
98             err.emit();
99         }
100     }
101
102     pub fn into_kinds_reporting_overflows(
103         self,
104         tcx: TyCtxt<'_, 'tcx>,
105         span: Span,
106         ty: Ty<'tcx>,
107     ) -> Vec<Kind<'tcx>> {
108         self.report_overflows(tcx, span, ty);
109         let DropckOutlivesResult { kinds, overflows: _ } = self;
110         kinds
111     }
112 }
113
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>>,
121
122     /// Types that could not be resolved: projections and params.
123     pub dtorck_types: Vec<Ty<'tcx>>,
124
125     /// If, during the computation of the dtorck constraint, we
126     /// overflow, that gets recorded here. The caller is expected to
127     /// report an error.
128     pub overflows: Vec<Ty<'tcx>>,
129 }
130
131 impl<'tcx> DtorckConstraint<'tcx> {
132     pub fn empty() -> DtorckConstraint<'tcx> {
133         DtorckConstraint {
134             outlives: vec![],
135             dtorck_types: vec![],
136             overflows: vec![],
137         }
138     }
139 }
140
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();
144
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);
149         }
150
151         result
152     }
153 }
154 BraceStructTypeFoldableImpl! {
155     impl<'tcx> TypeFoldable<'tcx> for DropckOutlivesResult<'tcx> {
156         kinds, overflows
157     }
158 }
159
160 BraceStructLiftImpl! {
161     impl<'a, 'tcx> Lift<'tcx> for DropckOutlivesResult<'a> {
162         type Lifted = DropckOutlivesResult<'tcx>;
163         kinds, overflows
164     }
165 }
166
167 impl_stable_hash_for!(struct DropckOutlivesResult<'tcx> {
168     kinds, overflows
169 });
170
171 impl_stable_hash_for!(struct DtorckConstraint<'tcx> {
172     outlives,
173     dtorck_types,
174     overflows
175 });
176
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.
185 ///
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 {
189     match ty.sty {
190         // None of these types have a destructor and hence they do not
191         // require anything in particular to outlive the dtor's
192         // execution.
193         ty::Infer(ty::FreshIntTy(_))
194         | ty::Infer(ty::FreshFloatTy(_))
195         | ty::Bool
196         | ty::Int(_)
197         | ty::Uint(_)
198         | ty::Float(_)
199         | ty::Never
200         | ty::FnDef(..)
201         | ty::FnPtr(_)
202         | ty::Char
203         | ty::GeneratorWitness(..)
204         | ty::RawPtr(_)
205         | ty::Ref(..)
206         | ty::Str
207         | ty::Foreign(..)
208         | ty::Error => true,
209
210         // [T; N] and [T] have same properties as T.
211         ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, ty),
212
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)),
219
220         ty::Adt(def, _) => {
221             if Some(def.did) == tcx.lang_items().manually_drop() {
222                 // `ManuallyDrop` never has a dtor.
223                 true
224             } else {
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.
229                 false
230             }
231         }
232
233         // The following *might* require a destructor: needs deeper inspection.
234         ty::Dynamic(..)
235         | ty::Projection(..)
236         | ty::Param(_)
237         | ty::Opaque(..)
238         | ty::Placeholder(..)
239         | ty::Infer(_)
240         | ty::Bound(..)
241         | ty::Generator(..) => false,
242
243         ty::UnnormalizedProjection(..) => bug!("only used with chalk-engine"),
244     }
245 }