Version 2.0.38

Cherry-pick: Abort member rebinding on missing library class definition.
CL: https://r8-review.googlesource.com/c/r8/+/49087
Bug: 149890887
Change-Id: Iab6dc393295ed71d0be2f879a602678060a4c1f6
diff --git a/src/main/java/com/android/tools/r8/Version.java b/src/main/java/com/android/tools/r8/Version.java
index e3ab84c..543adc6 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 = "2.0.37";
+  public static final String LABEL = "2.0.38";
 
   private Version() {
   }
diff --git a/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java b/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java
index 703deea..4d20f6b 100644
--- a/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java
+++ b/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java
@@ -53,7 +53,9 @@
     } else {
       newHolder = firstLibraryClass(target.holder, original.holder);
     }
-    return appView.dexItemFactory().createMethod(newHolder, original.proto, original.name);
+    return newHolder == null
+        ? original
+        : appView.dexItemFactory().createMethod(newHolder, original.proto, original.name);
   }
 
   private DexField validTargetFor(DexField target, DexField original,
@@ -69,12 +71,17 @@
     } else {
       newHolder = firstLibraryClass(target.holder, original.holder);
     }
-    return appView.dexItemFactory().createField(newHolder, original.type, original.name);
+    return newHolder == null
+        ? original
+        : appView.dexItemFactory().createField(newHolder, original.type, original.name);
   }
 
   private <T> DexType firstLibraryClassForInterfaceTarget(T target, DexType current,
       BiFunction<DexClass, T, ?> lookup) {
     DexClass clazz = appView.definitionFor(current);
+    if (clazz == null) {
+      return null;
+    }
     Object potential = lookup.apply(clazz, target);
     if (potential != null) {
       // Found, return type.
diff --git a/src/test/java/com/android/tools/r8/TestBase.java b/src/test/java/com/android/tools/r8/TestBase.java
index ea7d4b3..3166939 100644
--- a/src/test/java/com/android/tools/r8/TestBase.java
+++ b/src/test/java/com/android/tools/r8/TestBase.java
@@ -1391,7 +1391,7 @@
     return JarBuilder.builder(temp);
   }
 
-  public Collection<Path> buildOnDexRuntime(TestParameters parameters, Collection<Path> paths)
+  public List<Path> buildOnDexRuntime(TestParameters parameters, List<Path> paths)
       throws CompilationFailedException, IOException {
     if (parameters.isCfRuntime()) {
       return paths;
@@ -1404,7 +1404,7 @@
             .writeToZip());
   }
 
-  public Collection<Path> buildOnDexRuntime(TestParameters parameters, Path... paths)
+  public List<Path> buildOnDexRuntime(TestParameters parameters, Path... paths)
       throws IOException, CompilationFailedException {
     return buildOnDexRuntime(parameters, Arrays.asList(paths));
   }
diff --git a/src/test/java/com/android/tools/r8/regress/b149890887/MissingLibraryTargetTest.java b/src/test/java/com/android/tools/r8/regress/b149890887/MissingLibraryTargetTest.java
new file mode 100644
index 0000000..927decb
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/regress/b149890887/MissingLibraryTargetTest.java
@@ -0,0 +1,105 @@
+// 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.regress.b149890887;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import com.android.tools.r8.utils.StringUtils;
+import com.google.common.collect.ImmutableList;
+import java.nio.file.Path;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class MissingLibraryTargetTest extends TestBase {
+
+  static final String EXPECTED = StringUtils.lines("A::foo");
+
+  private final TestParameters parameters;
+
+  @Parameterized.Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimes().withAllApiLevels().build();
+  }
+
+  public MissingLibraryTargetTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  private static Class<?> MAIN = TestClass.class;
+  private static List<Class<?>> PROGRAM = ImmutableList.of(MAIN, DerivedProgramClass.class);
+  private static List<Class<?>> LIBRARY =
+      ImmutableList.of(PresentLibraryInterface.class, PresentLibraryClass.class);
+
+  private List<Path> runtimeClasspath() throws Exception {
+    return buildOnDexRuntime(
+        parameters,
+        jarTestClasses(
+            ImmutableList.<Class<?>>builder()
+                .addAll(LIBRARY)
+                .add(MissingLibraryClass.class)
+                .build()));
+  }
+
+  private boolean isDesugaring() {
+    return parameters.isDexRuntime() && parameters.getApiLevel().isLessThan(AndroidApiLevel.N);
+  }
+
+  @Test
+  public void testReference() throws Exception {
+    testForRuntime(parameters)
+        .addProgramClasses(PROGRAM)
+        .addRunClasspathFiles(runtimeClasspath())
+        .run(parameters.getRuntime(), MAIN)
+        .assertSuccessWithOutput(EXPECTED);
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .addProgramClasses(PROGRAM)
+        .addKeepMainRule(MAIN)
+        .addClasspathClasses(LIBRARY)
+        .setMinApi(parameters.getApiLevel())
+        .addKeepRules("-dontwarn")
+        .compile()
+        .addRunClasspathFiles(runtimeClasspath())
+        .run(parameters.getRuntime(), MAIN)
+        .assertSuccessWithOutput(EXPECTED);
+  }
+
+  // Non-present class with the actual definition.
+  static class MissingLibraryClass {
+    public void foo() {
+      System.out.println("A::foo");
+    }
+  }
+
+  // Present library interface declaring the method. This is needed to hit the member rebinding
+  // issue. If not here the initial resolution will fail and no search will be done.
+  interface PresentLibraryInterface {
+    void foo();
+  }
+
+  // Present library class to trigger the search for the "first library definition".
+  static class PresentLibraryClass extends MissingLibraryClass implements PresentLibraryInterface {
+    // Intentionally empty.
+  }
+
+  // Program type that needs to be targeted to initiate rebinding.
+  static class DerivedProgramClass extends PresentLibraryClass {
+    // Intentionally empty.
+  }
+
+  static class TestClass {
+
+    public static void main(String[] args) {
+      new DerivedProgramClass().foo();
+    }
+  }
+}