Fix hashCode generation for records
Bug: b/337134036
Change-Id: I11cce38fa09dd21118fc1b61f4f1cb55dd44a590
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/records/RecordDesugaring.java b/src/main/java/com/android/tools/r8/ir/desugar/records/RecordDesugaring.java
index 7aea6c8..71527c8 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/records/RecordDesugaring.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/records/RecordDesugaring.java
@@ -563,7 +563,7 @@
return factory.objectMembers.toString;
}
assert method == factory.recordMembers.hashCode;
- return factory.objectMembers.toString;
+ return factory.objectMembers.hashCode;
}
private static DexEncodedMethod synthesizeRecordInitMethod(AppView<?> appView) {
diff --git a/src/test/examplesJava17/records/RecordHashCodeTest.java b/src/test/examplesJava17/records/RecordHashCodeTest.java
new file mode 100644
index 0000000..59d4666
--- /dev/null
+++ b/src/test/examplesJava17/records/RecordHashCodeTest.java
@@ -0,0 +1,121 @@
+// Copyright (c) 2024, 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 records;
+
+import static org.junit.Assume.assumeTrue;
+
+import com.android.tools.r8.JdkClassFileProvider;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.R8FullTestBuilder;
+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 com.android.tools.r8.utils.StringUtils;
+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 RecordHashCodeTest extends TestBase {
+
+ private static final String EXPECTED_RESULT =
+ StringUtils.lines(
+ "true", "true", "true", "true", "false", "false", "true", "true", "false", "false");
+
+ @Parameter public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build();
+ }
+
+ private boolean isCfRuntimeWithNativeRecordSupport() {
+ return parameters.isCfRuntime()
+ && parameters.asCfRuntime().isNewerThanOrEqual(CfVm.JDK14)
+ && parameters.getApiLevel().equals(AndroidApiLevel.B);
+ }
+
+ @Test
+ public void testReference() throws Exception {
+ assumeTrue(isCfRuntimeWithNativeRecordSupport());
+ testForJvm(parameters)
+ .addInnerClassesAndStrippedOuter(getClass())
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutput(EXPECTED_RESULT);
+ }
+
+ @Test
+ public void testD8() throws Exception {
+ testForD8(parameters.getBackend())
+ .addInnerClassesAndStrippedOuter(getClass())
+ .setMinApi(parameters)
+ .run(parameters.getRuntime(), TestClass.class)
+ .applyIf(
+ isRecordsDesugaredForD8(parameters)
+ || runtimeWithRecordsSupport(parameters.getRuntime()),
+ r -> r.assertSuccessWithOutput(EXPECTED_RESULT),
+ r -> r.assertFailureWithErrorThatThrows(NoClassDefFoundError.class));
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ parameters.assumeR8TestParameters();
+ assumeTrue(parameters.isDexRuntime() || isCfRuntimeWithNativeRecordSupport());
+ R8FullTestBuilder builder =
+ testForR8(parameters.getBackend())
+ .addInnerClassesAndStrippedOuter(getClass())
+ .setMinApi(parameters)
+ .addInliningAnnotations()
+ .addKeepMainRule(TestClass.class);
+ if (parameters.isCfRuntime()) {
+ builder
+ .addLibraryProvider(JdkClassFileProvider.fromSystemJdk())
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutput(EXPECTED_RESULT);
+ return;
+ }
+ builder.run(parameters.getRuntime(), TestClass.class).assertSuccessWithOutput(EXPECTED_RESULT);
+ }
+
+ public static class TestClass {
+
+ record Person(String name, int age) {}
+
+ record Food(String name, int sweetness, int saltyness) {}
+
+ public static void main(String[] args) {
+ Person janeDoe = new Person("Jane Doe", 42);
+ System.out.println(equals(janeDoe.hashCode(), janeDoe.hashCode()));
+ System.out.println(equalsHash(janeDoe, janeDoe));
+ Person otherJaneDoe = new Person("Jane Doe", 42);
+ System.out.println(equals(janeDoe.hashCode(), otherJaneDoe.hashCode()));
+ System.out.println(equalsHash(janeDoe, otherJaneDoe));
+ Person johnDoe = new Person("John Doe", 45);
+ System.out.println(equals(janeDoe.hashCode(), johnDoe.hashCode()));
+ System.out.println(equalsHash(janeDoe, johnDoe));
+
+ Food bread = new Food("bread", 1, 100);
+ System.out.println(equals(bread.hashCode(), bread.hashCode()));
+ System.out.println(equalsHash(bread, bread));
+ Food biscuit = new Food("biscuit", 50, 20);
+ System.out.println(equals(bread.hashCode(), biscuit.hashCode()));
+ System.out.println(equalsHash(bread, biscuit));
+ }
+
+ @NeverInline
+ public static boolean equals(int i1, int i2) {
+ return System.currentTimeMillis() > 0 ? i1 == i2 : false;
+ }
+
+ @NeverInline
+ public static boolean equalsHash(Record r1, Record r2) {
+ return System.currentTimeMillis() > 0 ? equals(r1.hashCode(), r2.hashCode()) : false;
+ }
+ }
+}