Add reproduction of b/159441805
Static methods are not retargetted corretly for library desugaring.
Bug: 159441805
Change-Id: Idc84805c2bf34d48662cdc733380340aaff29270
diff --git a/src/test/java/com/android/tools/r8/MarkersTest.java b/src/test/java/com/android/tools/r8/MarkersTest.java
index 4fa3dd6..cc7b81e 100644
--- a/src/test/java/com/android/tools/r8/MarkersTest.java
+++ b/src/test/java/com/android/tools/r8/MarkersTest.java
@@ -69,7 +69,7 @@
allOf(
markerTool(Tool.L8),
markerCompilationMode(compilationMode),
- markerDesugaredLibraryIdentifier("com.tools.android:desugar_jdk_libs:1.0.9"),
+ markerDesugaredLibraryIdentifier("com.tools.android:desugar_jdk_libs:1.0.10"),
markerHasChecksums(false));
Matcher<Marker> d8Matcher =
allOf(
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetOverrideTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetOverrideTest.java
index 04a7258..32bbe65 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetOverrideTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetOverrideTest.java
@@ -122,6 +122,26 @@
System.out.println("43");
System.out.println(myAtomicInteger.updateAndGet(x -> x + 100));
System.out.println("145");
+
+ try {
+ MyDateNoOverride.from(myCal.toInstant());
+ System.out.println("b/159441805 fixed");
+ } catch (NoSuchMethodError e) {
+ // TODO(b/159441805): Should not throw.
+ }
+
+ try {
+ MyDateOverride.from(myCal.toInstant());
+ System.out.println("b/159441805 fixed");
+ } catch (NoSuchMethodError e) {
+ // TODO(b/159441805): Should not throw.
+ }
+
+ System.out.println(MyDateDoubleOverride.from(myCal.toInstant()).toInstant());
+ System.out.println("1970-01-02T10:17:36.788Z");
+
+ System.out.println(MyDateTrippleOverride.from(myCal.toInstant()).toInstant());
+ System.out.println("1970-01-02T10:17:36.788Z");
}
public static void polyTypes() {
@@ -212,6 +232,22 @@
public Instant toInstant() {
return super.toInstant().plusSeconds(3);
}
+
+ public static Date from(Instant instant) {
+ return new Date(123456788);
+ }
+ }
+
+ static class MyDateTrippleOverride extends MyDateDoubleOverride {
+
+ public MyDateTrippleOverride(long date) {
+ super(date);
+ }
+
+ @Override
+ public Instant toInstant() {
+ return super.toInstant().plusSeconds(6);
+ }
}
static class MyDateNoOverride extends Date {
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/BasicTimeConversionTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/BasicTimeConversionTest.java
deleted file mode 100644
index bb728fb..0000000
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/BasicTimeConversionTest.java
+++ /dev/null
@@ -1,158 +0,0 @@
-// Copyright (c) 2019, 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.desugaredlibrary.conversiontests;
-
-import static junit.framework.TestCase.assertTrue;
-
-import com.android.tools.r8.TestParameters;
-import com.android.tools.r8.desugar.desugaredlibrary.DesugaredLibraryTestBase;
-import com.android.tools.r8.utils.AndroidApiLevel;
-import com.android.tools.r8.utils.BooleanUtils;
-import com.android.tools.r8.utils.StringUtils;
-import com.android.tools.r8.utils.codeinspector.CodeInspector;
-import com.android.tools.r8.utils.codeinspector.MethodSubject;
-import java.time.ZoneId;
-import java.util.List;
-import java.util.TimeZone;
-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 BasicTimeConversionTest extends DesugaredLibraryTestBase {
-
- private final TestParameters parameters;
- private final boolean shrinkDesugaredLibrary;
- private static final String GMT = StringUtils.lines("GMT");
-
- @Parameters(name = "{0}, shrinkDesugaredLibrary: {1}")
- public static List<Object[]> data() {
- return buildParameters(
- getConversionParametersUpToExcluding(AndroidApiLevel.O), BooleanUtils.values());
- }
-
- public BasicTimeConversionTest(TestParameters parameters, boolean shrinkDesugaredLibrary) {
- this.shrinkDesugaredLibrary = shrinkDesugaredLibrary;
- this.parameters = parameters;
- }
-
- @Test
- public void testRewrittenAPICallsD8() throws Exception {
- KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
- testForD8()
- .setMinApi(parameters.getApiLevel())
- .addInnerClasses(BasicTimeConversionTest.class)
- .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
- .compile()
- .addDesugaredCoreLibraryRunClassPath(
- this::buildDesugaredLibrary,
- parameters.getApiLevel(),
- keepRuleConsumer.get(),
- shrinkDesugaredLibrary)
- .inspect(this::checkAPIRewritten)
- .run(parameters.getRuntime(), Executor.class)
- .assertSuccessWithOutput(GMT);
- if (shrinkDesugaredLibrary) {
- checkKeepRules(keepRuleConsumer.get());
- }
- }
-
- private void checkKeepRules(String keepRules) {
- assertTrue(keepRules.contains("TimeConversion"));
- }
-
- @Test
- public void testRewrittenAPICallsR8() throws Exception {
- KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
- testForR8(parameters.getBackend())
- .setMinApi(parameters.getApiLevel())
- .addKeepMainRule(Executor.class)
- .addInnerClasses(BasicTimeConversionTest.class)
- .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
- .compile()
- .addDesugaredCoreLibraryRunClassPath(
- this::buildDesugaredLibrary,
- parameters.getApiLevel(),
- keepRuleConsumer.get(),
- shrinkDesugaredLibrary)
- .run(parameters.getRuntime(), Executor.class)
- .assertSuccessWithOutput(GMT);
- if (shrinkDesugaredLibrary) {
- checkKeepRules(keepRuleConsumer.get());
- }
- }
-
- private void checkAPIRewritten(CodeInspector inspector) {
- MethodSubject mainMethod = inspector.clazz(Executor.class).uniqueMethodWithName("main");
- // Check the API calls are not using j$ types.
- assertTrue(
- mainMethod
- .streamInstructions()
- .anyMatch(
- instr ->
- instr.isInvokeStatic()
- && instr.getMethod().name.toString().equals("getTimeZone")
- && instr
- .getMethod()
- .proto
- .parameters
- .values[0]
- .toString()
- .equals("java.time.ZoneId")));
- assertTrue(
- mainMethod
- .streamInstructions()
- .anyMatch(
- instr ->
- instr.isInvokeVirtual()
- && instr.getMethod().name.toString().equals("toZoneId")
- && instr
- .getMethod()
- .proto
- .returnType
- .toString()
- .equals("java.time.ZoneId")));
- // Check the conversion instructions are present.
- assertTrue(
- mainMethod
- .streamInstructions()
- .anyMatch(
- instr ->
- instr.isInvokeStatic()
- && instr.getMethod().name.toString().equals("convert")
- && instr
- .getMethod()
- .proto
- .parameters
- .values[0]
- .toString()
- .equals("j$.time.ZoneId")));
- assertTrue(
- mainMethod
- .streamInstructions()
- .anyMatch(
- instr ->
- instr.isInvokeStatic()
- && instr.getMethod().name.toString().equals("convert")
- && instr
- .getMethod()
- .proto
- .parameters
- .values[0]
- .toString()
- .equals("java.time.ZoneId")));
- }
-
- static class Executor {
- public static void main(String[] args) {
- ZoneId zoneId = ZoneId.systemDefault();
- // Following is a call where java.time.ZoneId is a parameter type (getTimeZone()).
- TimeZone timeZone = TimeZone.getTimeZone(zoneId);
- // Following is a call where java.time.ZoneId is a return type (toZoneId()).
- System.out.println(timeZone.toZoneId().getId());
- }
- }
-}