Skip to content

Commit 67d181d

Browse files
author
Vicente Romero
committed
8351217: [lworld] instance field initializers should be executed immediately upon entry to a non-this-calling constructor
1 parent 7a1ff29 commit 67d181d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ void normalizeMethod(JCMethodDecl md, List<JCStatement> initCode, List<JCStateme
564564
// Find the super() invocation and append the given initializer code.
565565
if (md.sym.owner.isValueClass() || md.sym.owner.hasStrict()) {
566566
rewriteInitializersIfNeeded(md, initCode);
567-
TreeInfo.mapSuperCalls(md.body, supercall -> make.Block(0, initCode.append(supercall).appendList(initBlocks)));
567+
md.body.stats = initCode.appendList(md.body.stats);
568+
TreeInfo.mapSuperCalls(md.body, supercall -> make.Block(0, initBlocks.prepend(supercall)));
568569
} else {
569570
TreeInfo.mapSuperCalls(md.body, supercall -> make.Block(0, initCode.prepend(supercall)));
570571
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
5555
import com.sun.tools.classfile.ConstantPool.CONSTANT_Fieldref_info;
5656
import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info;
57+
import com.sun.tools.classfile.ConstantPool.CONSTANT_NameAndType_info;
58+
import com.sun.tools.classfile.ConstantPool.CPRefInfo;
5759
import com.sun.tools.classfile.Field;
5860
import com.sun.tools.classfile.Instruction;
5961
import com.sun.tools.classfile.Method;
@@ -1119,6 +1121,35 @@ public StrictNR() {
11191121
} finally {
11201122
setCompileOptions(previousOptions);
11211123
}
1124+
1125+
source =
1126+
"""
1127+
value class V {
1128+
int i = 1;
1129+
int y;
1130+
V() {
1131+
y = 2;
1132+
}
1133+
}
1134+
""";
1135+
dir = assertOK(true, source);
1136+
File fileEntry = dir.listFiles()[0];
1137+
ClassFile classFile = ClassFile.read(fileEntry);
1138+
expectedCodeSequence = "putfield i,putfield y,";
1139+
for (Method method : classFile.methods) {
1140+
if (method.getName(classFile.constant_pool).equals("<init>")) {
1141+
Code_attribute code = (Code_attribute)method.attributes.get("Code");
1142+
String foundCodeSequence = "";
1143+
for (Instruction inst: code.getInstructions()) {
1144+
if (inst.getMnemonic().equals("putfield")) {
1145+
CPRefInfo refInfo = (CPRefInfo)classFile.constant_pool.get(inst.getShort(1));
1146+
CONSTANT_NameAndType_info nameAndType = refInfo.getNameAndTypeInfo();
1147+
foundCodeSequence += inst.getMnemonic() + " " + nameAndType.getName() + ",";
1148+
}
1149+
}
1150+
Assert.check(foundCodeSequence.equals(expectedCodeSequence), foundCodeSequence);
1151+
}
1152+
}
11221153
}
11231154

11241155
@Test

0 commit comments

Comments
 (0)