]>
Commit | Line | Data |
---|---|---|
ba9703b0 | 1 | use rustc_middle::ty::{self, Ty, TyCtxt}; |
74b04a01 | 2 | |
5e7ed085 | 3 | pub use rustc_middle::traits::query::{DropckConstraint, DropckOutlivesResult}; |
0531ce1d | 4 | |
0531ce1d XL |
5 | /// This returns true if the type `ty` is "trivial" for |
6 | /// dropck-outlives -- that is, if it doesn't require any types to | |
7 | /// outlive. This is similar but not *quite* the same as the | |
8 | /// `needs_drop` test in the compiler already -- that is, for every | |
9 | /// type T for which this function return true, needs-drop would | |
9fa01778 | 10 | /// return `false`. But the reverse does not hold: in particular, |
0531ce1d XL |
11 | /// `needs_drop` returns false for `PhantomData`, but it is not |
12 | /// trivial for dropck-outlives. | |
13 | /// | |
14 | /// Note also that `needs_drop` requires a "global" type (i.e., one | |
a1dfa0c6 | 15 | /// with erased regions), but this function does not. |
064997fb FG |
16 | /// |
17 | // FIXME(@lcnr): remove this module and move this function somewhere else. | |
dc9dc135 | 18 | pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool { |
1b1a35ee | 19 | match ty.kind() { |
0531ce1d XL |
20 | // None of these types have a destructor and hence they do not |
21 | // require anything in particular to outlive the dtor's | |
22 | // execution. | |
b7449926 XL |
23 | ty::Infer(ty::FreshIntTy(_)) |
24 | | ty::Infer(ty::FreshFloatTy(_)) | |
25 | | ty::Bool | |
26 | | ty::Int(_) | |
27 | | ty::Uint(_) | |
28 | | ty::Float(_) | |
29 | | ty::Never | |
30 | | ty::FnDef(..) | |
31 | | ty::FnPtr(_) | |
32 | | ty::Char | |
33 | | ty::GeneratorWitness(..) | |
9ffffee4 | 34 | | ty::GeneratorWitnessMIR(..) |
b7449926 XL |
35 | | ty::RawPtr(_) |
36 | | ty::Ref(..) | |
37 | | ty::Str | |
38 | | ty::Foreign(..) | |
f035d41b | 39 | | ty::Error(_) => true, |
0531ce1d XL |
40 | |
41 | // [T; N] and [T] have same properties as T. | |
5099ac24 | 42 | ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, *ty), |
0531ce1d XL |
43 | |
44 | // (T1..Tn) and closures have same properties as T1..Tn -- | |
064997fb | 45 | // check if *all* of them are trivial. |
5e7ed085 | 46 | ty::Tuple(tys) => tys.iter().all(|t| trivial_dropck_outlives(tcx, t)), |
ba9703b0 | 47 | ty::Closure(_, ref substs) => { |
29967ef6 | 48 | trivial_dropck_outlives(tcx, substs.as_closure().tupled_upvars_ty()) |
dfeec247 | 49 | } |
0531ce1d | 50 | |
b7449926 | 51 | ty::Adt(def, _) => { |
5e7ed085 | 52 | if Some(def.did()) == tcx.lang_items().manually_drop() { |
8faf50e0 | 53 | // `ManuallyDrop` never has a dtor. |
0531ce1d XL |
54 | true |
55 | } else { | |
56 | // Other types might. Moreover, PhantomData doesn't | |
57 | // have a dtor, but it is considered to own its | |
b7449926 XL |
58 | // content, so it is non-trivial. Unions can have `impl Drop`, |
59 | // and hence are non-trivial as well. | |
0531ce1d XL |
60 | false |
61 | } | |
62 | } | |
63 | ||
0bf4aa26 | 64 | // The following *might* require a destructor: needs deeper inspection. |
b7449926 | 65 | ty::Dynamic(..) |
9c376795 | 66 | | ty::Alias(..) |
b7449926 | 67 | | ty::Param(_) |
a1dfa0c6 | 68 | | ty::Placeholder(..) |
b7449926 | 69 | | ty::Infer(_) |
a1dfa0c6 | 70 | | ty::Bound(..) |
b7449926 | 71 | | ty::Generator(..) => false, |
0531ce1d XL |
72 | } |
73 | } |