Version 1.6.74

Cherry-pick: Abort member rebinding on missing library class definition.
CL: https://r8-review.googlesource.com/c/r8/+/49087
Bug: 149890887
Change-Id: Ife97fc579a45594f6aeb4cbe9959c0b77e763d46
diff --git a/src/main/java/com/android/tools/r8/Version.java b/src/main/java/com/android/tools/r8/Version.java
index 7fce0ed..81b0ce0 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.73";
+  public static final String LABEL = "1.6.74";
 
   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 760904a..b1114fa 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 3f140b8..abdca3c 100644
--- a/src/test/java/com/android/tools/r8/TestBase.java
+++ b/src/test/java/com/android/tools/r8/TestBase.java
@@ -1236,4 +1236,34 @@
   public JarBuilder jarBuilder() throws IOException {
     return JarBuilder.builder(temp);
   }
+
+  public List<Path> buildOnDexRuntime(TestParameters parameters, List<Path> paths)
+      throws CompilationFailedException, IOException {
+    if (parameters.isCfRuntime()) {
+      return paths;
+    }
+    return Collections.singletonList(
+        testForD8()
+            .addProgramFiles(paths)
+            .setMinApi(parameters.getApiLevel())
+            .compile()
+            .writeToZip());
+  }
+
+  public List<Path> buildOnDexRuntime(TestParameters parameters, Path... paths)
+      throws IOException, CompilationFailedException {
+    return buildOnDexRuntime(parameters, Arrays.asList(paths));
+  }
+
+  public static String binaryName(Class<?> clazz) {
+    return DescriptorUtils.getBinaryNameFromJavaType(typeName(clazz));
+  }
+
+  public static String descriptor(Class<?> clazz) {
+    return DescriptorUtils.javaTypeToDescriptor(typeName(clazz));
+  }
+
+  public static String typeName(Class<?> clazz) {
+    return clazz.getTypeName();
+  }
 }
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..8268374
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/regress/b149890887/MissingLibraryTargetTest.java
@@ -0,0 +1,92 @@
+// 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()));
+  }
+
+  @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();
+    }
+  }
+}