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