Stream: t-compiler/wg-mir-opt

Topic: [const-prop] Fix ICE due to underflow when calculating enum


Wesley Wiser (Dec 02 2019 at 03:15, on Zulip):

Hey @oli I don't know if you saw my comment, but implementing enum variant checking like codegen does is a much neater fix and also resolves the issue:

diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs
index 5b263f76801..f709dfa43ee 100644
--- a/src/librustc_mir/interpret/place.rs
+++ b/src/librustc_mir/interpret/place.rs
@@ -1040,21 +1040,18 @@ where
     ) -> InterpResult<'tcx> {
         let variant_scalar = Scalar::from_u32(variant_index.as_u32()).into();

+        if dest.layout.for_variant(self, variant_index).abi.is_uninhabited() {
+            throw_ub!(InvalidDiscriminant(variant_scalar));
+        }
+
         match dest.layout.variants {
-            layout::Variants::Single { index } => {
-                if index != variant_index {
-                    throw_ub!(InvalidDiscriminant(variant_scalar));
-                }
-            }
+            layout::Variants::Single { .. } => { }
             layout::Variants::Multiple {
                 discr_kind: layout::DiscriminantKind::Tag,
                 discr: ref discr_layout,
                 discr_index,
                 ..
             } => {
-                if !dest.layout.ty.variant_range(*self.tcx).unwrap().contains(&variant_index) {
-                    throw_ub!(InvalidDiscriminant(variant_scalar));
-                }
                 let discr_val =
                     dest.layout.ty.discriminant_for_variant(*self.tcx, variant_index).unwrap().val;

@@ -1077,9 +1074,6 @@ where
                 discr_index,
                 ..
             } => {
-                if !variant_index.as_usize() < dest.layout.ty.ty_adt_def().unwrap().variants.len() {
-                    throw_ub!(InvalidDiscriminant(variant_scalar));
-                }
                 if variant_index != dataful_variant {
                     let variants_start = niche_variants.start().as_u32();
                     let variant_index_relative = variant_index.as_u32()

How do you feel about that as a fix?

oli (Dec 02 2019 at 08:08, on Zulip):

Oh yea, that is a lot better

Last update: Dec 12 2019 at 00:45UTC