8184444: The compiler error "variable not initialized in the default constructor" is not apt in case of static final variables

Reviewed-by: vromero
This commit is contained in:
Archie L. Cobbs 2023-03-23 16:06:47 +00:00 committed by Vicente Romero
parent 4b8f7db6be
commit 6fa25cc134
5 changed files with 23 additions and 4 deletions

View File

@ -2232,6 +2232,15 @@ public class Flow {
}
}
// verify all static final fields got initailized
for (int i = firstadr; i < nextadr; i++) {
JCVariableDecl vardecl = vardecls[i];
VarSymbol var = vardecl.sym;
if (var.owner == classDef.sym && var.isStatic()) {
checkInit(TreeInfo.diagnosticPositionFor(var, vardecl), var);
}
}
// define all the instance fields
for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
if (l.head.hasTag(VARDEF)) {
@ -2320,7 +2329,7 @@ public class Flow {
for (int i = firstadr; i < nextadr; i++) {
JCVariableDecl vardecl = vardecls[i];
VarSymbol var = vardecl.sym;
if (var.owner == classDef.sym) {
if (var.owner == classDef.sym && !var.isStatic()) {
// choose the diagnostic position based on whether
// the ctor is default(synthesized) or not
if (isSynthesized && !isCompactOrGeneratedRecordConstructor) {
@ -2329,7 +2338,6 @@ public class Flow {
} else if (isCompactOrGeneratedRecordConstructor) {
boolean isInstanceRecordField = var.enclClass().isRecord() &&
(var.flags_field & (Flags.PRIVATE | Flags.FINAL | Flags.GENERATED_MEMBER | Flags.RECORD)) != 0 &&
!var.isStatic() &&
var.owner.kind == TYP;
if (isInstanceRecordField) {
boolean notInitialized = !inits.isMember(var.adr);

View File

@ -0,0 +1,9 @@
/*
* @test /nodynamiccopyright/
* @bug 8184444
* @summary Report unintialized static final variables
* @compile/fail/ref=StaticFinalInit.out -XDrawDiagnostics StaticFinalInit.java
*/
class StaticFinalInit {
static final int i;
}

View File

@ -0,0 +1,2 @@
StaticFinalInit.java:8:22: compiler.err.var.might.not.have.been.initialized: i
1 error

View File

@ -1,2 +1,2 @@
CompileTimeErrorForNonAssignedStaticFieldTest.java:14:5: compiler.err.var.might.not.have.been.initialized: i
CompileTimeErrorForNonAssignedStaticFieldTest.java:9:30: compiler.err.var.might.not.have.been.initialized: i
1 error

View File

@ -147,7 +147,7 @@ public class TreeEndPosTest {
static void testFinalVariableWithConstructor() throws IOException {
compile(JavaSource.createJavaSource("public Bug (){} private static final String Foo; public void bar() { }",
"{}"));
"private static final String Foo;"));
}
static void testWholeTextSpan() throws IOException {