changeset 52953:2c8e6decb1c3

8214529: Exception while using Anonymous class in switch expression Summary: Clearing breakResult when creating methodEnv. Reviewed-by: mcimadamore
author jlahoda
date Mon, 03 Dec 2018 14:25:00 +0100
parents 9501a7b59111
children 55a05ed55768
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MemberEnter.java test/langtools/tools/javac/switchexpr/ExpressionSwitchBugs.java
diffstat 2 files changed, 24 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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();
+    }
 }