1 use rustc_middle::ty::{self, Ty, TyCtxt};
3 pub use rustc_middle::traits::query::{DropckConstraint, DropckOutlivesResult};
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
10 /// return `false`. But the reverse does not hold: in particular,
11 /// `needs_drop` returns false for `PhantomData`, but it is not
12 /// trivial for dropck-outlives.
14 /// Note also that `needs_drop` requires a "global" type (i.e., one
15 /// with erased regions), but this function does not.
17 // FIXME(@lcnr): remove this module and move this function somewhere else.
18 pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
20 // None of these types have a destructor and hence they do not
21 // require anything in particular to outlive the dtor's
23 ty::Infer(ty::FreshIntTy(_))
24 | ty::Infer(ty::FreshFloatTy(_))
33 | ty::GeneratorWitness(..)
38 | ty::Error(_) => true,
40 // [T; N] and [T] have same properties as T.
41 ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, *ty),
43 // (T1..Tn) and closures have same properties as T1..Tn --
44 // check if *all* of them are trivial.
45 ty::Tuple(tys) => tys.iter().all(|t| trivial_dropck_outlives(tcx, t)),
46 ty::Closure(_, ref substs) => {
47 trivial_dropck_outlives(tcx, substs.as_closure().tupled_upvars_ty())
51 if Some(def.did()) == tcx.lang_items().manually_drop() {
52 // `ManuallyDrop` never has a dtor.
55 // Other types might. Moreover, PhantomData doesn't
56 // have a dtor, but it is considered to own its
57 // content, so it is non-trivial. Unions can have `impl Drop`,
58 // and hence are non-trivial as well.
63 // The following *might* require a destructor: needs deeper inspection.
71 | ty::Generator(..) => false,