Do not create EnumSwitchInfo in presence of unexpected switch key

Bug: 149554602
Change-Id: I3d228425824094183281d066619048d3b50cad84
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/enums/EnumValueOptimizer.java b/src/main/java/com/android/tools/r8/ir/optimize/enums/EnumValueOptimizer.java
index c8122470..ee749f0 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/enums/EnumValueOptimizer.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/enums/EnumValueOptimizer.java
@@ -153,7 +153,9 @@
       Int2IntMap targetMap = new Int2IntArrayMap();
       for (int i = 0; i < switchInsn.numberOfKeys(); i++) {
         assert switchInsn.targetBlockIndices()[i] != switchInsn.getFallthroughBlockIndex();
-        EnumValueInfo valueInfo = info.valueInfoMap.get(info.indexMap.get(switchInsn.getKey(i)));
+        int key = switchInsn.getKey(i);
+        DexField field = info.indexMap.get(key);
+        EnumValueInfo valueInfo = info.valueInfoMap.get(field);
         targetMap.put(valueInfo.ordinal, switchInsn.targetBlockIndices()[i]);
       }
       int[] keys = targetMap.keySet().toIntArray();
@@ -224,8 +226,7 @@
    * and extracts the components and the index and ordinal maps. See {@link EnumInfoMapCollector}
    * and {@link SwitchMapCollector} for details.
    */
-  private EnumSwitchInfo analyzeSwitchOverEnum(Instruction switchInsn) {
-    AppInfoWithLiveness appInfo = appView.appInfo();
+  private EnumSwitchInfo analyzeSwitchOverEnum(IntSwitch switchInsn) {
     Instruction input = switchInsn.inValues().get(0).definition;
     if (input == null || !input.isArrayGet()) {
       return null;
@@ -237,8 +238,8 @@
     }
     InvokeVirtual ordinalInvoke = index.asInvokeVirtual();
     DexMethod ordinalMethod = ordinalInvoke.getInvokedMethod();
-    DexClass enumClass = appInfo.definitionFor(ordinalMethod.holder);
-    DexItemFactory dexItemFactory = appInfo.dexItemFactory();
+    DexClass enumClass = appView.definitionFor(ordinalMethod.holder);
+    DexItemFactory dexItemFactory = appView.dexItemFactory();
     // After member rebinding, enumClass will be the actual java.lang.Enum class.
     if (enumClass == null
         || (!enumClass.accessFlags.isEnum() && enumClass.type != dexItemFactory.enumType)
@@ -252,14 +253,18 @@
       return null;
     }
     StaticGet staticGet = array.asStaticGet();
-    Int2ReferenceMap<DexField> indexMap = appInfo.getSwitchMapFor(staticGet.getField());
+    Int2ReferenceMap<DexField> indexMap = appView.appInfo().getSwitchMapFor(staticGet.getField());
     if (indexMap == null || indexMap.isEmpty()) {
       return null;
     }
-    // Due to member rebinding, only the fields are certain to provide the actual enums
-    // class.
+    for (int key : switchInsn.getKeys()) {
+      if (!indexMap.containsKey(key)) {
+        return null;
+      }
+    }
+    // Due to member rebinding, only the fields are certain to provide the actual enums class.
     DexType enumType = indexMap.values().iterator().next().holder;
-    Map<DexField, EnumValueInfo> valueInfoMap = appInfo.getEnumValueInfoMapFor(enumType);
+    Map<DexField, EnumValueInfo> valueInfoMap = appView.appInfo().getEnumValueInfoMapFor(enumType);
     if (valueInfoMap == null) {
       return null;
     }