Add reproduction for repackaging picking merged and pruned class

Bug: 183111898
Change-Id: Ibb588af0e771124f65c02cb6dff787b1122d951c
diff --git a/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java b/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java
index 0f7b951..ceac26a 100644
--- a/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java
+++ b/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java
@@ -143,7 +143,8 @@
         if (definitionFor(type) == null && definitionFor(renamed) != null) {
           continue;
         }
-        assert definitionFor(type).type == renamed || definitionFor(renamed) != null;
+        assert definitionFor(type).type == renamed || definitionFor(renamed) != null
+            : "The lens and app is inconsistent";
       }
     }
     return true;
diff --git a/src/test/java/com/android/tools/r8/classmerging/horizontal/MergeWithRepackageNameCollissionTest.java b/src/test/java/com/android/tools/r8/classmerging/horizontal/MergeWithRepackageNameCollissionTest.java
new file mode 100644
index 0000000..7f7e655
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/classmerging/horizontal/MergeWithRepackageNameCollissionTest.java
@@ -0,0 +1,111 @@
+// 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.classmerging.horizontal;
+
+import static org.hamcrest.CoreMatchers.containsString;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoHorizontalClassMerging;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.google.common.collect.ImmutableList;
+import java.util.Collection;
+import org.junit.Test;
+import org.junit.runners.Parameterized;
+
+public class MergeWithRepackageNameCollissionTest extends HorizontalClassMergingTestBase {
+
+  @Parameterized.Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withDexRuntimes().withAllApiLevels().build();
+  }
+
+  public MergeWithRepackageNameCollissionTest(TestParameters parameters) {
+    super(parameters);
+  }
+
+  @Test(expected = CompilationFailedException.class)
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .addProgramClassFileData(getProgramFileData())
+        .addProgramClasses(I.class, Runner.class)
+        .addKeepMainRule(Main.class)
+        .addKeepClassAndMembersRules(I.class, Runner.class)
+        .setMinApi(parameters.getApiLevel())
+        .enableNoHorizontalClassMergingAnnotations()
+        .enableInliningAnnotations()
+        .compileWithExpectedDiagnostics(
+            diagnostics -> {
+              // TODO(b/183111898): Should not fail.
+              diagnostics.assertErrorMessageThatMatches(
+                  containsString("The lens and app is inconsistent"));
+            });
+  }
+
+  private Collection<byte[]> getProgramFileData() throws Exception {
+    return ImmutableList.of(
+        transformer(A.class).setClassDescriptor("La/a;").transform(),
+        transformer(B.class).setClassDescriptor("Lb/a;").setSuper("La/a;").transform(),
+        transformer(D.class).setClassDescriptor("Lc/a;").transform(),
+        transformer(Main.class)
+            .replaceClassDescriptorInMethodInstructions(descriptor(A.class), "La/a;")
+            .replaceClassDescriptorInMethodInstructions(descriptor(B.class), "Lb/a;")
+            .replaceClassDescriptorInMethodInstructions(descriptor(D.class), "Lc/a;")
+            .transform());
+  }
+
+  public static /* will be: a.a */ class A {
+
+    @NeverInline
+    public static void foo() {
+      System.out.println("A::foo");
+    }
+  }
+
+  // a.a will be merged into b.a and thereby produce a mapping from a.a -> b.a in our lens.
+  public static /* will be: b.a */ class B extends A {}
+
+  // c.a will be repackaged to a.a
+  @NoHorizontalClassMerging
+  public static /* will be: c.a */ class D {
+
+    @NeverInline
+    public static void baz() {
+      System.out.println("C::baz");
+    }
+  }
+
+  public interface I {
+
+    void run();
+  }
+
+  public static class Runner {
+
+    @NeverInline
+    static void callI(I i) {
+      i.run();
+    }
+  }
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      D.baz();
+      callFoo(null);
+      Runner.callI(
+          () -> {
+            System.out.println("Hello World");
+          });
+    }
+
+    public static void callFoo(Object obj) {
+      if (obj != null) {
+        B.foo();
+      }
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/utils/codeinspector/HorizontallyMergedClassesInspector.java b/src/test/java/com/android/tools/r8/utils/codeinspector/HorizontallyMergedClassesInspector.java
index 91fb002..e6fa548 100644
--- a/src/test/java/com/android/tools/r8/utils/codeinspector/HorizontallyMergedClassesInspector.java
+++ b/src/test/java/com/android/tools/r8/utils/codeinspector/HorizontallyMergedClassesInspector.java
@@ -4,7 +4,6 @@
 
 package com.android.tools.r8.utils.codeinspector;
 
-import static com.android.tools.r8.TestBase.toDexType;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
@@ -72,11 +71,20 @@
     return this;
   }
 
+  public HorizontallyMergedClassesInspector assertClassesMerged(Class<?>... classes) {
+    return assertClassesMerged(Arrays.asList(classes));
+  }
+
   public HorizontallyMergedClassesInspector assertClassesMerged(Collection<Class<?>> classes) {
     return assertTypesMerged(classes.stream().map(this::toDexType).collect(Collectors.toList()));
   }
 
   public HorizontallyMergedClassesInspector assertClassReferencesMerged(
+      ClassReference... classReferences) {
+    return assertClassReferencesMerged(Arrays.asList(classReferences));
+  }
+
+  public HorizontallyMergedClassesInspector assertClassReferencesMerged(
       Collection<ClassReference> classReferences) {
     return assertTypesMerged(
         classReferences.stream().map(this::toDexType).collect(Collectors.toList()));