@@ -191,11 +191,16 @@ impl<'tcx> Cx<'tcx> {
191
191
source : self . mirror_expr ( source) ,
192
192
cast : PointerCoercion :: ArrayToPointer ,
193
193
}
194
- } else {
195
- // check whether this is casting an enum variant discriminant
196
- // to prevent cycles, we refer to the discriminant initializer
194
+ } else if let hir:: ExprKind :: Path ( ref qpath) = source. kind
195
+ && let res = self . typeck_results ( ) . qpath_res ( qpath, source. hir_id )
196
+ && let ty = self . typeck_results ( ) . node_type ( source. hir_id )
197
+ && let ty:: Adt ( adt_def, args) = ty. kind ( )
198
+ && let Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , CtorKind :: Const ) , variant_ctor_id) = res
199
+ {
200
+ // Check whether this is casting an enum variant discriminant.
201
+ // To prevent cycles, we refer to the discriminant initializer,
197
202
// which is always an integer and thus doesn't need to know the
198
- // enum's layout (or its tag type) to compute it during const eval
203
+ // enum's layout (or its tag type) to compute it during const eval.
199
204
// Example:
200
205
// enum Foo {
201
206
// A,
@@ -204,21 +209,6 @@ impl<'tcx> Cx<'tcx> {
204
209
// The correct solution would be to add symbolic computations to miri,
205
210
// so we wouldn't have to compute and store the actual value
206
211
207
- let hir:: ExprKind :: Path ( ref qpath) = source. kind else {
208
- return ExprKind :: Cast { source : self . mirror_expr ( source) } ;
209
- } ;
210
-
211
- let res = self . typeck_results ( ) . qpath_res ( qpath, source. hir_id ) ;
212
- let ty = self . typeck_results ( ) . node_type ( source. hir_id ) ;
213
- let ty:: Adt ( adt_def, args) = ty. kind ( ) else {
214
- return ExprKind :: Cast { source : self . mirror_expr ( source) } ;
215
- } ;
216
-
217
- let Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , CtorKind :: Const ) , variant_ctor_id) = res
218
- else {
219
- return ExprKind :: Cast { source : self . mirror_expr ( source) } ;
220
- } ;
221
-
222
212
let idx = adt_def. variant_index_with_ctor_id ( variant_ctor_id) ;
223
213
let ( discr_did, discr_offset) = adt_def. discriminant_def_for_variant ( idx) ;
224
214
@@ -255,6 +245,10 @@ impl<'tcx> Cx<'tcx> {
255
245
} ;
256
246
257
247
ExprKind :: Cast { source }
248
+ } else {
249
+ // Default to `ExprKind::Cast` for all explicit casts.
250
+ // MIR building then picks the right MIR casts based on the types.
251
+ ExprKind :: Cast { source : self . mirror_expr ( source) }
258
252
}
259
253
}
260
254
0 commit comments