8221413: javac does not recognize variable assigned in switch expression as DA
authorjlahoda
Thu, 28 Mar 2019 10:32:05 +0100
changeset 54321 c9a492ad1aed
parent 54320 e9618b37f0a5
child 54322 cf75ea6af695
8221413: javac does not recognize variable assigned in switch expression as DA Summary: Fixing definite assignment in presence of implicit throws clause in switch expressions over enums. Reviewed-by: vromero
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java
test/langtools/tools/javac/switchexpr/DefiniteAssignment1.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Mar 28 09:15:41 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Mar 28 10:32:05 2019 +0100
@@ -2226,7 +2226,11 @@
                 // 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;
--- a/test/langtools/tools/javac/switchexpr/DefiniteAssignment1.java	Thu Mar 28 09:15:41 2019 +0100
+++ b/test/langtools/tools/javac/switchexpr/DefiniteAssignment1.java	Thu Mar 28 10:32:05 2019 +0100
@@ -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 @@
             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) {