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:
parent
4b8f7db6be
commit
6fa25cc134
@ -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
|
// define all the instance fields
|
||||||
for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
|
for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
|
||||||
if (l.head.hasTag(VARDEF)) {
|
if (l.head.hasTag(VARDEF)) {
|
||||||
@ -2320,7 +2329,7 @@ public class Flow {
|
|||||||
for (int i = firstadr; i < nextadr; i++) {
|
for (int i = firstadr; i < nextadr; i++) {
|
||||||
JCVariableDecl vardecl = vardecls[i];
|
JCVariableDecl vardecl = vardecls[i];
|
||||||
VarSymbol var = vardecl.sym;
|
VarSymbol var = vardecl.sym;
|
||||||
if (var.owner == classDef.sym) {
|
if (var.owner == classDef.sym && !var.isStatic()) {
|
||||||
// choose the diagnostic position based on whether
|
// choose the diagnostic position based on whether
|
||||||
// the ctor is default(synthesized) or not
|
// the ctor is default(synthesized) or not
|
||||||
if (isSynthesized && !isCompactOrGeneratedRecordConstructor) {
|
if (isSynthesized && !isCompactOrGeneratedRecordConstructor) {
|
||||||
@ -2329,7 +2338,6 @@ public class Flow {
|
|||||||
} else if (isCompactOrGeneratedRecordConstructor) {
|
} else if (isCompactOrGeneratedRecordConstructor) {
|
||||||
boolean isInstanceRecordField = var.enclClass().isRecord() &&
|
boolean isInstanceRecordField = var.enclClass().isRecord() &&
|
||||||
(var.flags_field & (Flags.PRIVATE | Flags.FINAL | Flags.GENERATED_MEMBER | Flags.RECORD)) != 0 &&
|
(var.flags_field & (Flags.PRIVATE | Flags.FINAL | Flags.GENERATED_MEMBER | Flags.RECORD)) != 0 &&
|
||||||
!var.isStatic() &&
|
|
||||||
var.owner.kind == TYP;
|
var.owner.kind == TYP;
|
||||||
if (isInstanceRecordField) {
|
if (isInstanceRecordField) {
|
||||||
boolean notInitialized = !inits.isMember(var.adr);
|
boolean notInitialized = !inits.isMember(var.adr);
|
||||||
|
@ -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;
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
StaticFinalInit.java:8:22: compiler.err.var.might.not.have.been.initialized: i
|
||||||
|
1 error
|
@ -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
|
1 error
|
||||||
|
@ -147,7 +147,7 @@ public class TreeEndPosTest {
|
|||||||
|
|
||||||
static void testFinalVariableWithConstructor() throws IOException {
|
static void testFinalVariableWithConstructor() throws IOException {
|
||||||
compile(JavaSource.createJavaSource("public Bug (){} private static final String Foo; public void bar() { }",
|
compile(JavaSource.createJavaSource("public Bug (){} private static final String Foo; public void bar() { }",
|
||||||
"{}"));
|
"private static final String Foo;"));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void testWholeTextSpan() throws IOException {
|
static void testWholeTextSpan() throws IOException {
|
||||||
|
Loading…
Reference in New Issue
Block a user