1- //! A copy of the `Qualif` trait in `qualify_consts.rs` that is suitable for the new validator.
1+ //! Structural const qualification.
2+ //!
3+ //! See the `Qualif` trait for more info.
24
35use rustc:: mir:: * ;
4- use rustc:: ty:: { self , Ty } ;
6+ use rustc:: ty:: { self , AdtDef , Ty } ;
57use rustc_span:: DUMMY_SP ;
68
79use super :: Item as ConstCx ;
@@ -14,169 +16,44 @@ pub fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> ConstQualifs
1416}
1517
1618/// 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.
19+ /// code for promotion or prevent it from evaluating at compile time.
2020///
21- /// The default implementations proceed structurally.
21+ /// Normally, we would determine what qualifications apply to each type and error when an illegal
22+ /// operation is performed on such a type. However, this was found to be too imprecise, especially
23+ /// in the presence of `enum`s. If only a single variant of an enum has a certain qualification, we
24+ /// needn't reject code unless it actually constructs and operates on the qualifed variant.
25+ ///
26+ /// To accomplish this, const-checking and promotion use a value-based analysis (as opposed to a
27+ /// type-based one). Qualifications propagate structurally across variables: If a local (or a
28+ /// projection of a local) is assigned a qualifed value, that local itself becomes qualifed.
2229pub trait Qualif {
2330 /// The name of the file used to debug the dataflow analysis that computes this qualif.
2431 const ANALYSIS_NAME : & ' static str ;
2532
2633 /// Whether this `Qualif` is cleared when a local is moved from.
2734 const IS_CLEARED_ON_MOVE : bool = false ;
2835
36+ /// Extracts the field of `ConstQualifs` that corresponds to this `Qualif`.
2937 fn in_qualifs ( qualifs : & ConstQualifs ) -> bool ;
3038
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 : & mut impl FnMut ( 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 : & mut impl FnMut ( 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 : & mut impl FnMut ( 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 : & mut impl FnMut ( 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- // Check the qualifs of the value of `const` items.
98- if let ty:: ConstKind :: Unevaluated ( def_id, _, promoted) = constant. literal . val {
99- assert ! ( promoted. is_none( ) ) ;
100- // Don't peek inside trait associated constants.
101- if cx. tcx . trait_of_item ( def_id) . is_none ( ) {
102- let qualifs = cx. tcx . at ( constant. span ) . mir_const_qualif ( def_id) ;
103- if !Self :: in_qualifs ( & qualifs) {
104- return false ;
105- }
106-
107- // Just in case the type is more specific than
108- // the definition, e.g., impl associated const
109- // with type parameters, take it into account.
110- }
111- }
112- // Otherwise use the qualifs of the type.
113- Self :: in_any_value_of_ty ( cx, constant. literal . ty )
114- }
115- }
116- }
117-
118- fn in_rvalue_structurally (
119- cx : & ConstCx < ' _ , ' tcx > ,
120- per_local : & mut impl FnMut ( Local ) -> bool ,
121- rvalue : & Rvalue < ' tcx > ,
122- ) -> bool {
123- match * rvalue {
124- Rvalue :: NullaryOp ( ..) => false ,
125-
126- Rvalue :: Discriminant ( ref place) | Rvalue :: Len ( ref place) => {
127- Self :: in_place ( cx, per_local, place. as_ref ( ) )
128- }
129-
130- Rvalue :: Use ( ref operand)
131- | Rvalue :: Repeat ( ref operand, _)
132- | Rvalue :: UnaryOp ( _, ref operand)
133- | Rvalue :: Cast ( _, ref operand, _) => Self :: in_operand ( cx, per_local, operand) ,
134-
135- Rvalue :: BinaryOp ( _, ref lhs, ref rhs)
136- | Rvalue :: CheckedBinaryOp ( _, ref lhs, ref rhs) => {
137- Self :: in_operand ( cx, per_local, lhs) || Self :: in_operand ( cx, per_local, rhs)
138- }
139-
140- Rvalue :: Ref ( _, _, ref place) | Rvalue :: AddressOf ( _, ref place) => {
141- // Special-case reborrows to be more like a copy of the reference.
142- if let [ proj_base @ .., ProjectionElem :: Deref ] = place. projection . as_ref ( ) {
143- let base_ty = Place :: ty_from ( place. local , proj_base, * cx. body , cx. tcx ) . ty ;
144- if let ty:: Ref ( ..) = base_ty. kind {
145- return Self :: in_place (
146- cx,
147- per_local,
148- PlaceRef { local : place. local , projection : proj_base } ,
149- ) ;
150- }
151- }
152-
153- Self :: in_place ( cx, per_local, place. as_ref ( ) )
154- }
155-
156- Rvalue :: Aggregate ( _, ref operands) => {
157- operands. iter ( ) . any ( |o| Self :: in_operand ( cx, per_local, o) )
158- }
159- }
160- }
161-
162- fn in_rvalue (
163- cx : & ConstCx < ' _ , ' tcx > ,
164- per_local : & mut impl FnMut ( Local ) -> bool ,
165- rvalue : & Rvalue < ' tcx > ,
166- ) -> bool {
167- Self :: in_rvalue_structurally ( cx, per_local, rvalue)
168- }
169-
170- fn in_call (
171- cx : & ConstCx < ' _ , ' tcx > ,
172- _per_local : & mut impl FnMut ( Local ) -> bool ,
173- _callee : & Operand < ' tcx > ,
174- _args : & [ Operand < ' tcx > ] ,
175- return_ty : Ty < ' tcx > ,
176- ) -> bool {
177- // Be conservative about the returned value of a const fn.
178- Self :: in_any_value_of_ty ( cx, return_ty)
179- }
39+ /// Returns `true` if *any* value of the given type could possibly have this `Qualif`.
40+ ///
41+ /// This function determines `Qualif`s when we cannot do a value-based analysis. Since qualif
42+ /// propagation is context-insenstive, this includes function arguments and values returned
43+ /// from a call to another function.
44+ ///
45+ /// It also determines the `Qualif`s for primitive types.
46+ fn in_any_value_of_ty ( cx : & ConstCx < ' _ , ' tcx > , ty : Ty < ' tcx > ) -> bool ;
47+
48+ /// Returns `true` if this `Qualif` is inherent to the given struct or enum.
49+ ///
50+ /// By default, `Qualif`s propagate into ADTs in a structural way: An ADT only becomes
51+ /// qualified if part of it is assigned a value with that `Qualif`. However, some ADTs *always*
52+ /// have a certain `Qualif`, regardless of whether their fields have it. For example, a type
53+ /// with a custom `Drop` impl is inherently `NeedsDrop`.
54+ ///
55+ /// Returning `true` for `in_adt_inherently` but `false` for `in_any_value_of_ty` is unsound.
56+ fn in_adt_inherently ( cx : & ConstCx < ' _ , ' tcx > , adt : & AdtDef ) -> bool ;
18057}
18158
18259/// Constant containing interior mutability (`UnsafeCell<T>`).
@@ -197,26 +74,10 @@ impl Qualif for HasMutInterior {
19774 !ty. is_freeze ( cx. tcx , cx. param_env , DUMMY_SP )
19875 }
19976
200- fn in_rvalue (
201- cx : & ConstCx < ' _ , ' tcx > ,
202- per_local : & mut impl FnMut ( Local ) -> bool ,
203- rvalue : & Rvalue < ' tcx > ,
204- ) -> bool {
205- match * rvalue {
206- Rvalue :: Aggregate ( ref kind, _) => {
207- if let AggregateKind :: Adt ( def, ..) = * * kind {
208- if Some ( def. did ) == cx. tcx . lang_items ( ) . unsafe_cell_type ( ) {
209- let ty = rvalue. ty ( * cx. body , cx. tcx ) ;
210- assert_eq ! ( Self :: in_any_value_of_ty( cx, ty) , true ) ;
211- return true ;
212- }
213- }
214- }
215-
216- _ => { }
217- }
218-
219- Self :: in_rvalue_structurally ( cx, per_local, rvalue)
77+ fn in_adt_inherently ( cx : & ConstCx < ' _ , ' tcx > , adt : & AdtDef ) -> bool {
78+ // Exactly one type, `UnsafeCell`, has the `HasMutInterior` qualif inherently.
79+ // It arises structurally for all other types.
80+ Some ( adt. did ) == cx. tcx . lang_items ( ) . unsafe_cell_type ( )
22081 }
22182}
22283
@@ -238,19 +99,127 @@ impl Qualif for NeedsDrop {
23899 ty. needs_drop ( cx. tcx , cx. param_env )
239100 }
240101
241- fn in_rvalue (
242- cx : & ConstCx < ' _ , ' tcx > ,
243- per_local : & mut impl FnMut ( Local ) -> bool ,
244- rvalue : & Rvalue < ' tcx > ,
245- ) -> bool {
246- if let Rvalue :: Aggregate ( ref kind, _) = * rvalue {
102+ fn in_adt_inherently ( cx : & ConstCx < ' _ , ' tcx > , adt : & AdtDef ) -> bool {
103+ adt. has_dtor ( cx. tcx )
104+ }
105+ }
106+
107+ // FIXME: Use `mir::visit::Visitor` for the `in_*` functions if/when it supports early return.
108+
109+ /// Returns `true` if this `Rvalue` contains qualif `Q`.
110+ pub fn in_rvalue < Q , F > ( cx : & ConstCx < ' _ , ' tcx > , in_local : & mut F , rvalue : & Rvalue < ' tcx > ) -> bool
111+ where
112+ Q : Qualif ,
113+ F : FnMut ( Local ) -> bool ,
114+ {
115+ match rvalue {
116+ Rvalue :: NullaryOp ( ..) => Q :: in_any_value_of_ty ( cx, rvalue. ty ( * cx. body , cx. tcx ) ) ,
117+
118+ Rvalue :: Discriminant ( place) | Rvalue :: Len ( place) => {
119+ in_place :: < Q , _ > ( cx, in_local, place. as_ref ( ) )
120+ }
121+
122+ Rvalue :: Use ( operand)
123+ | Rvalue :: Repeat ( operand, _)
124+ | Rvalue :: UnaryOp ( _, operand)
125+ | Rvalue :: Cast ( _, operand, _) => in_operand :: < Q , _ > ( cx, in_local, operand) ,
126+
127+ Rvalue :: BinaryOp ( _, lhs, rhs) | Rvalue :: CheckedBinaryOp ( _, lhs, rhs) => {
128+ in_operand :: < Q , _ > ( cx, in_local, lhs) || in_operand :: < Q , _ > ( cx, in_local, rhs)
129+ }
130+
131+ Rvalue :: Ref ( _, _, place) | Rvalue :: AddressOf ( _, place) => {
132+ // Special-case reborrows to be more like a copy of the reference.
133+ if let & [ ref proj_base @ .., ProjectionElem :: Deref ] = place. projection . as_ref ( ) {
134+ let base_ty = Place :: ty_from ( place. local , proj_base, * cx. body , cx. tcx ) . ty ;
135+ if let ty:: Ref ( ..) = base_ty. kind {
136+ return in_place :: < Q , _ > (
137+ cx,
138+ in_local,
139+ PlaceRef { local : place. local , projection : proj_base } ,
140+ ) ;
141+ }
142+ }
143+
144+ in_place :: < Q , _ > ( cx, in_local, place. as_ref ( ) )
145+ }
146+
147+ Rvalue :: Aggregate ( kind, operands) => {
148+ // Return early if we know that the struct or enum being constructed is always
149+ // qualified.
247150 if let AggregateKind :: Adt ( def, ..) = * * kind {
248- if def . has_dtor ( cx. tcx ) {
151+ if Q :: in_adt_inherently ( cx, def ) {
249152 return true ;
250153 }
251154 }
155+
156+ // Otherwise, proceed structurally...
157+ operands. iter ( ) . any ( |o| in_operand :: < Q , _ > ( cx, in_local, o) )
252158 }
159+ }
160+ }
253161
254- Self :: in_rvalue_structurally ( cx, per_local, rvalue)
162+ /// Returns `true` if this `Place` contains qualif `Q`.
163+ pub fn in_place < Q , F > ( cx : & ConstCx < ' _ , ' tcx > , in_local : & mut F , place : PlaceRef < ' tcx > ) -> bool
164+ where
165+ Q : Qualif ,
166+ F : FnMut ( Local ) -> bool ,
167+ {
168+ let mut projection = place. projection ;
169+ while let [ ref proj_base @ .., proj_elem] = projection {
170+ match * proj_elem {
171+ ProjectionElem :: Index ( index) if in_local ( index) => return true ,
172+
173+ ProjectionElem :: Deref
174+ | ProjectionElem :: Field ( _, _)
175+ | ProjectionElem :: ConstantIndex { .. }
176+ | ProjectionElem :: Subslice { .. }
177+ | ProjectionElem :: Downcast ( _, _)
178+ | ProjectionElem :: Index ( _) => { }
179+ }
180+
181+ let base_ty = Place :: ty_from ( place. local , proj_base, * cx. body , cx. tcx ) ;
182+ let proj_ty = base_ty. projection_ty ( cx. tcx , proj_elem) . ty ;
183+ if !Q :: in_any_value_of_ty ( cx, proj_ty) {
184+ return false ;
185+ }
186+
187+ projection = proj_base;
188+ }
189+
190+ assert ! ( projection. is_empty( ) ) ;
191+ in_local ( place. local )
192+ }
193+
194+ /// Returns `true` if this `Operand` contains qualif `Q`.
195+ pub fn in_operand < Q , F > ( cx : & ConstCx < ' _ , ' tcx > , in_local : & mut F , operand : & Operand < ' tcx > ) -> bool
196+ where
197+ Q : Qualif ,
198+ F : FnMut ( Local ) -> bool ,
199+ {
200+ let constant = match operand {
201+ Operand :: Copy ( place) | Operand :: Move ( place) => {
202+ return in_place :: < Q , _ > ( cx, in_local, place. as_ref ( ) ) ;
203+ }
204+
205+ Operand :: Constant ( c) => c,
206+ } ;
207+
208+ // Check the qualifs of the value of `const` items.
209+ if let ty:: ConstKind :: Unevaluated ( def_id, _, promoted) = constant. literal . val {
210+ assert ! ( promoted. is_none( ) ) ;
211+ // Don't peek inside trait associated constants.
212+ if cx. tcx . trait_of_item ( def_id) . is_none ( ) {
213+ let qualifs = cx. tcx . at ( constant. span ) . mir_const_qualif ( def_id) ;
214+ if !Q :: in_qualifs ( & qualifs) {
215+ return false ;
216+ }
217+
218+ // Just in case the type is more specific than
219+ // the definition, e.g., impl associated const
220+ // with type parameters, take it into account.
221+ }
255222 }
223+ // Otherwise use the qualifs of the type.
224+ Q :: in_any_value_of_ty ( cx, constant. literal . ty )
256225}
0 commit comments