Skip to content

Commit 36f43f7

Browse files
author
Vicente Romero
committed
8370634: [lworld] super should not be allowed in compact constructors
Reviewed-by: liach
1 parent a291a5d commit 36f43f7

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,8 @@ public void visitMethodDef(JCMethodDecl tree) {
11081108
);
11091109
}
11101110

1111-
if (!allowValueClasses && TreeInfo.hasAnyConstructorCall(tree)) {
1111+
if ((!allowValueClasses || TreeInfo.isCompactConstructor(tree)) &&
1112+
TreeInfo.hasAnyConstructorCall(tree)) {
11121113
log.error(tree, Errors.InvalidCanonicalConstructorInRecord(
11131114
Fragments.Canonical, env.enclClass.sym.name,
11141115
Fragments.CanonicalMustNotContainExplicitConstructorInvocation));

test/langtools/tools/javac/valhalla/value-objects/ValueObjectCompilationTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,40 @@ static class Foo {
11501150
""",
11511151
"new,dup,invokespecial,astore_1,aload_1,invokevirtual,pop,aload_0,aload_1,putfield,aload_0,invokespecial,return"
11521152
);
1153+
1154+
assertFail("compiler.err.invalid.canonical.constructor.in.record",
1155+
"""
1156+
record R(int x) {
1157+
public R {
1158+
super();
1159+
}
1160+
}
1161+
"""
1162+
);
1163+
1164+
assertFail("compiler.err.invalid.canonical.constructor.in.record",
1165+
"""
1166+
record R(int x) {
1167+
public R {
1168+
this();
1169+
}
1170+
public R() {
1171+
this(1);
1172+
}
1173+
}
1174+
"""
1175+
);
1176+
1177+
assertOK(
1178+
"""
1179+
record R(int x) {
1180+
public R(int x) {
1181+
this.x = x;
1182+
super();
1183+
}
1184+
}
1185+
"""
1186+
);
11531187
}
11541188

11551189
void checkMnemonicsFor(String source, String expectedMnemonics) throws Exception {

0 commit comments

Comments
 (0)