changeset 54321:c9a492ad1aed

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
author jlahoda
date Thu, 28 Mar 2019 10:32:05 +0100
parents e9618b37f0a5
children cf75ea6af695
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java test/langtools/tools/javac/switchexpr/DefiniteAssignment1.java
diffstat 2 files changed, 36 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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) {