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