8221413: javac does not recognize variable assigned in switch expression as DA
Fixing definite assignment in presence of implicit throws clause in switch expressions over enums. Reviewed-by: vromero
This commit is contained in:
parent
2da45f12f2
commit
4969d23b89
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/switchexpr
@ -2226,7 +2226,11 @@ public class Flow {
|
||||
// Warn about fall-through if lint switch fallthrough enabled.
|
||||
}
|
||||
if (!hasDefault) {
|
||||
inits.andSet(initsSwitch);
|
||||
if (tree.hasTag(SWITCH_EXPRESSION)) {
|
||||
markDead();
|
||||
} else {
|
||||
inits.andSet(initsSwitch);
|
||||
}
|
||||
}
|
||||
resolveBreaks(tree, prevPendingExits);
|
||||
nextadr = nextadrPrev;
|
||||
|
@ -23,7 +23,7 @@
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8214031
|
||||
* @bug 8214031 8221413
|
||||
* @summary Verify that definite assignment when true works (legal code)
|
||||
* @compile --enable-preview --source ${jdk.version} DefiniteAssignment1.java
|
||||
* @run main/othervm --enable-preview DefiniteAssignment1
|
||||
@ -395,6 +395,36 @@ public class DefiniteAssignment1 {
|
||||
throw new IllegalStateException("Unexpected result.");
|
||||
}
|
||||
}
|
||||
|
||||
{ //JDK-8221413: definite assignment for implicit default in switch expressions
|
||||
int x;
|
||||
E e = E.A;
|
||||
|
||||
int v = (switch(e) {
|
||||
case A -> x = 0;
|
||||
case B -> x = 0;
|
||||
case C -> x = 0;
|
||||
});
|
||||
|
||||
if (x != 0 || v != 0) {
|
||||
throw new IllegalStateException("Unexpected result.");
|
||||
}
|
||||
}
|
||||
|
||||
{ //JDK-8221413: definite assignment for implicit default in switch expressions
|
||||
int x;
|
||||
E e = E.A;
|
||||
|
||||
boolean tA = (switch(e) {
|
||||
case A -> { x = 1; break true; }
|
||||
case B -> { x = 1; break true; }
|
||||
case C -> { x = 1; break true; }
|
||||
}) && x == 1; //x is definitelly assigned here
|
||||
|
||||
if (!tA) {
|
||||
throw new IllegalStateException("Unexpected result.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int id(int v) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user