mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 02:54:35 +02:00
8030218: javac, compile time error isn't shown when final static field is not assigned, follow-up
Reviewed-by: jjg, jfranck, sundar
This commit is contained in:
parent
3c77de74be
commit
a4bac02fa4
3 changed files with 56 additions and 6 deletions
|
@ -1460,9 +1460,19 @@ public class Flow {
|
|||
this.names = names;
|
||||
}
|
||||
|
||||
private boolean isInitialConstructor = false;
|
||||
|
||||
@Override
|
||||
protected void markDead(JCTree tree) {
|
||||
if (!isInitialConstructor) {
|
||||
inits.inclRange(returnadr, nextadr);
|
||||
} else {
|
||||
for (int address = returnadr; address < nextadr; address++) {
|
||||
if (!(isFinalUninitializedStaticField(vardecls[address].sym))) {
|
||||
inits.incl(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
uninits.inclRange(returnadr, nextadr);
|
||||
}
|
||||
|
||||
|
@ -1475,8 +1485,17 @@ public class Flow {
|
|||
return
|
||||
sym.pos >= startPos &&
|
||||
((sym.owner.kind == MTH ||
|
||||
isFinalUninitializedField(sym)));
|
||||
}
|
||||
|
||||
boolean isFinalUninitializedField(VarSymbol sym) {
|
||||
return sym.owner.kind == TYP &&
|
||||
((sym.flags() & (FINAL | HASINIT | PARAMETER)) == FINAL &&
|
||||
classDef.sym.isEnclosedBy((ClassSymbol)sym.owner))));
|
||||
classDef.sym.isEnclosedBy((ClassSymbol)sym.owner));
|
||||
}
|
||||
|
||||
boolean isFinalUninitializedStaticField(VarSymbol sym) {
|
||||
return isFinalUninitializedField(sym) && sym.isStatic();
|
||||
}
|
||||
|
||||
/** Initialize new trackable variable by setting its address field
|
||||
|
@ -1730,10 +1749,9 @@ public class Flow {
|
|||
int returnadrPrev = returnadr;
|
||||
|
||||
Assert.check(pendingExits.isEmpty());
|
||||
|
||||
boolean lastInitialConstructor = isInitialConstructor;
|
||||
try {
|
||||
boolean isInitialConstructor =
|
||||
TreeInfo.isInitialConstructor(tree);
|
||||
isInitialConstructor = TreeInfo.isInitialConstructor(tree);
|
||||
|
||||
if (!isInitialConstructor) {
|
||||
firstadr = nextadr;
|
||||
|
@ -1788,6 +1806,7 @@ public class Flow {
|
|||
nextadr = nextadrPrev;
|
||||
firstadr = firstadrPrev;
|
||||
returnadr = returnadrPrev;
|
||||
isInitialConstructor = lastInitialConstructor;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8030218
|
||||
* @summary javac, compile time error isn't shown when final static field is not assigned, follow-up
|
||||
* @compile/fail/ref=CompileTimeErrorForNonAssignedStaticFieldTest.out -XDrawDiagnostics CompileTimeErrorForNonAssignedStaticFieldTest.java
|
||||
*/
|
||||
|
||||
public class CompileTimeErrorForNonAssignedStaticFieldTest {
|
||||
private final static int i;
|
||||
|
||||
public CompileTimeErrorForNonAssignedStaticFieldTest()
|
||||
throws InstantiationException {
|
||||
throw new InstantiationException("Can't instantiate");
|
||||
}
|
||||
|
||||
static class Inner {
|
||||
private final int j;
|
||||
public Inner(int x)
|
||||
throws InstantiationException {
|
||||
if (x == 0) {
|
||||
throw new InstantiationException("Can't instantiate");
|
||||
} else {
|
||||
j = 1;
|
||||
}
|
||||
System.out.println(j);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
CompileTimeErrorForNonAssignedStaticFieldTest.java:14:5: compiler.err.var.might.not.have.been.initialized: i
|
||||
1 error
|
Loading…
Add table
Add a link
Reference in a new issue