--- old/make/langtools/tools/propertiesparser/parser/MessageType.java 2018-08-03 14:07:24.282792679 +0200 +++ new/make/langtools/tools/propertiesparser/parser/MessageType.java 2018-08-03 14:07:23.286792725 +0200 @@ -95,6 +95,7 @@ KIND_NAME("kind name", "KindName", "com.sun.tools.javac.code.Kinds"), TARGET("target", "Target", "com.sun.tools.javac.jvm"), TOKEN("token", "TokenKind", "com.sun.tools.javac.parser.Tokens"), + TREE_TAG("tree tag", "Tag", "com.sun.tools.javac.tree.JCTree"), TYPE("type", "Type", "com.sun.tools.javac.code"), URL("url", "URL", "java.net"), SET("set", "Set", "java.util"), --- old/src/jdk.compiler/share/classes/com/sun/source/tree/CaseTree.java 2018-08-03 14:07:25.238792634 +0200 +++ new/src/jdk.compiler/share/classes/com/sun/source/tree/CaseTree.java 2018-08-03 14:07:24.798792654 +0200 @@ -69,11 +69,10 @@ List getExpressions(); /** - * Returns the statements labeled by the case. - * If this is a case in switch expression in for - * {@code case expression -> value}, returns a list - * containing a single synthetic BreakTree with - * the given value. + * For case with kind {@linkplain CaseKind#STATEMENT}, + * returns the statements labeled by the case. + * Returns {@code null} for case with kind + * {@linkplain CaseKind#RULE}. * @return the statements labeled by the case or null */ List getStatements(); --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java 2018-08-03 14:07:26.338792583 +0200 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java 2018-08-03 14:07:25.874792604 +0200 @@ -1906,7 +1906,7 @@ attribTree(tree.value, env, env.info.breakResult); JCTree immediateTarget = findJumpTarget(tree.pos(), tree.getTag(), null, env); if (immediateTarget.getTag() != SWITCH_EXPRESSION) { - log.error(tree.pos(), Errors.BreakExprNotImmediate); + log.error(tree.pos(), Errors.BreakExprNotImmediate(immediateTarget.getTag())); Env env1 = env; while (env1 != null && env1.tree.getTag() != SWITCH_EXPRESSION) { env1 = env1.next; --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java 2018-08-03 14:07:27.314792537 +0200 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java 2018-08-03 14:07:26.942792555 +0200 @@ -3366,6 +3366,11 @@ cases.append(c); break; default: //multiple labels, expand: + //case C1, C2, C3: ... + //=> + //case C1: + //case C2: + //case C3: ... List patterns = c.pats; while (patterns.tail.nonEmpty()) { cases.append(make_at(c.pos()).Case(JCCase.STATEMENT, @@ -3618,6 +3623,18 @@ @Override public void visitSwitchExpression(JCSwitchExpression tree) { + //translates switch expression to statement switch: + //switch (selector) { + // case C: break value; + // ... + //} + //=> + //(letexpr T exprswitch$; + // switch (selector) { + // case C: { exprswitch$ = value; break; } + // } + // exprswitch$ + //) VarSymbol dollar_switchexpr = new VarSymbol(Flags.FINAL|Flags.SYNTHETIC, names.fromString("exprswitch" + tree.pos + target.syntheticNameChar()), tree.type, --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties 2018-08-03 14:07:28.342792489 +0200 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties 2018-08-03 14:07:27.866792511 +0200 @@ -49,6 +49,7 @@ # kind name an informative description of the kind of a declaration; see compiler.misc.kindname.* # target a target version number, such as 1.5, 1.6, 1.7, taken from a com.sun.tools.javac.jvm.Target # token the name of a non-terminal in source code; see compiler.misc.token.* +# tree tag the name of a non-terminal in source code; see compiler.misc.token.* # type a Java type; e.g. int, X, X # url a URL # object a Java object (unspecified) @@ -188,26 +189,28 @@ break outside switch or loop compiler.err.break.missing.value=\ - break is missing a value to return from switch expression + missing break value compiler.err.break.outside.switch.expression=\ - break is jumping outside of the enclosing switch expression + break outside of enclosing switch expression compiler.err.continue.outside.switch.expression=\ - continue is jumping outside of the enclosing switch expression + continue outside of enclosing switch expression compiler.err.return.outside.switch.expression=\ - return is returning outside of the enclosing switch expression + return outside of enclosing switch expression # 0: name compiler.err.break.ambiguous.target=\ - {0} is ambiguously both a valid expression and a valid label + ambiguous reference to ''{0}''\n\ + (''{0}'' is both a label and an expression) +# 0: tree tag compiler.err.break.expr.not.immediate=\ - expression break not immediately enclosed by a switch expression + value break not supported in ''{0}'' compiler.err.break.complex.value.no.switch.expression=\ - value break outside of switch expression + unexpected value break compiler.err.switch.expression.empty=\ switch expression does not have any case clauses @@ -2609,6 +2612,22 @@ compiler.misc.kindname.instance.init=\ instance initializer +# the following are names of tree kinds: +compiler.misc.tree.tag.forloop=\ + for + +compiler.misc.tree.tag.foreachloop=\ + enhanced for + +compiler.misc.tree.tag.whileloop=\ + while + +compiler.misc.tree.tag.doloop=\ + do + +compiler.misc.tree.tag.switch=\ + switch + ##### compiler.misc.no.args=\ --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java 2018-08-03 14:07:29.166792451 +0200 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java 2018-08-03 14:07:28.806792468 +0200 @@ -1276,7 +1276,10 @@ @SuppressWarnings("removal") public List getExpressions() { return pats; } @Override @DefinedBy(Api.COMPILER_TREE) - public List getStatements() { return stats; } + @SuppressWarnings("removal") + public List getStatements() { + return caseKind == CaseKind.STATEMENT ? stats : null; + } @Override @DefinedBy(Api.COMPILER_TREE) @SuppressWarnings("removal") public JCTree getBody() { return body; } --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java 2018-08-03 14:07:29.958792414 +0200 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java 2018-08-03 14:07:29.610792430 +0200 @@ -219,6 +219,10 @@ else if (arg instanceof Source) { return ((Source)arg).name; } + else if (arg instanceof Tag) { + return messages.getLocalizedString(l, "compiler.misc.tree.tag." + + StringUtils.toLowerCase(((Tag) arg).name())); + } else { return String.valueOf(arg); } --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/util/RawDiagnosticFormatter.java 2018-08-03 14:07:30.714792379 +0200 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/util/RawDiagnosticFormatter.java 2018-08-03 14:07:30.370792395 +0200 @@ -158,6 +158,8 @@ s = "@" + rawDiagnosticPosHelper.getPosition((JCExpression)arg); } else if (arg instanceof PathFileObject) { s = ((PathFileObject) arg).getShortName(); + } else if (arg instanceof Tag) { + s = "compiler.misc.tree.tag." + StringUtils.toLowerCase(((Tag) arg).name()); } else { s = super.formatArgument(diag, arg, null); } --- old/test/langtools/tools/javac/diags/CheckResourceKeys.java 2018-08-03 14:07:31.498792342 +0200 +++ new/test/langtools/tools/javac/diags/CheckResourceKeys.java 2018-08-03 14:07:31.142792359 +0200 @@ -302,6 +302,7 @@ // prefix/embedded strings "compiler.", "compiler.misc.", + "compiler.misc.tree.tag.", "opt.Xlint.desc.", "count.", "illegal.", --- old/test/langtools/tools/javac/diags/examples/BreakExprNotImmediate.java 2018-08-03 14:07:32.266792306 +0200 +++ new/test/langtools/tools/javac/diags/examples/BreakExprNotImmediate.java 2018-08-03 14:07:31.914792323 +0200 @@ -22,17 +22,44 @@ */ // key: compiler.err.break.expr.not.immediate +// key: compiler.misc.tree.tag.doloop +// key: compiler.misc.tree.tag.foreachloop +// key: compiler.misc.tree.tag.forloop +// key: compiler.misc.tree.tag.switch +// key: compiler.misc.tree.tag.whileloop +// key: compiler.note.note +// key: compiler.err.error +// key: compiler.misc.count.error.plural // key: compiler.note.preview.filename // key: compiler.note.preview.recompile +// key: compiler.note.note // options: --enable-preview -source 12 +// run: backdoor class BreakExprNotImmediate { int t(int i) { return switch (i) { - default: + case 0: + for (; ;) { + break 1 + 1; + } + case 1: + for (String s : new String[0]) { + break 1 + 1; + } + case 2: while (true) { break 1 + 1; } + case 3: + do { + break 1 + 1; + } while (true); + case 4: + switch (i) { + default: break 1 + 1; + } + break 0; }; } } --- old/test/langtools/tools/javac/switchexpr/BreakTest.java 2018-08-03 14:07:33.054792270 +0200 +++ new/test/langtools/tools/javac/switchexpr/BreakTest.java 2018-08-03 14:07:32.686792287 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 20018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it --- old/test/langtools/tools/javac/switchexpr/ExpressionSwitchBreaks2.out 2018-08-03 14:07:33.806792235 +0200 +++ new/test/langtools/tools/javac/switchexpr/ExpressionSwitchBreaks2.out 2018-08-03 14:07:33.466792250 +0200 @@ -5,7 +5,7 @@ ExpressionSwitchBreaks2.java:28:29: compiler.err.continue.outside.switch.expression ExpressionSwitchBreaks2.java:29:29: compiler.err.continue.outside.switch.expression ExpressionSwitchBreaks2.java:30:29: compiler.err.undef.label: UNKNOWN -ExpressionSwitchBreaks2.java:34:37: compiler.err.break.expr.not.immediate +ExpressionSwitchBreaks2.java:34:37: compiler.err.break.expr.not.immediate: compiler.misc.tree.tag.switch ExpressionSwitchBreaks2.java:40:17: compiler.err.break.complex.value.no.switch.expression ExpressionSwitchBreaks2.java:40:29: compiler.err.cant.resolve.location: kindname.variable, undef, , , (compiler.misc.location: kindname.class, ExpressionSwitchBreaks2, null) ExpressionSwitchBreaks2.java:44:22: compiler.err.break.ambiguous.target: j --- /dev/null 2018-07-31 07:28:32.999928974 +0200 +++ new/test/langtools/tools/javac/switchextra/CaseTest.java 2018-08-03 14:07:34.186792217 +0200 @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8206986 + * @summary Ensure CaseTree methods return expected values + * @modules jdk.compiler + */ + +import java.io.IOException; +import java.io.StringWriter; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import javax.tools.*; + +import com.sun.source.tree.CaseTree; +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.Tree; +import com.sun.source.util.JavacTask; +import com.sun.source.util.TreePathScanner; + +public class CaseTest { + + public static void main(String[] args) throws Exception { + new CaseTest().testLabels(); + new CaseTest().testStatement(); + new CaseTest().testRule(); + } + + void testLabels() throws Exception { + String code = "class Test {\n" + + " void t(int i) {\n" + + " switch(i) {\n" + + " case 0: break;\n" + + " case 1, 2: breal;\n" + + " default: breal;\n" + + " }\n" + + " }\n" + + "}\n"; + List labels = new ArrayList<>(); + new TreePathScanner() { + @Override + public Void visitCase(CaseTree node, Void p) { + labels.add(String.valueOf(node.getExpression())); + labels.add(node.getExpressions().stream() + .map(String::valueOf) + .collect(Collectors.joining(",", "[", "]"))); + return super.visitCase(node, p); + } + }.scan(parse(code), null); + + List expected = Arrays.asList("0", "[0]", "1", "[1,2]", "null", "[]"); + + if (!expected.equals(labels)) { + throw new AssertionError("Unexpected labels found: " + labels); + } + } + + void testStatement() throws Exception { + String code = "class Test {\n" + + " void t(int i) {\n" + + " switch(i) {\n" + + " case 0:" + + " System.err.println();\n" + + " break;\n" + + " }\n" + + " }\n" + + "}\n"; + new TreePathScanner() { + @Override + public Void visitCase(CaseTree node, Void p) { + if (node.getStatements().size() != 2) { + throw new AssertionError("Unexpected statements: " + node.getStatements()); + } + if (node.getBody() != null) { + throw new AssertionError("Unexpected body: " + node.getBody()); + } + return super.visitCase(node, p); + } + }.scan(parse(code), null); + } + + void testRule() throws Exception { + String code = "class Test {\n" + + " void t(int i) {\n" + + " switch(i) {\n" + + " case 0 -> {" + + " System.err.println();\n" + + " };\n" + + " }\n" + + " }\n" + + "}\n"; + new TreePathScanner() { + @Override + public Void visitCase(CaseTree node, Void p) { + if (node.getStatements() != null) { + throw new AssertionError("Unexpected statements: " + node.getStatements()); + } + if (node.getBody().getKind() != Tree.Kind.BLOCK) { + throw new AssertionError("Unexpected body: " + node.getBody()); + } + return super.visitCase(node, p); + } + }.scan(parse(code), null); + } + + private CompilationUnitTree parse(String code) throws IOException { + final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); + assert tool != null; + DiagnosticListener noErrors = d -> {}; + + StringWriter out = new StringWriter(); + JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors, + List.of("-XDdev", "--enable-preview", "-source", "12"), null, + Arrays.asList(new MyFileObject(code))); + return ct.parse().iterator().next(); + } + + static class MyFileObject extends SimpleJavaFileObject { + private String text; + + public MyFileObject(String text) { + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); + this.text = text; + } + + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) { + return text; + } + } +}