Add test for strict assertion for SyntheticMethodReference
I am unsure what this means for the synthetic since the it returns null
and probably should return this.
Bug: 180092122
Change-Id: Iad96744bae446a150de06971b08295ee2a96fe87
diff --git a/src/main/java/com/android/tools/r8/synthesis/SyntheticMethodReference.java b/src/main/java/com/android/tools/r8/synthesis/SyntheticMethodReference.java
index 091d517..8edbe4d 100644
--- a/src/main/java/com/android/tools/r8/synthesis/SyntheticMethodReference.java
+++ b/src/main/java/com/android/tools/r8/synthesis/SyntheticMethodReference.java
@@ -55,7 +55,7 @@
if (method != rewritten && !lens.isSimpleRenaming(method.holder, rewritten.holder)) {
// If the referenced item is rewritten, it should be moved to another holder as the
// synthetic holder is no longer part of the synthetic collection.
- assert method.holder != rewritten.holder;
+ assert method.holder != rewritten.holder : "The synthetic method reference should have moved";
assert SyntheticNaming.verifyNotInternalSynthetic(rewritten.holder);
return null;
}
diff --git a/src/test/java/com/android/tools/r8/TestShrinkerBuilder.java b/src/test/java/com/android/tools/r8/TestShrinkerBuilder.java
index 4936451..c0cd551 100644
--- a/src/test/java/com/android/tools/r8/TestShrinkerBuilder.java
+++ b/src/test/java/com/android/tools/r8/TestShrinkerBuilder.java
@@ -244,7 +244,11 @@
}
public T addKeepPackageNamesRule(Package pkg) {
- return addKeepRules("-keeppackagenames " + pkg.getName());
+ return addKeepPackageNamesRule(pkg.getName());
+ }
+
+ public T addKeepPackageNamesRule(String packageName) {
+ return addKeepRules("-keeppackagenames " + packageName);
}
public T addKeepMainRule(Class<?> mainClass) {
diff --git a/src/test/java/com/android/tools/r8/repackage/RepackageParameterSyntheticOutlineTest.java b/src/test/java/com/android/tools/r8/repackage/RepackageParameterSyntheticOutlineTest.java
new file mode 100644
index 0000000..0636a50
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/repackage/RepackageParameterSyntheticOutlineTest.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.repackage;
+
+import static com.android.tools.r8.DiagnosticsMatcher.diagnosticMessage;
+import static com.android.tools.r8.shaking.ProguardConfigurationParser.REPACKAGE_CLASSES;
+import static org.hamcrest.CoreMatchers.containsString;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.utils.DescriptorUtils;
+import com.google.common.collect.ImmutableList;
+import java.util.List;
+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 RepackageParameterSyntheticOutlineTest extends RepackageTestBase {
+
+ private final String NEW_DESCRIPTOR = "Lfoo/ClassWithCodeToBeOutlined;";
+ private final String[] EXPECTED =
+ new String[] {"Param::testParam", "Return::print", "Param::testParam", "Return::print"};
+
+ @Parameters(name = "{1}, kind: {0}")
+ public static List<Object[]> data() {
+ return buildParameters(
+ ImmutableList.of(REPACKAGE_CLASSES), // Repackage will use foo as the package name.
+ getTestParameters().withDexRuntimes().withAllApiLevels().build());
+ }
+
+ public RepackageParameterSyntheticOutlineTest(
+ String flattenPackageHierarchyOrRepackageClasses, TestParameters parameters) {
+ super(flattenPackageHierarchyOrRepackageClasses, parameters);
+ }
+
+ @Test
+ public void testRuntime() throws Exception {
+ testForRuntime(parameters)
+ .addProgramClasses(Param.class, Return.class)
+ .addProgramClassFileData(
+ rewrittenPackageForClassWithCodeToBeOutlined(),
+ rewrittenMainWithMethodReferencesToCodeToBeOutlined())
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines(EXPECTED);
+ }
+
+ @Test(expected = CompilationFailedException.class)
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .addProgramClasses(Param.class, Return.class)
+ .addProgramClassFileData(
+ rewrittenPackageForClassWithCodeToBeOutlined(),
+ rewrittenMainWithMethodReferencesToCodeToBeOutlined())
+ .addKeepMainRule(Main.class)
+ .addKeepClassRulesWithAllowObfuscation(Param.class, Return.class)
+ .setMinApi(parameters.getApiLevel())
+ .enableInliningAnnotations()
+ .addOptionsModification(
+ options -> {
+ options.outline.minSize = 2;
+ options.outline.threshold = 2;
+ })
+ .apply(this::configureRepackaging)
+ .addKeepPackageNamesRule("bar**")
+ .compileWithExpectedDiagnostics(
+ diagnostics -> {
+ // TODO(b/180092122): This should not fail.
+ diagnostics.assertErrorsMatch(
+ diagnosticMessage(
+ containsString("The synthetic method reference should have moved")));
+ });
+ }
+
+ private byte[] rewrittenPackageForClassWithCodeToBeOutlined() throws Exception {
+ return transformer(ClassWithCodeToBeOutlined.class)
+ .setClassDescriptor(NEW_DESCRIPTOR)
+ .transform();
+ }
+
+ private byte[] rewrittenMainWithMethodReferencesToCodeToBeOutlined() throws Exception {
+ return transformer(Main.class)
+ .replaceClassDescriptorInMethodInstructions(
+ DescriptorUtils.javaTypeToDescriptor(ClassWithCodeToBeOutlined.class.getTypeName()),
+ NEW_DESCRIPTOR)
+ .transform();
+ }
+
+ // Will be renamed by repackaging
+ public static class Param {
+
+ @NeverInline
+ public void testParam() {
+ System.out.println("Param::testParam");
+ }
+
+ @NeverInline
+ public Return getReturn() {
+ return new Return();
+ }
+ }
+
+ // Will be renamed by repackaging
+ public static class Return {
+
+ public void print() {
+ System.out.println("Return::print");
+ }
+ }
+
+ // Renamed to baz.ClassWithCodeToBeOutlined such that we can keep this package name.
+ public static class ClassWithCodeToBeOutlined {
+
+ @NeverInline
+ public static Return foo(Param param) {
+ param.testParam();
+ return param.getReturn();
+ }
+
+ @NeverInline
+ public static Return bar(Param param) {
+ param.testParam();
+ return param.getReturn();
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ Param param = new Param();
+ ClassWithCodeToBeOutlined.foo(param).print();
+ ClassWithCodeToBeOutlined.bar(param).print();
+ }
+ }
+}