changeset 51548:bd1b9d9af00d switch

Ambiguity between expression and label in breaks should be an error.
author jlahoda
date Wed, 11 Jul 2018 11:07:00 +0200
parents 39fb108ceec8
children 5e622e823b4e
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties test/langtools/tools/javac/diags/examples/BreakAmbiguousTarget.java test/langtools/tools/javac/expswitch/ExpSwitchNestingTest.java test/langtools/tools/javac/switchexpr/ExpressionSwitchBreaks1.java test/langtools/tools/javac/switchexpr/ExpressionSwitchBreaks2.java test/langtools/tools/javac/switchexpr/ExpressionSwitchBreaks2.out
diffstat 7 files changed, 11 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Jul 11 11:06:01 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Jul 11 11:07:00 2018 +0200
@@ -1883,7 +1883,7 @@
                     if (jumpTarget.fst != null) {
                         JCTree speculative = deferredAttr.attribSpeculative(tree.value, env, unknownExprInfo);
                         if (!speculative.type.hasTag(ERROR)) {
-                            log.warning(tree.pos(), Warnings.BreakAmbiguousTarget(label));
+                            log.error(tree.pos(), Errors.BreakAmbiguousTarget(label));
                             if (jumpTarget.snd == null) {
                                 tree.target = jumpTarget.fst;
                                 attribute = false;
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Jul 11 11:06:01 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Jul 11 11:07:00 2018 +0200
@@ -200,7 +200,7 @@
     return is returning outside of the enclosing switch expression
 
 # 0: name
-compiler.warn.break.ambiguous.target=\
+compiler.err.break.ambiguous.target=\
     {0} is both a valid expression and a valid label
 
 compiler.err.break.expr.not.immediate=\
--- a/test/langtools/tools/javac/diags/examples/BreakAmbiguousTarget.java	Wed Jul 11 11:06:01 2018 +0200
+++ b/test/langtools/tools/javac/diags/examples/BreakAmbiguousTarget.java	Wed Jul 11 11:07:00 2018 +0200
@@ -21,7 +21,7 @@
  * questions.
  */
 
-// key: compiler.warn.break.ambiguous.target
+// key: compiler.err.break.ambiguous.target
 // key: compiler.note.preview.filename
 // key: compiler.note.preview.recompile
 // options: --enable-preview -source 12
--- a/test/langtools/tools/javac/expswitch/ExpSwitchNestingTest.java	Wed Jul 11 11:06:01 2018 +0200
+++ b/test/langtools/tools/javac/expswitch/ExpSwitchNestingTest.java	Wed Jul 11 11:07:00 2018 +0200
@@ -208,9 +208,8 @@
 
     public void testBreakExpressionLabelDisambiguation() {
         assertOK(DEF_LABEL_VAR, ESWITCH_Z, BREAK_L);
-        assertOKWithWarning("compiler.warn.break.ambiguous.target", DEF_LABEL_VAR, ESWITCH_Z, LABEL, FOR, BREAK_L); //label break
-        assertOKWithWarning("compiler.warn.break.ambiguous.target", DEF_LABEL_VAR, LABEL, BLOCK, ESWITCH_Z, BREAK_L); //expression break
-        //
+        assertFail("compiler.err.break.ambiguous.target", DEF_LABEL_VAR, ESWITCH_Z, LABEL, FOR, BREAK_L);
+        assertFail("compiler.err.break.ambiguous.target", DEF_LABEL_VAR, LABEL, BLOCK, ESWITCH_Z, BREAK_L);
     }
 
     public void testFunReturningSwitchExp() {
--- a/test/langtools/tools/javac/switchexpr/ExpressionSwitchBreaks1.java	Wed Jul 11 11:06:01 2018 +0200
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitchBreaks1.java	Wed Jul 11 11:07:00 2018 +0200
@@ -93,13 +93,12 @@
             case 1:
                 break "1";
             case 2:
-                int LOOP = 0;
-                LOOP: while (j-- > 0) {
+                LOP: while (j-- > 0) {
                     if (k == 5) {
                         k--;
                         continue;
                     }
-                    break LOOP; //may produce a warning
+                    break LOP;
                 }
                 Supplier<String> getter = () -> { return "2-X-5"; };
                 break getter.get();
--- a/test/langtools/tools/javac/switchexpr/ExpressionSwitchBreaks2.java	Wed Jul 11 11:06:01 2018 +0200
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitchBreaks2.java	Wed Jul 11 11:07:00 2018 +0200
@@ -40,7 +40,7 @@
         }
         }
         j: print(switch (i) {
-            default: break j;
+            default: break j; //error: "j" is ambiguous (expression/label)
         }, 0);
         j2: print(switch (i) {
             default: break j2;
--- a/test/langtools/tools/javac/switchexpr/ExpressionSwitchBreaks2.out	Wed Jul 11 11:06:01 2018 +0200
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitchBreaks2.out	Wed Jul 11 11:07:00 2018 +0200
@@ -1,6 +1,6 @@
 ExpressionSwitchBreaks2.java:16:25: compiler.err.break.missing.value
 ExpressionSwitchBreaks2.java:18:25: compiler.err.break.outside.switch.expression
-ExpressionSwitchBreaks2.java:22:37: compiler.warn.break.ambiguous.target: x
+ExpressionSwitchBreaks2.java:22:37: compiler.err.break.ambiguous.target: x
 ExpressionSwitchBreaks2.java:26:29: compiler.err.return.outside.switch.expression
 ExpressionSwitchBreaks2.java:27:29: compiler.err.continue.outside.switch.expression
 ExpressionSwitchBreaks2.java:28:29: compiler.err.continue.outside.switch.expression
@@ -8,9 +8,8 @@
 ExpressionSwitchBreaks2.java:33:37: compiler.err.break.expr.not.immediate
 ExpressionSwitchBreaks2.java:39:17: compiler.err.break.complex.value.no.switch.expression
 ExpressionSwitchBreaks2.java:39:29: compiler.err.cant.resolve.location: kindname.variable, undef, , , (compiler.misc.location: kindname.class, ExpressionSwitchBreaks2, null)
-ExpressionSwitchBreaks2.java:43:22: compiler.warn.break.ambiguous.target: j
+ExpressionSwitchBreaks2.java:43:22: compiler.err.break.ambiguous.target: j
 ExpressionSwitchBreaks2.java:46:22: compiler.err.break.outside.switch.expression
 - compiler.note.preview.filename: ExpressionSwitchBreaks2.java
 - compiler.note.preview.recompile
-10 errors
-2 warnings
+12 errors