Skip to content

Commit cff0747

Browse files
committed
8326204: yield statements doesn't allow cast expressions with more than 1 type arguments
Reviewed-by: jlahoda
1 parent 6f2676d commit cff0747

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -2894,12 +2894,15 @@ List<JCStatement> blockStatement() {
28942894
int lookahead = 2;
28952895
int balance = 1;
28962896
boolean hasComma = false;
2897+
boolean inTypeArgs = false;
28972898
Token l;
28982899
while ((l = S.token(lookahead)).kind != EOF && balance != 0) {
28992900
switch (l.kind) {
29002901
case LPAREN: balance++; break;
29012902
case RPAREN: balance--; break;
2902-
case COMMA: if (balance == 1) hasComma = true; break;
2903+
case COMMA: if (balance == 1 && !inTypeArgs) hasComma = true; break;
2904+
case LT: inTypeArgs = true; break;
2905+
case GT: inTypeArgs = false;
29032906
}
29042907
lookahead++;
29052908
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8326204
27+
* @summary yield statements doesn't allow cast expressions with more than 1 type arguments
28+
* @compile T8326204a.java
29+
*/
30+
import java.util.*;
31+
32+
public class T8326204a {
33+
void testOneParam() {
34+
Object value = new ArrayList<String>();
35+
Object returnedValue = switch (1) {
36+
default -> {
37+
yield (List<String>) value;
38+
}
39+
};
40+
}
41+
42+
void testTwoParams() {
43+
Object value = new HashMap<String, String>();
44+
Object returnedValue = switch (1) {
45+
default -> {
46+
yield (Map<String, String>) value;
47+
}
48+
};
49+
}
50+
51+
void testTwoParamsInParens() {
52+
Object value = new HashMap<String, String>();
53+
Object returnedValue = switch (1) {
54+
default -> {
55+
yield ((Map<String, String>) value);
56+
}
57+
};
58+
}
59+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.Map;
2+
3+
/*
4+
* @test /nodynamiccopyright/
5+
* @bug 8326204
6+
* @summary yield statements doesn't allow cast expressions with more than 1 type arguments
7+
* @compile/fail/ref=T8326204b.out -XDrawDiagnostics --should-stop=at=FLOW -XDdev T8326204b.java
8+
*/
9+
public class T8326204b {
10+
private static void t(int i) { yield((Map<String, String>) null, 2); }
11+
12+
private static void yield(Map<String, String> m, int j) { }
13+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
T8326204b.java:10:36: compiler.err.invalid.yield
2+
1 error

0 commit comments

Comments
 (0)