From cff0747d7f62efc3dafcd259ef2b15cd13bafbeb Mon Sep 17 00:00:00 2001 From: Aggelos Biboudis Date: Thu, 14 Mar 2024 07:01:32 +0000 Subject: [PATCH] 8326204: yield statements doesn't allow cast expressions with more than 1 type arguments Reviewed-by: jlahoda --- .../sun/tools/javac/parser/JavacParser.java | 5 +- test/langtools/tools/javac/T8326204a.java | 59 +++++++++++++++++++ test/langtools/tools/javac/T8326204b.java | 13 ++++ test/langtools/tools/javac/T8326204b.out | 2 + 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 test/langtools/tools/javac/T8326204a.java create mode 100644 test/langtools/tools/javac/T8326204b.java create mode 100644 test/langtools/tools/javac/T8326204b.out diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java index e1a6d08303c..2c0f95b5432 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java @@ -2894,12 +2894,15 @@ public class JavacParser implements Parser { int lookahead = 2; int balance = 1; boolean hasComma = false; + boolean inTypeArgs = false; Token l; while ((l = S.token(lookahead)).kind != EOF && balance != 0) { switch (l.kind) { case LPAREN: balance++; break; case RPAREN: balance--; break; - case COMMA: if (balance == 1) hasComma = true; break; + case COMMA: if (balance == 1 && !inTypeArgs) hasComma = true; break; + case LT: inTypeArgs = true; break; + case GT: inTypeArgs = false; } lookahead++; } diff --git a/test/langtools/tools/javac/T8326204a.java b/test/langtools/tools/javac/T8326204a.java new file mode 100644 index 00000000000..962fd60ee07 --- /dev/null +++ b/test/langtools/tools/javac/T8326204a.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2024, 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 8326204 + * @summary yield statements doesn't allow cast expressions with more than 1 type arguments + * @compile T8326204a.java + */ +import java.util.*; + +public class T8326204a { + void testOneParam() { + Object value = new ArrayList(); + Object returnedValue = switch (1) { + default -> { + yield (List) value; + } + }; + } + + void testTwoParams() { + Object value = new HashMap(); + Object returnedValue = switch (1) { + default -> { + yield (Map) value; + } + }; + } + + void testTwoParamsInParens() { + Object value = new HashMap(); + Object returnedValue = switch (1) { + default -> { + yield ((Map) value); + } + }; + } +} diff --git a/test/langtools/tools/javac/T8326204b.java b/test/langtools/tools/javac/T8326204b.java new file mode 100644 index 00000000000..e29090b73a3 --- /dev/null +++ b/test/langtools/tools/javac/T8326204b.java @@ -0,0 +1,13 @@ +import java.util.Map; + +/* + * @test /nodynamiccopyright/ + * @bug 8326204 + * @summary yield statements doesn't allow cast expressions with more than 1 type arguments + * @compile/fail/ref=T8326204b.out -XDrawDiagnostics --should-stop=at=FLOW -XDdev T8326204b.java + */ +public class T8326204b { + private static void t(int i) { yield((Map) null, 2); } + + private static void yield(Map m, int j) { } +} diff --git a/test/langtools/tools/javac/T8326204b.out b/test/langtools/tools/javac/T8326204b.out new file mode 100644 index 00000000000..17864700bda --- /dev/null +++ b/test/langtools/tools/javac/T8326204b.out @@ -0,0 +1,2 @@ +T8326204b.java:10:36: compiler.err.invalid.yield +1 error