Add regression test for inlining code with invalid calls

Bug: 199142666
Change-Id: I7a11d158b182815559b6763b2804937adcbace01
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/inliner/Regress199142666.java b/src/test/java/com/android/tools/r8/ir/optimize/inliner/Regress199142666.java
new file mode 100644
index 0000000..27a8639
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/inliner/Regress199142666.java
@@ -0,0 +1,139 @@
+// 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.ir.optimize.inliner;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.R8TestRunResult;
+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.codeinspector.CodeInspector;
+import com.android.tools.r8.utils.codeinspector.InstructionSubject;
+import java.io.IOException;
+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 Regress199142666 extends TestBase {
+
+  private final TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection params() {
+    return getTestParameters().withDexRuntimes().withAllApiLevels().build();
+  }
+
+  public Regress199142666(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  private byte[] getVirtualAAsStaticA() throws IOException {
+    return transformer(VirtualA.class).setClassDescriptor(descriptor(StaticA.class)).transform();
+  }
+
+  private byte[] getStaticAAsVirtualA() throws IOException {
+    return transformer(StaticA.class).setClassDescriptor(descriptor(VirtualA.class)).transform();
+  }
+
+  @Test
+  public void testInliningWhenInvalidTarget() throws Exception {
+    R8TestRunResult run =
+        testForR8(parameters.getBackend())
+            .addProgramClasses(B.class)
+            .addProgramClassFileData(getVirtualAAsStaticA())
+            .addKeepMainRule(B.class)
+            .addKeepMethodRules(StaticA.class, "void foo()")
+            .setMinApi(parameters.getApiLevel())
+            .run(parameters.getRuntime(), B.class);
+    if (parameters.getRuntime().asDex().getVm().getVersion().isDalvik()) {
+      // TODO(b/199142666): We should not inline to provoke this error.
+      run.assertFailureWithErrorThatMatches(
+          containsString("invoke type does not match method type of"));
+    } else {
+      run.assertSuccessWithOutputLines("foochanged")
+          .inspect(inspector -> ensureThisNumberOfCalls(inspector, B.class, 2));
+    }
+  }
+
+  @Test
+  public void testInliningWhenInvalidCaller() throws Exception {
+    // On pre 7 we do interface desugaring, where we insert ICCE directly if that would happen
+    // at runtime, even when there are no interfaces involved. Tracked in b/199135051
+    int assumedFooCalls = parameters.getApiLevel().isLessThan(AndroidApiLevel.N) ? 1 : 2;
+    R8TestRunResult run =
+        testForR8(parameters.getBackend())
+            .addProgramClasses(C.class)
+            .addProgramClassFileData(getStaticAAsVirtualA())
+            .addKeepMainRule(C.class)
+            .addKeepMethodRules(VirtualA.class, "static void foo()")
+            .setMinApi(parameters.getApiLevel())
+            .run(parameters.getRuntime(), C.class)
+            .assertSuccessWithOutputLines("foo")
+            .inspect(inspector -> ensureThisNumberOfCalls(inspector, C.class, assumedFooCalls));
+  }
+
+  private void ensureThisNumberOfCalls(CodeInspector inspector, Class clazz, int fooCalls) {
+    long count =
+        inspector
+            .clazz(clazz)
+            .mainMethod()
+            .streamInstructions()
+            .filter(InstructionSubject::isInvoke)
+            .filter(invoke -> invoke.getMethod().name.toString().equals("foo"))
+            .count();
+    // TODO(b/199142666): We should not inline, so count should be 1.
+    assertEquals(count, fooCalls);
+  }
+
+  static class StaticA {
+    public static void callFoo() {
+      StaticA.foo();
+    }
+
+    public static void foo() {
+      System.out.println("foo");
+    }
+  }
+
+  static class VirtualA {
+    public static void callFoo() {
+      new VirtualA().foo();
+    }
+
+    public void foo() {
+      System.out.println("foochanged");
+    }
+  }
+
+  static class B {
+    public static void main(String[] args) {
+      StaticA.callFoo();
+      try {
+        StaticA.foo();
+      } catch (IncompatibleClassChangeError e) {
+        // Expected
+        return;
+      }
+      throw new RuntimeException("Should have thrown ICCE");
+    }
+  }
+
+  static class C {
+    public static void main(String[] args) {
+      VirtualA.callFoo();
+      try {
+        new VirtualA().foo();
+      } catch (IncompatibleClassChangeError e) {
+        return;
+      }
+      throw new RuntimeException("Should have thrown ICCE");
+    }
+  }
+}