@@ -191,11 +191,16 @@ impl<'tcx> Cx<'tcx> {
191191 source : self . mirror_expr ( source) ,
192192 cast : PointerCoercion :: ArrayToPointer ,
193193 }
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,
197202 // 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.
199204 // Example:
200205 // enum Foo {
201206 // A,
@@ -204,21 +209,6 @@ impl<'tcx> Cx<'tcx> {
204209 // The correct solution would be to add symbolic computations to miri,
205210 // so we wouldn't have to compute and store the actual value
206211
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-
222212 let idx = adt_def. variant_index_with_ctor_id ( variant_ctor_id) ;
223213 let ( discr_did, discr_offset) = adt_def. discriminant_def_for_variant ( idx) ;
224214
@@ -255,6 +245,10 @@ impl<'tcx> Cx<'tcx> {
255245 } ;
256246
257247 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) }
258252 }
259253 }
260254
0 commit comments