From 6fa25cc134e8a6787490e080fb98c2d61cf0b049 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Thu, 23 Mar 2023 16:06:47 +0000 Subject: [PATCH] 8184444: The compiler error "variable not initialized in the default constructor" is not apt in case of static final variables Reviewed-by: vromero --- .../share/classes/com/sun/tools/javac/comp/Flow.java | 12 ++++++++++-- .../javac/DefiniteAssignment/StaticFinalInit.java | 9 +++++++++ .../javac/DefiniteAssignment/StaticFinalInit.out | 2 ++ ...CompileTimeErrorForNonAssignedStaticFieldTest.out | 2 +- .../tools/javac/positions/TreeEndPosTest.java | 2 +- 5 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 test/langtools/tools/javac/DefiniteAssignment/StaticFinalInit.java create mode 100644 test/langtools/tools/javac/DefiniteAssignment/StaticFinalInit.out diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java index 70bab056c5a..4e2ad71a053 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java @@ -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 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); diff --git a/test/langtools/tools/javac/DefiniteAssignment/StaticFinalInit.java b/test/langtools/tools/javac/DefiniteAssignment/StaticFinalInit.java new file mode 100644 index 00000000000..13aeafb96e1 --- /dev/null +++ b/test/langtools/tools/javac/DefiniteAssignment/StaticFinalInit.java @@ -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; +} diff --git a/test/langtools/tools/javac/DefiniteAssignment/StaticFinalInit.out b/test/langtools/tools/javac/DefiniteAssignment/StaticFinalInit.out new file mode 100644 index 00000000000..80ee4fed0f3 --- /dev/null +++ b/test/langtools/tools/javac/DefiniteAssignment/StaticFinalInit.out @@ -0,0 +1,2 @@ +StaticFinalInit.java:8:22: compiler.err.var.might.not.have.been.initialized: i +1 error diff --git a/test/langtools/tools/javac/flow/T8030218/CompileTimeErrorForNonAssignedStaticFieldTest.out b/test/langtools/tools/javac/flow/T8030218/CompileTimeErrorForNonAssignedStaticFieldTest.out index 7821299792f..d8fe0a12e7e 100644 --- a/test/langtools/tools/javac/flow/T8030218/CompileTimeErrorForNonAssignedStaticFieldTest.out +++ b/test/langtools/tools/javac/flow/T8030218/CompileTimeErrorForNonAssignedStaticFieldTest.out @@ -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 diff --git a/test/langtools/tools/javac/positions/TreeEndPosTest.java b/test/langtools/tools/javac/positions/TreeEndPosTest.java index d3454298128..990053a7d4e 100644 --- a/test/langtools/tools/javac/positions/TreeEndPosTest.java +++ b/test/langtools/tools/javac/positions/TreeEndPosTest.java @@ -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 {