@@ -201,33 +201,23 @@ impl<'tcx> Cx<'tcx> {
201
201
source : self . mirror_expr ( source) ,
202
202
cast : PointerCast :: ArrayToPointer ,
203
203
}
204
- } else {
205
- // check whether this is casting an enum variant discriminant
206
- // to prevent cycles, we refer to the discriminant initializer
204
+ } else if let hir:: ExprKind :: Path ( ref qpath) = source. kind
205
+ && let res = self . typeck_results ( ) . qpath_res ( qpath, source. hir_id )
206
+ && let ty = self . typeck_results ( ) . node_type ( source. hir_id )
207
+ && let ty:: Adt ( adt_def, substs) = ty. kind ( )
208
+ && let Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , CtorKind :: Const ) , variant_ctor_id) = res
209
+ {
210
+ // Check whether this is casting an enum variant discriminant.
211
+ // To prevent cycles, we refer to the discriminant initializer,
207
212
// which is always an integer and thus doesn't need to know the
208
- // enum's layout (or its tag type) to compute it during const eval
213
+ // enum's layout (or its tag type) to compute it during const eval.
209
214
// Example:
210
215
// enum Foo {
211
216
// A,
212
217
// B = A as isize + 4,
213
218
// }
214
219
// The correct solution would be to add symbolic computations to miri,
215
220
// so we wouldn't have to compute and store the actual value
216
-
217
- let hir:: ExprKind :: Path ( ref qpath) = source. kind else {
218
- return ExprKind :: Cast { source : self . mirror_expr ( source) } ;
219
- } ;
220
-
221
- let res = self . typeck_results ( ) . qpath_res ( qpath, source. hir_id ) ;
222
- let ty = self . typeck_results ( ) . node_type ( source. hir_id ) ;
223
- let ty:: Adt ( adt_def, substs) = ty. kind ( ) else {
224
- return ExprKind :: Cast { source : self . mirror_expr ( source) } ;
225
- } ;
226
-
227
- let Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , CtorKind :: Const ) , variant_ctor_id) = res else {
228
- return ExprKind :: Cast { source : self . mirror_expr ( source) } ;
229
- } ;
230
-
231
221
let idx = adt_def. variant_index_with_ctor_id ( variant_ctor_id) ;
232
222
let ( discr_did, discr_offset) = adt_def. discriminant_def_for_variant ( idx) ;
233
223
@@ -266,6 +256,10 @@ impl<'tcx> Cx<'tcx> {
266
256
} ;
267
257
268
258
ExprKind :: Cast { source }
259
+ } else {
260
+ // Default to `ExprKind::Cast` for all explicit casts.
261
+ // MIR building then picks the right MIR casts based on the types.
262
+ ExprKind :: Cast { source : self . mirror_expr ( source) }
269
263
}
270
264
}
271
265
0 commit comments