]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/query/dropck_outlives.rs
Rollup merge of #99194 - simlay:simlay/update-rust-gdbgui-gdb-args-to-gdb-cmd, r...
[rust.git] / compiler / rustc_trait_selection / src / traits / query / dropck_outlives.rs
1 use rustc_middle::ty::{self, Ty, TyCtxt};
2
3 pub use rustc_middle::traits::query::{DropckConstraint, DropckOutlivesResult};
4
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.
13 ///
14 /// Note also that `needs_drop` requires a "global" type (i.e., one
15 /// with erased regions), but this function does not.
16 ///
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 {
19     match ty.kind() {
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.
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(..)
34         | ty::RawPtr(_)
35         | ty::Ref(..)
36         | ty::Str
37         | ty::Foreign(..)
38         | ty::Error(_) => true,
39
40         // [T; N] and [T] have same properties as T.
41         ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, *ty),
42
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())
48         }
49
50         ty::Adt(def, _) => {
51             if Some(def.did()) == tcx.lang_items().manually_drop() {
52                 // `ManuallyDrop` never has a dtor.
53                 true
54             } else {
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.
59                 false
60             }
61         }
62
63         // The following *might* require a destructor: needs deeper inspection.
64         ty::Dynamic(..)
65         | ty::Projection(..)
66         | ty::Param(_)
67         | ty::Opaque(..)
68         | ty::Placeholder(..)
69         | ty::Infer(_)
70         | ty::Bound(..)
71         | ty::Generator(..) => false,
72     }
73 }