Version 1.6.82

Cherry pick: Look for reservation state in frontier when missing
CL: https://r8-review.googlesource.com/49483

Bug: 150844383
Change-Id: I3d2d53aa2fb44da62056cc747db4a90890964bad
diff --git a/src/main/java/com/android/tools/r8/Version.java b/src/main/java/com/android/tools/r8/Version.java
index 8c6b22e..cc39999 100644
--- a/src/main/java/com/android/tools/r8/Version.java
+++ b/src/main/java/com/android/tools/r8/Version.java
@@ -11,7 +11,7 @@
 
   // This field is accessed from release scripts using simple pattern matching.
   // Therefore, changing this field could break our release scripts.
-  public static final String LABEL = "1.6.81";
+  public static final String LABEL = "1.6.82";
 
   private Version() {
   }
diff --git a/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java b/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java
index d8fc1c6..f495453 100644
--- a/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java
+++ b/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java
@@ -307,7 +307,9 @@
           parentState = getOrAllocateMethodNamingStates(holder.superType);
         }
       }
-      MethodReservationState<?> reservationState = reservationStates.get(type);
+      // There can be gaps in the reservation states if a library class extends a program class.
+      // See b/150325706 for more information.
+      MethodReservationState<?> reservationState = findReservationStateInHierarchy(type);
       assert reservationState != null : "Could not find reservation state for " + type.toString();
       namingState = parentState.createChild(reservationState);
       namingStates.put(type, namingState);
@@ -315,6 +317,21 @@
     return namingState;
   }
 
+  private MethodReservationState<?> findReservationStateInHierarchy(DexType type) {
+    MethodReservationState<?> reservationState = reservationStates.get(type);
+    if (reservationState != null) {
+      return reservationState;
+    }
+    // If we cannot find the reservation state, which is a result from a library class extending
+    // a program class. The gap is tracked in the frontier state.
+    assert frontiers.containsKey(type);
+    DexType frontierType = frontiers.get(type);
+    reservationState = reservationStates.get(frontierType);
+    assert reservationState != null
+        : "Could not find reservation state for frontier type " + frontierType.toString();
+    return reservationState;
+  }
+
   // Shuffles the given methods if assertions are enabled and deterministic debugging is disabled.
   // Used to ensure that the generated output is deterministic.
   private static Iterable<DexEncodedMethod> shuffleMethods(
diff --git a/src/test/java/com/android/tools/r8/naming/LibraryClassInheritingFromProgramClassNamingTest.java b/src/test/java/com/android/tools/r8/naming/LibraryClassInheritingFromProgramClassNamingTest.java
new file mode 100644
index 0000000..3b176f9
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/LibraryClassInheritingFromProgramClassNamingTest.java
@@ -0,0 +1,94 @@
+// Copyright (c) 2020, 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.naming;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.R8TestCompileResult;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+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 LibraryClassInheritingFromProgramClassNamingTest extends TestBase {
+
+  private final TestParameters parameters;
+  private final Class[] LIBRARY_CLASSES =
+      new Class[] {AndroidTestCase.class, ApplicationTestCase.class};
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  public LibraryClassInheritingFromProgramClassNamingTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testR8() throws ExecutionException, CompilationFailedException, IOException {
+    R8TestCompileResult libraryResult =
+        testForR8(parameters.getBackend())
+            .setMinApi(parameters.getApiLevel())
+            .addProgramClasses(LIBRARY_CLASSES)
+            .addProgramClasses(
+                I.class, Assert.class, TestCase.class, ApplicationTestCaseInProgram.class)
+            .addKeepAllClassesRule()
+            .compile();
+    testForR8Compat(parameters.getBackend())
+        .addLibraryClasses(LIBRARY_CLASSES)
+        .addLibraryFiles(runtimeJar(parameters.getBackend()))
+        .addProgramClasses(
+            I.class, Assert.class, TestCase.class, ApplicationTestCaseInProgram.class, Main.class)
+        .addKeepClassAndMembersRulesWithAllowObfuscation(
+            I.class, Assert.class, TestCase.class, ApplicationTestCaseInProgram.class)
+        .addKeepMainRule(Main.class)
+        .setMinApi(parameters.getApiLevel())
+        .compile()
+        .addRunClasspathFiles(libraryResult.writeToZip())
+        .run(parameters.getRuntime(), Main.class)
+        // TODO(b/150844383): Pin supertypes from re-entry point into library.
+        .assertFailureWithErrorThatThrows(VerifyError.class);
+  }
+
+  public interface I {
+
+    void foo();
+  }
+
+  public static class Assert implements I {
+
+    @Override
+    public void foo() {
+      System.out.println("Assert.foo");
+    }
+  }
+
+  public static class TestCase extends Assert {
+
+    @Override
+    public void foo() {
+      System.out.println("TestCase.foo");
+    }
+  }
+
+  public static class AndroidTestCase extends TestCase {}
+
+  public static class ApplicationTestCase extends AndroidTestCase {}
+
+  public static class ApplicationTestCaseInProgram extends AndroidTestCase {}
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      new ApplicationTestCaseInProgram().foo();
+    }
+  }
+}