Add reproduction of incorrectly inserting forwarding method

Bug: 171550305
Bug: 182148338
Change-Id: I4bcf208dc38d1ab8574ece7f1bbc7a6ba85b09ea
diff --git a/src/test/java/com/android/tools/r8/desugar/DefaultLambdaMethodWithPrivateSuperClassTest.java b/src/test/java/com/android/tools/r8/desugar/DefaultLambdaMethodWithPrivateSuperClassTest.java
new file mode 100644
index 0000000..cbd1fb8
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/DefaultLambdaMethodWithPrivateSuperClassTest.java
@@ -0,0 +1,80 @@
+// 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.desugar;
+
+import static org.junit.Assume.assumeTrue;
+
+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 org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * Regression test code for ensuring that desugared code behaves at least kind of like
+ * https://bugs.openjdk.java.net/browse/JDK-8021581
+ */
+@RunWith(Parameterized.class)
+public class DefaultLambdaMethodWithPrivateSuperClassTest extends TestBase {
+
+  private final TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build();
+  }
+
+  public DefaultLambdaMethodWithPrivateSuperClassTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testJvm() throws Exception {
+    assumeTrue(parameters.isCfRuntime());
+    testForJvm()
+        .addInnerClasses(getClass())
+        .run(parameters.getRuntime(), Main.class)
+        .assertFailureWithErrorThatThrows(IllegalAccessError.class);
+  }
+
+  @Test
+  public void testDesugar() throws Exception {
+    testForD8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .setMinApi(parameters.getApiLevel())
+        .run(parameters.getRuntime(), Main.class)
+        // TODO(b/171550305): Should be illegal access for DEX.
+        .applyIf(
+            parameters.isDexRuntime() || parameters.getApiLevel().isEqualTo(AndroidApiLevel.B),
+            r -> r.assertSuccessWithOutputLines("interface"),
+            r -> r.assertFailureWithErrorThatThrows(IllegalAccessError.class));
+  }
+
+  interface Named {
+
+    default String name() {
+      return "interface";
+    }
+
+    class PrivateNameBase {
+      private String name() {
+        throw new AssertionError("shouldn't get here");
+      }
+    }
+
+    class PrivateName extends PrivateNameBase implements Named {}
+  }
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      String unexpected = new Named.PrivateName().name();
+      System.out.println(unexpected);
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/DefaultLambdaMethodWithPublicSuperClassTest.java b/src/test/java/com/android/tools/r8/desugar/DefaultLambdaMethodWithPublicSuperClassTest.java
new file mode 100644
index 0000000..11bc898
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/DefaultLambdaMethodWithPublicSuperClassTest.java
@@ -0,0 +1,68 @@
+// 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.desugar;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+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 DefaultLambdaMethodWithPublicSuperClassTest extends TestBase {
+
+  private final TestParameters parameters;
+  private final String EXPECTED = "PublicNameBase::name";
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withCfRuntimes().withAllApiLevelsAlsoForCf().build();
+  }
+
+  public DefaultLambdaMethodWithPublicSuperClassTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testJvm() throws Exception {
+    testForJvm()
+        .addInnerClasses(getClass())
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines(EXPECTED);
+  }
+
+  @Test
+  public void testDesugar() throws Exception {
+    testForD8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .setMinApi(parameters.getApiLevel())
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines(EXPECTED);
+  }
+
+  interface Named {
+
+    default String name() {
+      return "interface";
+    }
+
+    class PublicNameBase {
+      public String name() {
+        return "PublicNameBase::name";
+      }
+    }
+
+    class PublicName extends PublicNameBase implements Named {}
+  }
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      System.out.println(new Named.PublicName().name());
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/graph/invokevirtual/InvokeVirtualPrivateBaseWithDefaultDirectInvokeTest.java b/src/test/java/com/android/tools/r8/graph/invokevirtual/InvokeVirtualPrivateBaseWithDefaultDirectInvokeTest.java
new file mode 100644
index 0000000..86204ee
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/graph/invokevirtual/InvokeVirtualPrivateBaseWithDefaultDirectInvokeTest.java
@@ -0,0 +1,107 @@
+// 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.graph.invokevirtual;
+
+import static org.junit.Assume.assumeTrue;
+
+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.TestRuntime.CfVm;
+import com.android.tools.r8.utils.AndroidApiLevel;
+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 InvokeVirtualPrivateBaseWithDefaultDirectInvokeTest extends TestBase {
+
+  private final TestParameters parameters;
+  private final String INVALID_EXPECTED = "I::foo";
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build();
+  }
+
+  public InvokeVirtualPrivateBaseWithDefaultDirectInvokeTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testJvm() throws Exception {
+    assumeTrue(parameters.isCfRuntime());
+    testForJvm()
+        .addInnerClasses(getClass())
+        .run(parameters.getRuntime(), Main.class)
+        .applyIf(
+            parameters.isCfRuntime(CfVm.JDK11),
+            // TODO(B/182148338): Figure out why JVM 11 has changed resolution for interface
+            //  lookups.
+            r -> r.assertSuccessWithOutputLines(INVALID_EXPECTED),
+            r -> r.assertFailureWithErrorThatThrows(IllegalAccessError.class));
+  }
+
+  @Test
+  public void testD8() throws Exception {
+    testForD8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .setMinApi(parameters.getApiLevel())
+        .run(parameters.getRuntime(), Main.class)
+        // TODO(B/182148338): Figure out why JVM 11 has changed resolution for interface lookups.
+        // TODO(b/171550305): Should be illegal access for DEX.
+        .applyIf(
+            parameters.isDexRuntime()
+                || parameters.getApiLevel().isEqualTo(AndroidApiLevel.B)
+                || parameters.isCfRuntime(CfVm.JDK11),
+            r -> r.assertSuccessWithOutputLines(INVALID_EXPECTED),
+            r -> r.assertFailureWithErrorThatThrows(IllegalAccessError.class));
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .addKeepMainRule(Main.class)
+        .addKeepClassAndMembersRules(I.class)
+        .enableNoVerticalClassMergingAnnotations()
+        .setMinApi(parameters.getApiLevel())
+        .compile()
+        .run(parameters.getRuntime(), Main.class)
+        // TODO(b/171550305): Should be illegal access.
+        .assertSuccessWithOutputLines(INVALID_EXPECTED);
+  }
+
+  @NoVerticalClassMerging
+  public interface I {
+
+    default void foo() {
+      System.out.println("I::foo");
+    }
+  }
+
+  @NoVerticalClassMerging
+  public static class Base {
+
+    private void foo() {
+      System.out.println("Base::foo");
+    }
+  }
+
+  public static class Sub extends Base implements I {}
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      runFoo(new Sub());
+    }
+
+    private static void runFoo(I i) {
+      i.foo();
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/graph/invokevirtual/InvokeVirtualPrivateBaseWithDefaultTest.java b/src/test/java/com/android/tools/r8/graph/invokevirtual/InvokeVirtualPrivateBaseWithDefaultTest.java
new file mode 100644
index 0000000..381eb18
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/graph/invokevirtual/InvokeVirtualPrivateBaseWithDefaultTest.java
@@ -0,0 +1,97 @@
+// 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.graph.invokevirtual;
+
+import static org.junit.Assume.assumeTrue;
+
+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.AndroidApiLevel;
+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 InvokeVirtualPrivateBaseWithDefaultTest extends TestBase {
+
+  private final TestParameters parameters;
+  private final String INVALID_EXPECTED = "I::foo";
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build();
+  }
+
+  public InvokeVirtualPrivateBaseWithDefaultTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testJvm() throws Exception {
+    assumeTrue(parameters.isCfRuntime());
+    testForJvm()
+        .addInnerClasses(getClass())
+        .run(parameters.getRuntime(), Main.class)
+        .assertFailureWithErrorThatThrows(IllegalAccessError.class);
+  }
+
+  @Test
+  public void testD8() throws Exception {
+    testForD8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .setMinApi(parameters.getApiLevel())
+        .run(parameters.getRuntime(), Main.class)
+        // TODO(b/171550305): Should be illegal access for DEX.
+        .applyIf(
+            parameters.isDexRuntime() || parameters.getApiLevel().isEqualTo(AndroidApiLevel.B),
+            r -> r.assertSuccessWithOutputLines(INVALID_EXPECTED),
+            r -> r.assertFailureWithErrorThatThrows(IllegalAccessError.class));
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .addKeepMainRule(Main.class)
+        .addKeepClassAndMembersRules(I.class)
+        .enableNoVerticalClassMergingAnnotations()
+        .setMinApi(parameters.getApiLevel())
+        .compile()
+        .run(parameters.getRuntime(), Main.class)
+        // TODO(b/171550305): Should be illegal access.
+        .applyIf(
+            parameters.isDexRuntime(),
+            r -> r.assertSuccessWithOutputLines(INVALID_EXPECTED),
+            r -> r.assertFailureWithErrorThatThrows(IllegalAccessError.class));
+  }
+
+  @NoVerticalClassMerging
+  public interface I {
+
+    default void foo() {
+      System.out.println("I::foo");
+    }
+  }
+
+  @NoVerticalClassMerging
+  public static class Base {
+
+    private void foo() {
+      System.out.println("Base::foo");
+    }
+  }
+
+  public static class Sub extends Base implements I {}
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      new Sub().foo();
+    }
+  }
+}