Skip to content

Commit

Permalink
Handle compact record constructors
Browse files Browse the repository at this point in the history
bazelbuild/bazel#14249

PiperOrigin-RevId: 408952084
  • Loading branch information
cushon authored and Javac Team committed Nov 10, 2021
1 parent 157c076 commit 972d62a
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 10 deletions.
34 changes: 28 additions & 6 deletions java/com/google/turbine/binder/TypeBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private SourceTypeBoundClass bind() {
ImmutableList.Builder<MethodInfo> methods =
ImmutableList.<MethodInfo>builder()
.addAll(syntheticMethods(syntheticMethods, components))
.addAll(bindMethods(scope, base.decl().members()));
.addAll(bindMethods(scope, base.decl().members(), components));
if (base.kind().equals(TurbineTyKind.RECORD)) {
methods.addAll(syntheticRecordMethods(syntheticMethods, components));
}
Expand Down Expand Up @@ -563,18 +563,22 @@ private ImmutableMap<TyVarSymbol, TyVarInfo> bindTyParams(
return result.build();
}

private List<MethodInfo> bindMethods(CompoundScope scope, ImmutableList<Tree> members) {
private List<MethodInfo> bindMethods(
CompoundScope scope,
ImmutableList<Tree> members,
ImmutableList<RecordComponentInfo> components) {
List<MethodInfo> methods = new ArrayList<>();
int idx = 0;
for (Tree member : members) {
if (member.kind() == Tree.Kind.METH_DECL) {
methods.add(bindMethod(idx++, scope, (Tree.MethDecl) member));
methods.add(bindMethod(idx++, scope, (MethDecl) member, components));
}
}
return methods;
}

private MethodInfo bindMethod(int idx, CompoundScope scope, Tree.MethDecl t) {
private MethodInfo bindMethod(
int idx, CompoundScope scope, MethDecl t, ImmutableList<RecordComponentInfo> components) {

MethodSymbol sym = new MethodSymbol(idx, owner, t.name().value());

Expand Down Expand Up @@ -604,8 +608,26 @@ private MethodInfo bindMethod(int idx, CompoundScope scope, Tree.MethDecl t) {
if (name.equals("<init>")) {
if (hasEnclosingInstance(base)) {
parameters.add(enclosingInstanceParameter(sym));
} else if (base.kind() == TurbineTyKind.ENUM && name.equals("<init>")) {
parameters.addAll(enumCtorParams(sym));
} else {
switch (base.kind()) {
case ENUM:
parameters.addAll(enumCtorParams(sym));
break;
case RECORD:
if (t.mods().contains(TurbineModifier.COMPACT_CTOR)) {
for (RecordComponentInfo component : components) {
parameters.add(
new ParamInfo(
new ParamSymbol(sym, component.name()),
component.type(),
component.annotations(),
component.access()));
}
}
break;
default:
break;
}
}
}
ParamInfo receiver = null;
Expand Down
3 changes: 3 additions & 0 deletions java/com/google/turbine/model/TurbineFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@ public final class TurbineFlag {
public static final int ACC_SEALED = 1 << 19;
public static final int ACC_NON_SEALED = 1 << 20;

/** Compact record constructor. */
public static final int ACC_COMPACT_CTOR = 1 << 21;

private TurbineFlag() {}
}
19 changes: 19 additions & 0 deletions java/com/google/turbine/parse/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,25 @@ private ImmutableList<Tree> member(
name = ident;
return ImmutableList.of(methodRest(pos, access, annos, typaram, null, name));
}
case LBRACE:
{
dropBlocks();
name = new Ident(position, CTOR_NAME);
String javadoc = lexer.javadoc();
access.add(TurbineModifier.COMPACT_CTOR);
return ImmutableList.<Tree>of(
new MethDecl(
pos,
access,
annos,
typaram,
/* ret= */ Optional.empty(),
name,
/* params= */ ImmutableList.of(),
/* exntys= */ ImmutableList.of(),
/* defaultValue= */ Optional.empty(),
javadoc));
}
case IDENT:
{
result =
Expand Down
1 change: 1 addition & 0 deletions java/com/google/turbine/tree/Pretty.java
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ private void printModifiers(ImmutableSet<TurbineModifier> mods) {
case ACC_ANNOTATION:
case ACC_SYNTHETIC:
case ACC_BRIDGE:
case COMPACT_CTOR:
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion java/com/google/turbine/tree/TurbineModifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public enum TurbineModifier {
DEFAULT(TurbineFlag.ACC_DEFAULT),
TRANSITIVE(TurbineFlag.ACC_TRANSITIVE),
SEALED(TurbineFlag.ACC_SEALED),
NON_SEALED(TurbineFlag.ACC_NON_SEALED);
NON_SEALED(TurbineFlag.ACC_NON_SEALED),
COMPACT_CTOR(TurbineFlag.ACC_COMPACT_CTOR);

private final int flag;

Expand Down
5 changes: 2 additions & 3 deletions javatests/com/google/turbine/lower/LowerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@
public class LowerIntegrationTest {

private static final ImmutableMap<String, Integer> SOURCE_VERSION =
ImmutableMap.of(
"record.test", 16,
"sealed.test", 17);
ImmutableMap.of("record.test", 16, "record2.test", 16, "sealed.test", 17);

@Parameters(name = "{index}: {0}")
public static Iterable<Object[]> parameters() {
Expand Down Expand Up @@ -265,6 +263,7 @@ public static Iterable<Object[]> parameters() {
"rawfbound.test",
"receiver_param.test",
"record.test",
"record2.test",
"rek.test",
"samepkg.test",
"sealed.test",
Expand Down
27 changes: 27 additions & 0 deletions javatests/com/google/turbine/lower/testdata/record2.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== Records.java ===

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.util.Objects;

class Records {
public record R1(String one) {
public R1 {
Objects.requireNonNull(one);
}
}

public record R2(String one) {
@Deprecated
public R2 {
Objects.requireNonNull(one);
}
}

public record R3<T>(T x) {
@Deprecated
public R3 {
Objects.requireNonNull(x);
}
}
}

0 comments on commit 972d62a

Please sign in to comment.