Check for holder being array type before looking it up

Bug: 185735455
Change-Id: Ifcd113578a025413ce29e5d78212663173b0728a
diff --git a/src/main/java/com/android/tools/r8/dex/CodeToKeep.java b/src/main/java/com/android/tools/r8/dex/CodeToKeep.java
index f4e85a8..2261228 100644
--- a/src/main/java/com/android/tools/r8/dex/CodeToKeep.java
+++ b/src/main/java/com/android/tools/r8/dex/CodeToKeep.java
@@ -79,9 +79,12 @@
 
     @Override
     void recordMethod(DexMethod method) {
-      if (shouldKeep(method.holder)) {
-        keepClass(method.holder);
-        toKeep.get(method.holder).methods.add(method);
+      DexType baseType = method.holder.toBaseType(options.dexItemFactory());
+      if (shouldKeep(baseType)) {
+        keepClass(baseType);
+        if (!method.holder.isArrayType()) {
+          toKeep.get(method.holder).methods.add(method);
+        }
       }
       if (shouldKeep(method.proto.returnType)) {
         keepClass(method.proto.returnType);
@@ -95,9 +98,12 @@
 
     @Override
     void recordField(DexField field) {
-      if (shouldKeep(field.holder)) {
-        keepClass(field.holder);
-        toKeep.get(field.holder).fields.add(field);
+      DexType baseType = field.holder.toBaseType(options.dexItemFactory());
+      if (shouldKeep(baseType)) {
+        keepClass(baseType);
+        if (!field.holder.isArrayType()) {
+          toKeep.get(field.holder).fields.add(field);
+        }
       }
       if (shouldKeep(field.type)) {
         keepClass(field.type);
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryCloneTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryCloneTest.java
index 069ef26..73e5d67 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryCloneTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryCloneTest.java
@@ -4,9 +4,6 @@
 
 package com.android.tools.r8.desugar.desugaredlibrary;
 
-import static com.android.tools.r8.DiagnosticsMatcher.diagnosticException;
-
-import com.android.tools.r8.CompilationFailedException;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.utils.BooleanUtils;
 import java.time.DayOfWeek;
@@ -21,6 +18,7 @@
 
   private final TestParameters parameters;
   private final boolean shrinkDesugaredLibrary;
+  private final String EXPECTED = "Just another manic MONDAY";
 
   @Parameters(name = "{1}, shrinkDesugaredLibrary: {0}")
   public static List<Object[]> data() {
@@ -33,21 +31,24 @@
     this.shrinkDesugaredLibrary = shrinkDesugaredLibrary;
   }
 
-  @Test(expected = CompilationFailedException.class)
+  @Test
   public void testD8() throws Exception {
     KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
     testForD8()
         .addInnerClasses(getClass())
         .setMinApi(parameters.getApiLevel())
         .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
-        .compileWithExpectedDiagnostics(
-            diagnostics ->
-                // TODO(b/185735455): Should not throw an error.
-                diagnostics.assertErrorThatMatches(
-                    diagnosticException(NullPointerException.class)));
+        .compile()
+        .addDesugaredCoreLibraryRunClassPath(
+            this::buildDesugaredLibrary,
+            parameters.getApiLevel(),
+            keepRuleConsumer.get(),
+            shrinkDesugaredLibrary)
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines(EXPECTED);
   }
 
-  @Test(expected = CompilationFailedException.class)
+  @Test
   public void testR8() throws Exception {
     KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
     testForR8(parameters.getBackend())
@@ -55,11 +56,14 @@
         .addKeepMainRule(Main.class)
         .setMinApi(parameters.getApiLevel())
         .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
-        .compileWithExpectedDiagnostics(
-            diagnostics ->
-                // TODO(b/185735455): Should not throw an error.
-                diagnostics.assertErrorThatMatches(
-                    diagnosticException(NullPointerException.class)));
+        .compile()
+        .addDesugaredCoreLibraryRunClassPath(
+            this::buildDesugaredLibrary,
+            parameters.getApiLevel(),
+            keepRuleConsumer.get(),
+            shrinkDesugaredLibrary)
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines(EXPECTED);
   }
 
   public static class Main {