@@ -98,59 +98,32 @@ pub fn value_to_const_value<'tcx>(
98
98
mut val : Value ,
99
99
ty : Ty < ' tcx > ,
100
100
) -> & ' tcx ty:: Const < ' tcx > {
101
- let result = ( || {
101
+ let val = ( || {
102
102
// Convert to ByVal or ByValPair if possible
103
103
if let Value :: ByRef ( ptr, align) = val {
104
104
if let Some ( read_val) = ecx. try_read_value ( ptr, align, ty) ? {
105
105
val = read_val;
106
106
}
107
107
}
108
-
109
- let layout = ecx. tcx . layout_of ( ty:: ParamEnv :: reveal_all ( ) . and ( ty) ) . unwrap ( ) ;
110
-
111
- if layout. is_zst ( ) {
112
- return Ok ( ty:: Const :: from_const_value (
113
- ecx. tcx . tcx ,
114
- ConstValue :: ByVal ( PrimVal :: Undef ) ,
115
- ty) ) ;
116
- }
117
-
118
- let val = match layout. abi {
119
- layout:: Abi :: Scalar ( ..) => {
120
- if let Value :: ByVal ( val) = val {
121
- ConstValue :: ByVal ( val)
122
- } else {
123
- bug ! ( "expected ByVal value, got {:?}" , val) ;
124
- }
125
- }
126
- layout:: Abi :: ScalarPair ( ..) => {
127
- if let Value :: ByValPair ( a, b) = val {
128
- ConstValue :: ByValPair ( a, b)
129
- } else {
130
- bug ! ( "expected ByValPair value, got {:?}" , val) ;
131
- }
108
+ match val {
109
+ Value :: ByVal ( val) => Ok ( ConstValue :: ByVal ( val) ) ,
110
+ Value :: ByValPair ( a, b) => Ok ( ConstValue :: ByValPair ( a, b) ) ,
111
+ Value :: ByRef ( ptr, align) => {
112
+ let ptr = ptr. primval . to_ptr ( ) . unwrap ( ) ;
113
+ assert_eq ! ( ptr. offset, 0 ) ;
114
+ let alloc = ecx. memory . get ( ptr. alloc_id ) ?;
115
+ assert ! ( alloc. align. abi( ) >= layout. align. abi( ) ) ;
116
+ assert ! ( alloc. bytes. len( ) as u64 == layout. size. bytes( ) ) ;
117
+ let mut alloc = alloc. clone ( ) ;
118
+ // The align field is meaningless for values, so just use the layout's align
119
+ alloc. align = layout. align ;
120
+ let alloc = ecx. tcx . intern_const_alloc ( alloc) ;
121
+ Ok ( ConstValue :: ByRef ( alloc) )
132
122
}
133
- _ => {
134
- if let Value :: ByRef ( ptr, _) = val {
135
- let ptr = ptr. primval . to_ptr ( ) . unwrap ( ) ;
136
- assert_eq ! ( ptr. offset, 0 ) ;
137
- let alloc = ecx. memory . get ( ptr. alloc_id ) ?;
138
- assert ! ( alloc. align. abi( ) >= layout. align. abi( ) ) ;
139
- assert ! ( alloc. bytes. len( ) as u64 == layout. size. bytes( ) ) ;
140
- let mut alloc = alloc. clone ( ) ;
141
- // The align field is meaningless for values, so just use the layout's align
142
- alloc. align = layout. align ;
143
- let alloc = ecx. tcx . intern_const_alloc ( alloc) ;
144
- ConstValue :: ByRef ( alloc)
145
- } else {
146
- bug ! ( "expected ByRef value, got {:?}" , val) ;
147
- }
148
- } ,
149
- } ;
150
- Ok ( ty:: Const :: from_const_value ( ecx. tcx . tcx , val, ty) )
123
+ }
151
124
} ) ( ) ;
152
125
match result {
153
- Ok ( v) => v ,
126
+ Ok ( v) => ty :: Const :: from_const_value ( tcx , val , ty ) ,
154
127
Err ( mut err) => {
155
128
ecx. report ( & mut err, true , None ) ;
156
129
bug ! ( "miri error occured when converting Value to ConstValue" )
@@ -182,49 +155,49 @@ fn eval_body_using_ecx<'a, 'mir, 'tcx>(
182
155
) -> EvalResult < ' tcx , ( Value , Pointer , Ty < ' tcx > ) > {
183
156
debug ! ( "eval_body: {:?}, {:?}" , cid, param_env) ;
184
157
let tcx = ecx. tcx . tcx ;
185
- let mut mir = match mir {
186
- Some ( mir) => mir,
187
- None => ecx. load_mir ( cid. instance . def ) ?,
188
- } ;
189
- if let Some ( index) = cid. promoted {
190
- mir = & mir. promoted [ index] ;
191
- }
192
- let layout = ecx. layout_of ( mir. return_ty ( ) . subst ( tcx, cid. instance . substs ) ) ?;
193
- assert ! ( !layout. is_unsized( ) ) ;
194
- let ptr = ecx. memory . allocate (
195
- layout. size . bytes ( ) ,
196
- layout. align ,
197
- None ,
198
- ) ?;
199
- let internally_mutable = !layout. ty . is_freeze ( tcx, param_env, mir. span ) ;
200
- let mutability = tcx. is_static ( cid. instance . def_id ( ) ) ;
201
- let mutability = if mutability == Some ( hir:: Mutability :: MutMutable ) || internally_mutable {
202
- Mutability :: Mutable
203
- } else {
204
- Mutability :: Immutable
205
- } ;
206
- let cleanup = StackPopCleanup :: MarkStatic ( mutability) ;
207
- let name = ty:: tls:: with ( |tcx| tcx. item_path_str ( cid. instance . def_id ( ) ) ) ;
208
- let prom = cid. promoted . map_or ( String :: new ( ) , |p| format ! ( "::promoted[{:?}]" , p) ) ;
209
- trace ! ( "const_eval: pushing stack frame for global: {}{}" , name, prom) ;
210
- assert ! ( mir. arg_count == 0 ) ;
211
- ecx. push_stack_frame (
212
- cid. instance ,
213
- mir. span ,
214
- mir,
215
- Place :: from_ptr ( ptr, layout. align ) ,
216
- cleanup,
217
- ) ?;
158
+ let mut mir = match mir {
159
+ Some ( mir) => mir,
160
+ None => ecx. load_mir ( cid. instance . def ) ?,
161
+ } ;
162
+ if let Some ( index) = cid. promoted {
163
+ mir = & mir. promoted [ index] ;
164
+ }
165
+ let layout = ecx. layout_of ( mir. return_ty ( ) . subst ( tcx, cid. instance . substs ) ) ?;
166
+ assert ! ( !layout. is_unsized( ) ) ;
167
+ let ptr = ecx. memory . allocate (
168
+ layout. size . bytes ( ) ,
169
+ layout. align ,
170
+ None ,
171
+ ) ?;
172
+ let internally_mutable = !layout. ty . is_freeze ( tcx, param_env, mir. span ) ;
173
+ let is_static = tcx. is_static ( cid. instance . def_id ( ) ) ;
174
+ let mutability = if is_static == Some ( hir:: Mutability :: MutMutable ) || internally_mutable {
175
+ Mutability :: Mutable
176
+ } else {
177
+ Mutability :: Immutable
178
+ } ;
179
+ let cleanup = StackPopCleanup :: MarkStatic ( mutability) ;
180
+ let name = ty:: tls:: with ( |tcx| tcx. item_path_str ( cid. instance . def_id ( ) ) ) ;
181
+ let prom = cid. promoted . map_or ( String :: new ( ) , |p| format ! ( "::promoted[{:?}]" , p) ) ;
182
+ trace ! ( "const_eval: pushing stack frame for global: {}{}" , name, prom) ;
183
+ assert ! ( mir. arg_count == 0 ) ;
184
+ ecx. push_stack_frame (
185
+ cid. instance ,
186
+ mir. span ,
187
+ mir,
188
+ Place :: from_ptr ( ptr, layout. align ) ,
189
+ cleanup,
190
+ ) ?;
218
191
219
- while ecx. step ( ) ? { }
220
- let ptr = ptr. into ( ) ;
221
- // always try to read the value and report errors
222
- let value = match ecx. try_read_value ( ptr, layout. align , layout. ty ) ? {
223
- Some ( val) => val,
224
- // point at the allocation
225
- _ => Value :: ByRef ( ptr, layout. align ) ,
226
- } ;
227
- Ok ( ( value, ptr, layout. ty ) )
192
+ while ecx. step ( ) ? { }
193
+ let ptr = ptr. into ( ) ;
194
+ // always try to read the value and report errors
195
+ let value = match ecx. try_read_value ( ptr, layout. align , layout. ty ) ? {
196
+ Some ( val) if is_static . is_none ( ) => val,
197
+ // point at the allocation
198
+ _ => Value :: ByRef ( ptr, layout. align ) ,
199
+ } ;
200
+ Ok ( ( value, ptr, layout. ty ) )
228
201
}
229
202
230
203
pub struct CompileTimeEvaluator ;
0 commit comments