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