changeset 51542:1b687dc837a3 switch

Fixing error message when there are incompatible types in switch expression.
author jlahoda
date Tue, 10 Jul 2018 14:19:38 +0200
parents 36a1c43285a6
children 90bd2b3ccb1c
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/switchexpr/ExpressionSwitchInfer.java test/langtools/tools/javac/switchexpr/ExpressionSwitchInfer.out
diffstat 4 files changed, 26 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Jul 10 10:56:51 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Jul 10 14:19:38 2018 +0200
@@ -1417,8 +1417,7 @@
 
         ResultInfo condInfo = tree.polyKind == PolyKind.STANDALONE ?
                 unknownExprInfo :
-                //TODO: fix error in conditionalContext
-                resultInfo.dup(conditionalContext(resultInfo.checkContext));
+                resultInfo.dup(switchExpressionContext(resultInfo.checkContext));
 
         ListBuffer<Type> caseTypes = new ListBuffer<>();
 
@@ -1443,6 +1442,18 @@
 
         result = tree.type = check(tree, owntype, KindSelector.VAL, resultInfo);
     }
+    //where:
+        CheckContext switchExpressionContext(CheckContext checkContext) {
+            return new Check.NestedCheckContext(checkContext) {
+                //this will use enclosing check context to check compatibility of
+                //subexpression against target type; if we are in a method check context,
+                //depending on whether boxing is allowed, we could have incompatibilities
+                @Override
+                public void report(DiagnosticPosition pos, JCDiagnostic details) {
+                    enclosingContext.report(pos, diags.fragment(Fragments.IncompatibleTypeInSwitchExpression(details)));
+                }
+            };
+        }
 
     private void handleSwitch(JCTree switchTree,
                               JCExpression selector,
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Jul 10 10:56:51 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Jul 10 14:19:38 2018 +0200
@@ -840,6 +840,11 @@
     target-type for switch expression cannot be void
 
 # 0: message segment
+compiler.misc.incompatible.type.in.switch.expression=\
+    bad type in switch expression\n\
+    {0}
+
+# 0: message segment
 compiler.misc.incompatible.ret.type.in.lambda=\
     bad return type in lambda expression\n\
     {0}
--- a/test/langtools/tools/javac/switchexpr/ExpressionSwitchInfer.java	Tue Jul 10 10:56:51 2018 +0200
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitchInfer.java	Tue Jul 10 14:19:38 2018 +0200
@@ -24,6 +24,12 @@
             default -> new ArrayList<>();
         }, CharSequence.class, param).substring(0);
 
+        String str = switch (param) {
+            case "" -> {
+                break 0;
+            } default ->"default";
+        };
+
         return null;
     }
 
--- a/test/langtools/tools/javac/switchexpr/ExpressionSwitchInfer.out	Tue Jul 10 10:56:51 2018 +0200
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitchInfer.out	Tue Jul 10 14:19:38 2018 +0200
@@ -1,5 +1,6 @@
 ExpressionSwitchInfer.java:16:95: compiler.err.cant.resolve.location.args: kindname.method, substring, , int, (compiler.misc.location: kindname.interface, java.lang.CharSequence, null)
 ExpressionSwitchInfer.java:25:38: compiler.err.cant.resolve.location.args: kindname.method, substring, , int, (compiler.misc.location: kindname.interface, java.lang.CharSequence, null)
+ExpressionSwitchInfer.java:29:23: compiler.err.prob.found.req: (compiler.misc.incompatible.type.in.switch.expression: (compiler.misc.inconvertible.types: int, java.lang.String))
 - compiler.note.preview.filename: ExpressionSwitchInfer.java
 - compiler.note.preview.recompile
-2 errors
+3 errors