]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/query/dropck_outlives.rs
Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup
[rust.git] / compiler / rustc_trait_selection / src / traits / query / dropck_outlives.rs
1 use crate::infer::at::At;
2 use crate::infer::canonical::OriginalQueryValues;
3 use crate::infer::InferOk;
4
5 use rustc_middle::ty::subst::GenericArg;
6 use rustc_middle::ty::{self, Ty, TyCtxt};
7
8 pub use rustc_middle::traits::query::{DropckOutlivesResult, DtorckConstraint};
9
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> {
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
32     fn dropck_outlives(&self, ty: Ty<'tcx>) -> InferOk<'tcx, Vec<GenericArg<'tcx>>> {
33         debug!("dropck_outlives(ty={:?}, param_env={:?})", ty, self.param_env,);
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 { value: vec![], obligations: vec![] };
40         }
41
42         let mut orig_values = OriginalQueryValues::default();
43         let c_ty = self.infcx.canonicalize_query(&self.param_env.and(ty), &mut orig_values);
44         let span = self.cause.span;
45         debug!("c_ty = {:?}", c_ty);
46         if let Ok(result) = &tcx.dropck_outlives(c_ty) {
47             if result.is_proven() {
48                 if 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 };
59                 }
60             }
61         }
62
63         // Errors and ambiuity in dropck occur in two cases:
64         // - unresolved inference variables at the end of typeck
65         // - non well-formed types where projections cannot be resolved
66         // Either of these should have created an error before.
67         tcx.sess.delay_span_bug(span, "dtorck encountered internal error");
68
69         InferOk { value: vec![], obligations: vec![] }
70     }
71 }
72
73 /// This returns true if the type `ty` is "trivial" for
74 /// dropck-outlives -- that is, if it doesn't require any types to
75 /// outlive. This is similar but not *quite* the same as the
76 /// `needs_drop` test in the compiler already -- that is, for every
77 /// type T for which this function return true, needs-drop would
78 /// return `false`. But the reverse does not hold: in particular,
79 /// `needs_drop` returns false for `PhantomData`, but it is not
80 /// trivial for dropck-outlives.
81 ///
82 /// Note also that `needs_drop` requires a "global" type (i.e., one
83 /// with erased regions), but this function does not.
84 pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
85     match ty.kind() {
86         // None of these types have a destructor and hence they do not
87         // require anything in particular to outlive the dtor's
88         // execution.
89         ty::Infer(ty::FreshIntTy(_))
90         | ty::Infer(ty::FreshFloatTy(_))
91         | ty::Bool
92         | ty::Int(_)
93         | ty::Uint(_)
94         | ty::Float(_)
95         | ty::Never
96         | ty::FnDef(..)
97         | ty::FnPtr(_)
98         | ty::Char
99         | ty::GeneratorWitness(..)
100         | ty::RawPtr(_)
101         | ty::Ref(..)
102         | ty::Str
103         | ty::Foreign(..)
104         | ty::Error(_) => true,
105
106         // [T; N] and [T] have same properties as T.
107         ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, ty),
108
109         // (T1..Tn) and closures have same properties as T1..Tn --
110         // check if *any* of those are trivial.
111         ty::Tuple(ref tys) => tys.iter().all(|t| trivial_dropck_outlives(tcx, t.expect_ty())),
112         ty::Closure(_, ref substs) => {
113             substs.as_closure().upvar_tys().all(|t| trivial_dropck_outlives(tcx, t))
114         }
115
116         ty::Adt(def, _) => {
117             if Some(def.did) == tcx.lang_items().manually_drop() {
118                 // `ManuallyDrop` never has a dtor.
119                 true
120             } else {
121                 // Other types might. Moreover, PhantomData doesn't
122                 // have a dtor, but it is considered to own its
123                 // content, so it is non-trivial. Unions can have `impl Drop`,
124                 // and hence are non-trivial as well.
125                 false
126             }
127         }
128
129         // The following *might* require a destructor: needs deeper inspection.
130         ty::Dynamic(..)
131         | ty::Projection(..)
132         | ty::Param(_)
133         | ty::Opaque(..)
134         | ty::Placeholder(..)
135         | ty::Infer(_)
136         | ty::Bound(..)
137         | ty::Generator(..) => false,
138     }
139 }