Skip to content

Commit

Permalink
Don't emit duplicate record component getters
Browse files Browse the repository at this point in the history
Check if record component getters have been explicitly declared, and don't emit the synthetic methods if they have.

bazelbuild/bazel#22105

PiperOrigin-RevId: 627802539
  • Loading branch information
cushon authored and Javac Team committed Apr 24, 2024
1 parent 5637a07 commit 4822097
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
12 changes: 11 additions & 1 deletion java/com/google/turbine/binder/TypeBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ private ImmutableList<MethodInfo> recordMethods(
boolean hasEquals = false;
boolean hasHashCode = false;
boolean hasPrimaryConstructor = false;
Set<String> componentNamesToDeclare = new HashSet<>();
for (RecordComponentInfo c : components) {
componentNamesToDeclare.add(c.name());
}
for (MethodInfo m : boundMethods) {
if (m.name().equals("<init>")) {
if (isPrimaryConstructor(m, components)) {
Expand All @@ -321,7 +325,10 @@ private ImmutableList<MethodInfo> recordMethods(
case "hashCode":
hasHashCode = m.parameters().isEmpty();
break;
default: // fall out
default:
if (m.parameters().isEmpty()) {
componentNamesToDeclare.remove(m.name());
}
}
boundNonConstructors.add(m);
}
Expand Down Expand Up @@ -383,6 +390,9 @@ private ImmutableList<MethodInfo> recordMethods(
null));
}
for (RecordComponentInfo c : components) {
if (!componentNamesToDeclare.contains(c.name())) {
continue;
}
MethodSymbol componentMethod = syntheticMethods.create(owner, c.name());
methods.add(
new MethodInfo(
Expand Down
25 changes: 14 additions & 11 deletions javatests/com/google/turbine/lower/LowerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;
import static com.google.turbine.testing.TestResources.getResource;
import static java.util.Map.entry;
import static java.util.stream.Collectors.toList;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -53,17 +54,18 @@
public class LowerIntegrationTest {

private static final ImmutableMap<String, Integer> SOURCE_VERSION =
ImmutableMap.of(
"record.test", 16, //
"record2.test", 16,
"record_tostring.test", 16,
"record_ctor.test", 16,
"sealed.test", 17,
"sealed_nested.test", 17,
"textblock.test", 15,
"textblock2.test", 15,
"B306423115.test", 15,
"string_template.test", 21);
ImmutableMap.ofEntries(
entry("record.test", 16),
entry("record2.test", 16),
entry("record_tostring.test", 16),
entry("record_ctor.test", 16),
entry("record_getter_override.test", 16),
entry("sealed.test", 17),
entry("sealed_nested.test", 17),
entry("textblock.test", 15),
entry("textblock2.test", 15),
entry("B306423115.test", 15),
entry("string_template.test", 21));

private static final ImmutableSet<String> SOURCE_VERSION_PREVIEW =
ImmutableSet.of("string_template.test");
Expand Down Expand Up @@ -288,6 +290,7 @@ public static Iterable<Object[]> parameters() {
"record.test",
"record2.test",
"record_ctor.test",
"record_getter_override.test",
"record_tostring.test",
"rek.test",
"samepkg.test",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== Foo.java ===
import java.util.ArrayList;
import java.util.List;

public record Foo(int bar, List<String> baz) {
/** This should override the default baz() getter. */
public List<String> baz() {
return baz == null ? new ArrayList<>() : baz;
}
}

0 comments on commit 4822097

Please sign in to comment.