Skip to content

Commit f3b69da

Browse files
Evemoselahodaj
authored andcommitted
8335136: Underscore as parameter name in one-parameter functional types fails to compile
Reviewed-by: jlahoda
1 parent 46b817b commit f3b69da

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2920,7 +2920,7 @@ List<JCStatement> blockStatement() {
29202920
case PLUS: case SUB: case STRINGLITERAL: case CHARLITERAL:
29212921
case STRINGFRAGMENT:
29222922
case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: case DOUBLELITERAL:
2923-
case NULL: case IDENTIFIER: case TRUE: case FALSE:
2923+
case NULL: case IDENTIFIER: case UNDERSCORE: case TRUE: case FALSE:
29242924
case NEW: case SWITCH: case THIS: case SUPER:
29252925
case BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, VOID, BOOLEAN:
29262926
isYieldStatement = true;
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 8335136
27+
* @summary Underscore as parameter name in one-parameter functional types fails to compile in yield statement if not enclosed in parentheses
28+
* @library /tools/lib
29+
* @modules jdk.compiler/com.sun.tools.javac.api
30+
* jdk.compiler/com.sun.tools.javac.main
31+
* @build toolbox.JavacTask toolbox.ToolBox toolbox.Task
32+
* @run main ExpressionSwitchUnderscoreAfterYield
33+
*/
34+
35+
import toolbox.*;
36+
37+
import java.nio.file.Path;
38+
import java.util.List;
39+
40+
public class ExpressionSwitchUnderscoreAfterYield extends TestRunner {
41+
42+
private final ToolBox tb = new ToolBox();
43+
44+
private final Path ROOT = Path.of(".");
45+
46+
public ExpressionSwitchUnderscoreAfterYield() {
47+
super(System.err);
48+
}
49+
50+
public static void main(String[] args) throws Exception {
51+
new ExpressionSwitchUnderscoreAfterYield().runTests();
52+
}
53+
54+
protected void runTests() throws Exception {
55+
runTests(f -> {
56+
if (f.getName().endsWith("_ShouldFailToCompile")) {
57+
return new Object[]{
58+
List.of(
59+
FailParams.UNDERSCORE_YIELDED,
60+
FailParams.ASSIGNMENT_TO_UNDERSCORE_IN_YIELD
61+
)
62+
};
63+
}
64+
return new Object[0];
65+
});
66+
}
67+
68+
@Test
69+
public void testUnderscoreAsParameterNameInLambda_ShouldCompileFine() throws Exception {
70+
var code = """
71+
import java.util.function.*;
72+
\s
73+
public class Test {
74+
public static void main(String[] args) {
75+
Consumer<Object> result = switch (1) {
76+
case 1 -> {
77+
yield _ -> {};
78+
}
79+
default -> null;
80+
};
81+
}
82+
}
83+
""";
84+
tb.writeJavaFiles(ROOT, code);
85+
new toolbox.JavacTask(tb)
86+
.files(tb.findJavaFiles(ROOT))
87+
.run(Task.Expect.SUCCESS);
88+
}
89+
90+
public record FailParams(String code, List<String> expectedDiagnosticMessage) {
91+
public static FailParams UNDERSCORE_YIELDED = new FailParams(
92+
"""
93+
public class Test {
94+
public static void main(String[] args) {
95+
Object result = switch (1) {
96+
case 1 -> {
97+
yield _;
98+
}
99+
default -> null;
100+
};
101+
}
102+
}
103+
""",
104+
List.of("Test.java:5:23: compiler.err.use.of.underscore.not.allowed.non.variable", "1 error")
105+
);
106+
107+
public static FailParams ASSIGNMENT_TO_UNDERSCORE_IN_YIELD = new FailParams(
108+
"""
109+
public class Test {
110+
public static void main(String[] args) {
111+
Object result = switch (1) {
112+
case 1 -> {
113+
yield _ = 1;
114+
}
115+
default -> null;
116+
};
117+
}
118+
}
119+
""",
120+
List.of("Test.java:5:23: compiler.err.use.of.underscore.not.allowed.non.variable", "1 error")
121+
);
122+
}
123+
124+
@Test
125+
public void testUnderscoreAsParameterNameInLambda_ShouldFailToCompile(List<FailParams> params) throws Exception {
126+
for (var param : params) {
127+
tb.writeJavaFiles(ROOT, param.code);
128+
Task.Result result = new JavacTask(tb)
129+
.options("-XDrawDiagnostics")
130+
.files(tb.findJavaFiles(ROOT))
131+
.run(Task.Expect.FAIL);
132+
tb.checkEqual(param.expectedDiagnosticMessage, result.getOutputLines(Task.OutputKind.DIRECT));
133+
}
134+
}
135+
136+
}

0 commit comments

Comments
 (0)