]>
Commit | Line | Data |
---|---|---|
9fa01778 | 1 | use crate::infer::at::At; |
9fa01778 | 2 | use crate::infer::canonical::OriginalQueryValues; |
dfeec247 | 3 | use crate::infer::InferOk; |
74b04a01 | 4 | |
ba9703b0 XL |
5 | use rustc_middle::ty::subst::GenericArg; |
6 | use rustc_middle::ty::{self, Ty, TyCtxt}; | |
74b04a01 | 7 | |
5e7ed085 | 8 | pub use rustc_middle::traits::query::{DropckConstraint, DropckOutlivesResult}; |
0531ce1d | 9 | |
ba9703b0 XL |
10 | pub trait AtExt<'tcx> { |
11 | fn dropck_outlives(&self, ty: Ty<'tcx>) -> InferOk<'tcx, Vec<GenericArg<'tcx>>>; | |
12 | } | |
13 | ||
14 | impl<'cx, 'tcx> AtExt<'tcx> for At<'cx, 'tcx> { | |
0531ce1d XL |
15 | /// Given a type `ty` of some value being dropped, computes a set |
16 | /// of "kinds" (types, regions) that must be outlive the execution | |
17 | /// of the destructor. These basically correspond to data that the | |
18 | /// destructor might access. This is used during regionck to | |
19 | /// impose "outlives" constraints on any lifetimes referenced | |
20 | /// within. | |
21 | /// | |
22 | /// The rules here are given by the "dropck" RFCs, notably [#1238] | |
23 | /// and [#1327]. This is a fixed-point computation, where we | |
24 | /// explore all the data that will be dropped (transitively) when | |
25 | /// a value of type `ty` is dropped. For each type T that will be | |
26 | /// dropped and which has a destructor, we must assume that all | |
27 | /// the types/regions of T are live during the destructor, unless | |
28 | /// they are marked with a special attribute (`#[may_dangle]`). | |
29 | /// | |
30 | /// [#1238]: https://github.com/rust-lang/rfcs/blob/master/text/1238-nonparametric-dropck.md | |
31 | /// [#1327]: https://github.com/rust-lang/rfcs/blob/master/text/1327-dropck-param-eyepatch.md | |
ba9703b0 | 32 | fn dropck_outlives(&self, ty: Ty<'tcx>) -> InferOk<'tcx, Vec<GenericArg<'tcx>>> { |
dfeec247 | 33 | debug!("dropck_outlives(ty={:?}, param_env={:?})", ty, self.param_env,); |
0531ce1d XL |
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; | |
60c5eb7d | 38 | if trivial_dropck_outlives(tcx, ty) { |
dfeec247 | 39 | return InferOk { value: vec![], obligations: vec![] }; |
0531ce1d XL |
40 | } |
41 | ||
0bf4aa26 | 42 | let mut orig_values = OriginalQueryValues::default(); |
fc512014 | 43 | let c_ty = self.infcx.canonicalize_query(self.param_env.and(ty), &mut orig_values); |
0531ce1d XL |
44 | let span = self.cause.span; |
45 | debug!("c_ty = {:?}", c_ty); | |
5e7ed085 FG |
46 | if let Ok(result) = tcx.dropck_outlives(c_ty) |
47 | && result.is_proven() | |
48 | && let Ok(InferOk { value, obligations }) = | |
49 | self.infcx.instantiate_query_response_and_region_obligations( | |
50 | self.cause, | |
51 | self.param_env, | |
52 | &orig_values, | |
53 | result, | |
54 | ) | |
55 | { | |
56 | let ty = self.infcx.resolve_vars_if_possible(ty); | |
57 | let kinds = value.into_kinds_reporting_overflows(tcx, span, ty); | |
58 | return InferOk { value: kinds, obligations }; | |
0531ce1d XL |
59 | } |
60 | ||
5e7ed085 | 61 | // Errors and ambiguity in dropck occur in two cases: |
0531ce1d XL |
62 | // - unresolved inference variables at the end of typeck |
63 | // - non well-formed types where projections cannot be resolved | |
b7449926 | 64 | // Either of these should have created an error before. |
dfeec247 | 65 | tcx.sess.delay_span_bug(span, "dtorck encountered internal error"); |
0731742a | 66 | |
dfeec247 | 67 | InferOk { value: vec![], obligations: vec![] } |
0531ce1d XL |
68 | } |
69 | } | |
70 | ||
0531ce1d XL |
71 | /// This returns true if the type `ty` is "trivial" for |
72 | /// dropck-outlives -- that is, if it doesn't require any types to | |
73 | /// outlive. This is similar but not *quite* the same as the | |
74 | /// `needs_drop` test in the compiler already -- that is, for every | |
75 | /// type T for which this function return true, needs-drop would | |
9fa01778 | 76 | /// return `false`. But the reverse does not hold: in particular, |
0531ce1d XL |
77 | /// `needs_drop` returns false for `PhantomData`, but it is not |
78 | /// trivial for dropck-outlives. | |
79 | /// | |
80 | /// Note also that `needs_drop` requires a "global" type (i.e., one | |
a1dfa0c6 | 81 | /// with erased regions), but this function does not. |
dc9dc135 | 82 | pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool { |
1b1a35ee | 83 | match ty.kind() { |
0531ce1d XL |
84 | // None of these types have a destructor and hence they do not |
85 | // require anything in particular to outlive the dtor's | |
86 | // execution. | |
b7449926 XL |
87 | ty::Infer(ty::FreshIntTy(_)) |
88 | | ty::Infer(ty::FreshFloatTy(_)) | |
89 | | ty::Bool | |
90 | | ty::Int(_) | |
91 | | ty::Uint(_) | |
92 | | ty::Float(_) | |
93 | | ty::Never | |
94 | | ty::FnDef(..) | |
95 | | ty::FnPtr(_) | |
96 | | ty::Char | |
97 | | ty::GeneratorWitness(..) | |
98 | | ty::RawPtr(_) | |
99 | | ty::Ref(..) | |
100 | | ty::Str | |
101 | | ty::Foreign(..) | |
f035d41b | 102 | | ty::Error(_) => true, |
0531ce1d XL |
103 | |
104 | // [T; N] and [T] have same properties as T. | |
5099ac24 | 105 | ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, *ty), |
0531ce1d XL |
106 | |
107 | // (T1..Tn) and closures have same properties as T1..Tn -- | |
108 | // check if *any* of those are trivial. | |
5e7ed085 | 109 | ty::Tuple(tys) => tys.iter().all(|t| trivial_dropck_outlives(tcx, t)), |
ba9703b0 | 110 | ty::Closure(_, ref substs) => { |
29967ef6 | 111 | trivial_dropck_outlives(tcx, substs.as_closure().tupled_upvars_ty()) |
dfeec247 | 112 | } |
0531ce1d | 113 | |
b7449926 | 114 | ty::Adt(def, _) => { |
5e7ed085 | 115 | if Some(def.did()) == tcx.lang_items().manually_drop() { |
8faf50e0 | 116 | // `ManuallyDrop` never has a dtor. |
0531ce1d XL |
117 | true |
118 | } else { | |
119 | // Other types might. Moreover, PhantomData doesn't | |
120 | // have a dtor, but it is considered to own its | |
b7449926 XL |
121 | // content, so it is non-trivial. Unions can have `impl Drop`, |
122 | // and hence are non-trivial as well. | |
0531ce1d XL |
123 | false |
124 | } | |
125 | } | |
126 | ||
0bf4aa26 | 127 | // The following *might* require a destructor: needs deeper inspection. |
b7449926 XL |
128 | ty::Dynamic(..) |
129 | | ty::Projection(..) | |
130 | | ty::Param(_) | |
131 | | ty::Opaque(..) | |
a1dfa0c6 | 132 | | ty::Placeholder(..) |
b7449926 | 133 | | ty::Infer(_) |
a1dfa0c6 | 134 | | ty::Bound(..) |
b7449926 | 135 | | ty::Generator(..) => false, |
0531ce1d XL |
136 | } |
137 | } |