Reproduce non-public override of public method after class merging

Bug: 203446070
Change-Id: I9790ba602485babe0a2d924b0b74c9af341c2aea
diff --git a/src/test/java/com/android/tools/r8/classmerging/horizontal/NonPublicOverrideOfPublicMethodAfterClassMergingTest.java b/src/test/java/com/android/tools/r8/classmerging/horizontal/NonPublicOverrideOfPublicMethodAfterClassMergingTest.java
new file mode 100644
index 0000000..8093d0c
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/classmerging/horizontal/NonPublicOverrideOfPublicMethodAfterClassMergingTest.java
@@ -0,0 +1,117 @@
+// 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 com.android.tools.r8.utils.codeinspector.Matchers.isExtending;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPackagePrivate;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPublic;
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.android.tools.r8.NeverClassInline;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoHorizontalClassMerging;
+import com.android.tools.r8.NoUnusedInterfaceRemoval;
+import com.android.tools.r8.NoVerticalClassMerging;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class NonPublicOverrideOfPublicMethodAfterClassMergingTest extends TestBase {
+
+  @Parameter(0)
+  public TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection parameters() {
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  @Test
+  public void test() throws Exception {
+    testForR8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .addKeepMainRule(Main.class)
+        .addHorizontallyMergedClassesInspector(
+            inspector ->
+                inspector.assertIsCompleteMergeGroup(I.class, J.class).assertNoOtherClassesMerged())
+        .enableInliningAnnotations()
+        .enableNeverClassInliningAnnotations()
+        .enableNoHorizontalClassMergingAnnotations()
+        .enableNoUnusedInterfaceRemovalAnnotations()
+        .enableNoVerticalClassMergingAnnotations()
+        .setMinApi(parameters.getApiLevel())
+        .compile()
+        .inspect(
+            inspector -> {
+              ClassSubject iClassSubject = inspector.clazz(I.class);
+              assertThat(iClassSubject, isPresent());
+              assertThat(iClassSubject.uniqueMethodWithName("m"), allOf(isPresent(), isPublic()));
+
+              ClassSubject aClassSubject = inspector.clazz(A.class);
+              assertThat(aClassSubject, isPresent());
+              assertThat(aClassSubject, isExtending(iClassSubject));
+              assertThat(
+                  aClassSubject.uniqueMethodWithName("m"), allOf(isPresent(), isPackagePrivate()));
+            })
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines("A.m()", "B.m()");
+  }
+
+  static class Main {
+
+    public static void main(String[] args) {
+      new A().m();
+      (System.currentTimeMillis() > 0 ? new B() : new C()).m();
+    }
+  }
+
+  @NoUnusedInterfaceRemoval
+  @NoVerticalClassMerging
+  abstract static class I {}
+
+  @NoUnusedInterfaceRemoval
+  @NoVerticalClassMerging
+  abstract static class J {
+
+    public abstract void m();
+  }
+
+  @NeverClassInline
+  @NoHorizontalClassMerging
+  static class A extends I {
+
+    @NeverInline
+    void m() {
+      System.out.println("A.m()");
+    }
+  }
+
+  @NoHorizontalClassMerging
+  static class B extends J {
+
+    @Override
+    public void m() {
+      System.out.println("B.m()");
+    }
+  }
+
+  @NoHorizontalClassMerging
+  static class C extends J {
+
+    @Override
+    public void m() {
+      System.out.println("C.m()");
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/classmerging/horizontal/interfaces/IllegalSiblingAfterInterfaceMergingTest.java b/src/test/java/com/android/tools/r8/classmerging/horizontal/interfaces/IllegalSiblingAfterInterfaceMergingTest.java
new file mode 100644
index 0000000..a75fd7e
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/classmerging/horizontal/interfaces/IllegalSiblingAfterInterfaceMergingTest.java
@@ -0,0 +1,140 @@
+// 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.interfaces;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isExtending;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isImplementing;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPackagePrivate;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPublic;
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.android.tools.r8.NeverClassInline;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoHorizontalClassMerging;
+import com.android.tools.r8.NoUnusedInterfaceRemoval;
+import com.android.tools.r8.NoVerticalClassMerging;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class IllegalSiblingAfterInterfaceMergingTest extends TestBase {
+
+  @Parameter(0)
+  public TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection parameters() {
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  @Test
+  public void test() throws Exception {
+    testForR8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .addKeepMainRule(Main.class)
+        .addHorizontallyMergedClassesInspector(
+            inspector ->
+                inspector.assertIsCompleteMergeGroup(I.class, J.class).assertNoOtherClassesMerged())
+        .enableInliningAnnotations()
+        .enableNeverClassInliningAnnotations()
+        .enableNoHorizontalClassMergingAnnotations()
+        .enableNoUnusedInterfaceRemovalAnnotations()
+        .enableNoVerticalClassMergingAnnotations()
+        .setMinApi(parameters.getApiLevel())
+        .compile()
+        .inspect(
+            inspector -> {
+              ClassSubject iClassSubject = inspector.clazz(I.class);
+              assertThat(iClassSubject, isPresent());
+              assertThat(iClassSubject.uniqueMethodWithName("m"), allOf(isPresent(), isPublic()));
+
+              ClassSubject a0ClassSubject = inspector.clazz(A0.class);
+              assertThat(a0ClassSubject, isPresent());
+              assertThat(
+                  a0ClassSubject.uniqueMethodWithName("m"), allOf(isPresent(), isPackagePrivate()));
+
+              ClassSubject aClassSubject = inspector.clazz(A.class);
+              assertThat(aClassSubject, isPresent());
+              assertThat(aClassSubject, isExtending(a0ClassSubject));
+              assertThat(aClassSubject, isImplementing(iClassSubject));
+            })
+        .run(parameters.getRuntime(), Main.class)
+        // TODO(b/203446070): Should always succeed.
+        .applyIf(
+            parameters.isCfRuntime(),
+            runResult -> runResult.assertSuccessWithOutputLines("A.m()", "B.m()"),
+            runResult ->
+                runResult.applyIf(
+                    parameters.getDexRuntimeVersion().isDalvik(),
+                    ignore ->
+                        runResult.assertFailureWithErrorThatThrows(NoClassDefFoundError.class),
+                    ignore ->
+                        runResult.assertFailureWithErrorThatThrows(IllegalAccessError.class)));
+  }
+
+  static class Main {
+
+    public static void main(String[] args) {
+      new A().m();
+      (System.currentTimeMillis() > 0 ? new B() : new C()).m();
+    }
+  }
+
+  @NoUnusedInterfaceRemoval
+  @NoVerticalClassMerging
+  interface I {}
+
+  // Should not be merged into I, since I has a subclass with a package private method signature
+  // `void m()`.
+  @NoUnusedInterfaceRemoval
+  @NoVerticalClassMerging
+  interface J {
+
+    void m();
+  }
+
+  @NoHorizontalClassMerging
+  @NoVerticalClassMerging
+  static class A0 {
+
+    // Intentionally package private. If J is merged into I then this is an illegal override of
+    // I.m().
+    @NeverInline
+    void m() {
+      System.out.println("A.m()");
+    }
+  }
+
+  @NeverClassInline
+  @NoHorizontalClassMerging
+  static class A extends A0 implements I {}
+
+  @NoHorizontalClassMerging
+  static class B implements J {
+
+    @Override
+    public void m() {
+      System.out.println("B.m()");
+    }
+  }
+
+  @NoHorizontalClassMerging
+  static class C implements J {
+
+    @Override
+    public void m() {
+      System.out.println("C.m()");
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/utils/codeinspector/AbsentClassSubject.java b/src/test/java/com/android/tools/r8/utils/codeinspector/AbsentClassSubject.java
index 4ea9a82..a6d7085 100644
--- a/src/test/java/com/android/tools/r8/utils/codeinspector/AbsentClassSubject.java
+++ b/src/test/java/com/android/tools/r8/utils/codeinspector/AbsentClassSubject.java
@@ -90,6 +90,11 @@
   }
 
   @Override
+  public boolean isExtending(ClassSubject subject) {
+    throw new Unreachable("Cannot determine if an absent class is extending a given class");
+  }
+
+  @Override
   public boolean isImplementing(ClassSubject subject) {
     throw new Unreachable("Cannot determine if an absent class is implementing a given interface");
   }
diff --git a/src/test/java/com/android/tools/r8/utils/codeinspector/ClassSubject.java b/src/test/java/com/android/tools/r8/utils/codeinspector/ClassSubject.java
index b100332..0181860 100644
--- a/src/test/java/com/android/tools/r8/utils/codeinspector/ClassSubject.java
+++ b/src/test/java/com/android/tools/r8/utils/codeinspector/ClassSubject.java
@@ -175,6 +175,8 @@
 
   public abstract boolean isAnnotation();
 
+  public abstract boolean isExtending(ClassSubject subject);
+
   public abstract boolean isImplementing(ClassSubject subject);
 
   public abstract boolean isImplementing(Class<?> clazz);
diff --git a/src/test/java/com/android/tools/r8/utils/codeinspector/FoundClassSubject.java b/src/test/java/com/android/tools/r8/utils/codeinspector/FoundClassSubject.java
index 318f0d9..e04c6c1 100644
--- a/src/test/java/com/android/tools/r8/utils/codeinspector/FoundClassSubject.java
+++ b/src/test/java/com/android/tools/r8/utils/codeinspector/FoundClassSubject.java
@@ -294,6 +294,11 @@
   }
 
   @Override
+  public boolean isExtending(ClassSubject subject) {
+    return getSuperClass().getDexProgramClass().getType() == subject.getDexProgramClass().getType();
+  }
+
+  @Override
   public boolean isInterface() {
     return dexClass.isInterface();
   }
diff --git a/src/test/java/com/android/tools/r8/utils/codeinspector/Matchers.java b/src/test/java/com/android/tools/r8/utils/codeinspector/Matchers.java
index edeed5a..66c1dde 100644
--- a/src/test/java/com/android/tools/r8/utils/codeinspector/Matchers.java
+++ b/src/test/java/com/android/tools/r8/utils/codeinspector/Matchers.java
@@ -504,6 +504,22 @@
     };
   }
 
+  public static Matcher<ClassSubject> isExtending(ClassSubject superSubject) {
+    assertThat(superSubject, isPresent());
+    assertThat(superSubject, not(isInterface()));
+    return new TypeSafeMatcher<ClassSubject>() {
+      @Override
+      public boolean matchesSafely(ClassSubject subject) {
+        return subject.isPresent() && subject.isExtending(superSubject);
+      }
+
+      @Override
+      public void describeTo(Description description) {
+        description.appendText("extends ").appendText(superSubject.getOriginalName());
+      }
+    };
+  }
+
   public static Matcher<FieldSubject> isFieldOfType(DexType type) {
     return new TypeSafeMatcher<FieldSubject>() {
       @Override