8214529: Exception while using Anonymous class in switch expression
authorjlahoda
Mon, 03 Dec 2018 14:25:00 +0100
changeset 52796 2c8e6decb1c3
parent 52795 9501a7b59111
child 52797 55a05ed55768
8214529: Exception while using Anonymous class in switch expression Summary: Clearing breakResult when creating methodEnv. Reviewed-by: mcimadamore
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MemberEnter.java
test/langtools/tools/javac/switchexpr/ExpressionSwitchBugs.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MemberEnter.java	Mon Dec 03 14:28:19 2018 +0300
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MemberEnter.java	Mon Dec 03 14:25:00 2018 +0100
@@ -245,6 +245,7 @@
                                                              tree.sym.type.getReturnType());
         }
         if ((tree.mods.flags & STATIC) != 0) localEnv.info.staticLevel++;
+        localEnv.info.breakResult = null;
         return localEnv;
     }
 
--- a/test/langtools/tools/javac/switchexpr/ExpressionSwitchBugs.java	Mon Dec 03 14:28:19 2018 +0300
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitchBugs.java	Mon Dec 03 14:25:00 2018 +0100
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8206986
+ * @bug 8206986 8214529
  * @summary Verify various corner cases with nested switch expressions.
  * @compile --enable-preview -source 12 ExpressionSwitchBugs.java
  * @run main/othervm --enable-preview ExpressionSwitchBugs
@@ -32,6 +32,7 @@
 public class ExpressionSwitchBugs {
     public static void main(String... args) {
         new ExpressionSwitchBugs().testNested();
+        new ExpressionSwitchBugs().testAnonymousClasses();
     }
 
     private void testNested() {
@@ -66,6 +67,23 @@
         }));
     }
 
+    private void testAnonymousClasses() {
+        for (int i : new int[] {1, 2}) {
+            check(3, id((switch (i) {
+                case 1: break new I() {
+                    public int g() { return 3; }
+                };
+                default: break (I) () -> { return 3; };
+            }).g()));
+            check(3, id((switch (i) {
+                case 1 -> new I() {
+                    public int g() { return 3; }
+                };
+                default -> (I) () -> { return 3; };
+            }).g()));
+        }
+    }
+
     private int id(int i) {
         return i;
     }
@@ -79,4 +97,8 @@
             throw new AssertionError("Unexpected result: " + actual);
         }
     }
+
+    public interface I {
+        public int g();
+    }
 }