Add reproduction of NPE when trying to keep method on desugared arr

Bug: 185735455
Change-Id: I9a7a88dcab244393564919cc3759e885eb507a73
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
new file mode 100644
index 0000000..069ef26
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryCloneTest.java
@@ -0,0 +1,81 @@
+// Copyright (c) 2021, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+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;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class DesugaredLibraryCloneTest extends DesugaredLibraryTestBase {
+
+  private final TestParameters parameters;
+  private final boolean shrinkDesugaredLibrary;
+
+  @Parameters(name = "{1}, shrinkDesugaredLibrary: {0}")
+  public static List<Object[]> data() {
+    return buildParameters(
+        getTestParameters().withDexRuntimes().withAllApiLevels().build(), BooleanUtils.values());
+  }
+
+  public DesugaredLibraryCloneTest(TestParameters parameters, boolean shrinkDesugaredLibrary) {
+    this.parameters = parameters;
+    this.shrinkDesugaredLibrary = shrinkDesugaredLibrary;
+  }
+
+  @Test(expected = CompilationFailedException.class)
+  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)));
+  }
+
+  @Test(expected = CompilationFailedException.class)
+  public void testR8() throws Exception {
+    KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
+    testForR8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .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)));
+  }
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      DayOfWeek[] dayOfWeeks = new DayOfWeek[args.length + 1];
+      if (args.length == 0) {
+        dayOfWeeks[0] = DayOfWeek.MONDAY;
+      }
+      print(dayOfWeeks.clone());
+    }
+
+    public static void print(DayOfWeek[] arr) {
+      if (arr.length > 0) {
+        System.out.println("Just another manic " + arr[0]);
+      }
+    }
+  }
+}