]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/check_consts/qualifs.rs
8c52a4e7a29dd5ac4436882ffb7f2b62fadafed0
[rust.git] / src / librustc_mir / transform / check_consts / qualifs.rs
1 //! A copy of the `Qualif` trait in `qualify_consts.rs` that is suitable for the new validator.
2
3 use rustc::mir::*;
4 use rustc::ty::{self, Ty};
5 use rustc_span::DUMMY_SP;
6
7 use super::Item as ConstCx;
8
9 pub fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> ConstQualifs {
10     ConstQualifs {
11         has_mut_interior: HasMutInterior::in_any_value_of_ty(cx, ty),
12         needs_drop: NeedsDrop::in_any_value_of_ty(cx, ty),
13     }
14 }
15
16 /// A "qualif"(-ication) is a way to look for something "bad" in the MIR that would disqualify some
17 /// code for promotion or prevent it from evaluating at compile time. So `return true` means
18 /// "I found something bad, no reason to go on searching". `false` is only returned if we
19 /// definitely cannot find anything bad anywhere.
20 ///
21 /// The default implementations proceed structurally.
22 pub trait Qualif {
23     /// The name of the file used to debug the dataflow analysis that computes this qualif.
24     const ANALYSIS_NAME: &'static str;
25
26     /// Whether this `Qualif` is cleared when a local is moved from.
27     const IS_CLEARED_ON_MOVE: bool = false;
28
29     fn in_qualifs(qualifs: &ConstQualifs) -> bool;
30
31     /// Return the qualification that is (conservatively) correct for any value
32     /// of the type.
33     fn in_any_value_of_ty(_cx: &ConstCx<'_, 'tcx>, _ty: Ty<'tcx>) -> bool;
34
35     fn in_projection_structurally(
36         cx: &ConstCx<'_, 'tcx>,
37         per_local: &impl Fn(Local) -> bool,
38         place: PlaceRef<'_, 'tcx>,
39     ) -> bool {
40         if let [proj_base @ .., elem] = place.projection {
41             let base_qualif =
42                 Self::in_place(cx, per_local, PlaceRef { base: place.base, projection: proj_base });
43             let qualif = base_qualif
44                 && Self::in_any_value_of_ty(
45                     cx,
46                     Place::ty_from(place.base, proj_base, *cx.body, cx.tcx)
47                         .projection_ty(cx.tcx, elem)
48                         .ty,
49                 );
50             match elem {
51                 ProjectionElem::Deref
52                 | ProjectionElem::Subslice { .. }
53                 | ProjectionElem::Field(..)
54                 | ProjectionElem::ConstantIndex { .. }
55                 | ProjectionElem::Downcast(..) => qualif,
56
57                 ProjectionElem::Index(local) => qualif || per_local(*local),
58             }
59         } else {
60             bug!("This should be called if projection is not empty");
61         }
62     }
63
64     fn in_projection(
65         cx: &ConstCx<'_, 'tcx>,
66         per_local: &impl Fn(Local) -> bool,
67         place: PlaceRef<'_, 'tcx>,
68     ) -> bool {
69         Self::in_projection_structurally(cx, per_local, place)
70     }
71
72     fn in_place(
73         cx: &ConstCx<'_, 'tcx>,
74         per_local: &impl Fn(Local) -> bool,
75         place: PlaceRef<'_, 'tcx>,
76     ) -> bool {
77         match place {
78             PlaceRef { base: PlaceBase::Local(local), projection: [] } => per_local(*local),
79             PlaceRef { base: _, projection: [.., _] } => Self::in_projection(cx, per_local, place),
80         }
81     }
82
83     fn in_operand(
84         cx: &ConstCx<'_, 'tcx>,
85         per_local: &impl Fn(Local) -> bool,
86         operand: &Operand<'tcx>,
87     ) -> bool {
88         match *operand {
89             Operand::Copy(ref place) | Operand::Move(ref place) => {
90                 Self::in_place(cx, per_local, place.as_ref())
91             }
92
93             Operand::Constant(ref constant) => {
94                 if constant.check_static_ptr(cx.tcx).is_some() {
95                     // `mir_const_qualif` does return the qualifs in the final value of a `static`,
96                     // so we could use value-based qualification here, but we shouldn't do this
97                     // without a good reason.
98                     //
99                     // Note: this uses `constant.literal.ty` which is a reference or pointer to the
100                     // type of the actual `static` item.
101                     Self::in_any_value_of_ty(cx, constant.literal.ty)
102                 } else if let ty::ConstKind::Unevaluated(def_id, _, promoted) = constant.literal.val
103                 {
104                     assert!(promoted.is_none());
105                     // Don't peek inside trait associated constants.
106                     if cx.tcx.trait_of_item(def_id).is_some() {
107                         Self::in_any_value_of_ty(cx, constant.literal.ty)
108                     } else {
109                         let qualifs = cx.tcx.at(constant.span).mir_const_qualif(def_id);
110                         let qualif = Self::in_qualifs(&qualifs);
111
112                         // Just in case the type is more specific than
113                         // the definition, e.g., impl associated const
114                         // with type parameters, take it into account.
115                         qualif && Self::in_any_value_of_ty(cx, constant.literal.ty)
116                     }
117                 } else {
118                     false
119                 }
120             }
121         }
122     }
123
124     fn in_rvalue_structurally(
125         cx: &ConstCx<'_, 'tcx>,
126         per_local: &impl Fn(Local) -> bool,
127         rvalue: &Rvalue<'tcx>,
128     ) -> bool {
129         match *rvalue {
130             Rvalue::NullaryOp(..) => false,
131
132             Rvalue::Discriminant(ref place) | Rvalue::Len(ref place) => {
133                 Self::in_place(cx, per_local, place.as_ref())
134             }
135
136             Rvalue::Use(ref operand)
137             | Rvalue::Repeat(ref operand, _)
138             | Rvalue::UnaryOp(_, ref operand)
139             | Rvalue::Cast(_, ref operand, _) => Self::in_operand(cx, per_local, operand),
140
141             Rvalue::BinaryOp(_, ref lhs, ref rhs)
142             | Rvalue::CheckedBinaryOp(_, ref lhs, ref rhs) => {
143                 Self::in_operand(cx, per_local, lhs) || Self::in_operand(cx, per_local, rhs)
144             }
145
146             Rvalue::Ref(_, _, ref place) | Rvalue::AddressOf(_, ref place) => {
147                 // Special-case reborrows to be more like a copy of the reference.
148                 if let [proj_base @ .., ProjectionElem::Deref] = place.projection.as_ref() {
149                     let base_ty = Place::ty_from(&place.base, proj_base, *cx.body, cx.tcx).ty;
150                     if let ty::Ref(..) = base_ty.kind {
151                         return Self::in_place(
152                             cx,
153                             per_local,
154                             PlaceRef { base: &place.base, projection: proj_base },
155                         );
156                     }
157                 }
158
159                 Self::in_place(cx, per_local, place.as_ref())
160             }
161
162             Rvalue::Aggregate(_, ref operands) => {
163                 operands.iter().any(|o| Self::in_operand(cx, per_local, o))
164             }
165         }
166     }
167
168     fn in_rvalue(
169         cx: &ConstCx<'_, 'tcx>,
170         per_local: &impl Fn(Local) -> bool,
171         rvalue: &Rvalue<'tcx>,
172     ) -> bool {
173         Self::in_rvalue_structurally(cx, per_local, rvalue)
174     }
175
176     fn in_call(
177         cx: &ConstCx<'_, 'tcx>,
178         _per_local: &impl Fn(Local) -> bool,
179         _callee: &Operand<'tcx>,
180         _args: &[Operand<'tcx>],
181         return_ty: Ty<'tcx>,
182     ) -> bool {
183         // Be conservative about the returned value of a const fn.
184         Self::in_any_value_of_ty(cx, return_ty)
185     }
186 }
187
188 /// Constant containing interior mutability (`UnsafeCell<T>`).
189 /// This must be ruled out to make sure that evaluating the constant at compile-time
190 /// and at *any point* during the run-time would produce the same result. In particular,
191 /// promotion of temporaries must not change program behavior; if the promoted could be
192 /// written to, that would be a problem.
193 pub struct HasMutInterior;
194
195 impl Qualif for HasMutInterior {
196     const ANALYSIS_NAME: &'static str = "flow_has_mut_interior";
197
198     fn in_qualifs(qualifs: &ConstQualifs) -> bool {
199         qualifs.has_mut_interior
200     }
201
202     fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
203         !ty.is_freeze(cx.tcx, cx.param_env, DUMMY_SP)
204     }
205
206     fn in_rvalue(
207         cx: &ConstCx<'_, 'tcx>,
208         per_local: &impl Fn(Local) -> bool,
209         rvalue: &Rvalue<'tcx>,
210     ) -> bool {
211         match *rvalue {
212             Rvalue::Aggregate(ref kind, _) => {
213                 if let AggregateKind::Adt(def, ..) = **kind {
214                     if Some(def.did) == cx.tcx.lang_items().unsafe_cell_type() {
215                         let ty = rvalue.ty(*cx.body, cx.tcx);
216                         assert_eq!(Self::in_any_value_of_ty(cx, ty), true);
217                         return true;
218                     }
219                 }
220             }
221
222             _ => {}
223         }
224
225         Self::in_rvalue_structurally(cx, per_local, rvalue)
226     }
227 }
228
229 /// Constant containing an ADT that implements `Drop`.
230 /// This must be ruled out (a) because we cannot run `Drop` during compile-time
231 /// as that might not be a `const fn`, and (b) because implicit promotion would
232 /// remove side-effects that occur as part of dropping that value.
233 pub struct NeedsDrop;
234
235 impl Qualif for NeedsDrop {
236     const ANALYSIS_NAME: &'static str = "flow_needs_drop";
237     const IS_CLEARED_ON_MOVE: bool = true;
238
239     fn in_qualifs(qualifs: &ConstQualifs) -> bool {
240         qualifs.needs_drop
241     }
242
243     fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
244         ty.needs_drop(cx.tcx, cx.param_env)
245     }
246
247     fn in_rvalue(
248         cx: &ConstCx<'_, 'tcx>,
249         per_local: &impl Fn(Local) -> bool,
250         rvalue: &Rvalue<'tcx>,
251     ) -> bool {
252         if let Rvalue::Aggregate(ref kind, _) = *rvalue {
253             if let AggregateKind::Adt(def, ..) = **kind {
254                 if def.has_dtor(cx.tcx) {
255                     return true;
256                 }
257             }
258         }
259
260         Self::in_rvalue_structurally(cx, per_local, rvalue)
261     }
262 }