Skip to content

Commit abf4f3d

Browse files
committed
8370865: Incorrect parser error for compact source files and multi-variable declarations
Reviewed-by: vromero
1 parent 15fd529 commit abf4f3d

File tree

3 files changed

+155
-2
lines changed

3 files changed

+155
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4933,7 +4933,8 @@ private List<JCTree> topLevelMethodOrFieldDeclaration(JCModifiers mods, Comment
49334933
}
49344934

49354935
// Field
4936-
if (!isVoid && typarams.isEmpty() && (token.kind == EQ || token.kind == SEMI)) {
4936+
if (!isVoid && typarams.isEmpty() &&
4937+
(token.kind == EQ || token.kind == SEMI || token.kind == COMMA)) {
49374938
List<JCTree> defs =
49384939
variableDeclaratorsRest(pos, mods, type, name, false, dc,
49394940
new ListBuffer<JCTree>(), false).toList();
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2025, 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 8370865
27+
* @summary Check that multiple comma-separated fields work
28+
* @library /tools/lib
29+
* @modules jdk.compiler/com.sun.tools.javac.api
30+
* jdk.compiler/com.sun.tools.javac.main
31+
* jdk.compiler/com.sun.tools.javac.util
32+
* @build toolbox.ToolBox toolbox.JavacTask
33+
* @run main MultipleFields
34+
*/
35+
36+
import java.nio.file.Files;
37+
import java.nio.file.Path;
38+
import java.nio.file.Paths;
39+
import java.util.List;
40+
import java.util.Objects;
41+
42+
import toolbox.TestRunner;
43+
import toolbox.JavacTask;
44+
import toolbox.JavaTask;
45+
import toolbox.Task;
46+
import toolbox.ToolBox;
47+
48+
public class MultipleFields extends TestRunner {
49+
50+
private ToolBox tb;
51+
52+
public static void main(String... args) throws Exception {
53+
new MultipleFields().runTests();
54+
}
55+
56+
MultipleFields() {
57+
super(System.err);
58+
tb = new ToolBox();
59+
}
60+
61+
public void runTests() throws Exception {
62+
runTests(m -> new Object[] { Paths.get(m.getName()) });
63+
}
64+
65+
@Test
66+
public void testWithExplicitImport(Path base) throws Exception {
67+
Path current = base.resolve(".");
68+
Path src = current.resolve("src");
69+
Path classes = current.resolve("classes");
70+
tb.writeFile(src.resolve("Test.java"),
71+
"""
72+
int f1, f2;
73+
74+
public void main() {
75+
f1 = 1;
76+
f2 = f1 + 1;
77+
System.out.println("field: " + f1 + "/" + f2);
78+
}
79+
""");
80+
81+
Files.createDirectories(classes);
82+
83+
new JavacTask(tb)
84+
.outdir(classes)
85+
.files(tb.findJavaFiles(src))
86+
.run(Task.Expect.SUCCESS)
87+
.writeAll();
88+
89+
var out = new JavaTask(tb)
90+
.classpath(classes.toString())
91+
.className("Test")
92+
.run()
93+
.writeAll()
94+
.getOutputLines(Task.OutputKind.STDOUT);
95+
96+
var expectedOut = List.of("field: 1/2");
97+
98+
if (!Objects.equals(expectedOut, out)) {
99+
throw new AssertionError("Incorrect Output, expected: " + expectedOut +
100+
", actual: " + out);
101+
102+
}
103+
}
104+
}

test/langtools/tools/javac/parser/JavacParserTest.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8310326 8312093 8312204 8315452 8337976 8324859 8344706 8351260
26+
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8310326 8312093 8312204 8315452 8337976 8324859 8344706 8351260 8370865
2727
* @summary tests error and diagnostics positions
2828
* @author Jan Lahoda
2929
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -3076,6 +3076,54 @@ void testVeryBrokenTypeWithAnnotationsMinimal() throws IOException {
30763076
ct.parse().iterator().next();
30773077
}
30783078

3079+
@Test //JDK-8370865
3080+
void testCompactSourceFileMultiField1() throws IOException {
3081+
String code = """
3082+
int i, j, k;
3083+
void main() {}
3084+
""";
3085+
DiagnosticCollector<JavaFileObject> coll =
3086+
new DiagnosticCollector<>();
3087+
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll,
3088+
List.of("-XDrawDiagnostics"),
3089+
null, Arrays.asList(new MyFileObject(code)));
3090+
//no exceptions:
3091+
ct.parse().iterator().next();
3092+
List<String> codes = new LinkedList<>();
3093+
3094+
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
3095+
codes.add(d.getLineNumber() + ":" + d.getColumnNumber() + ":" + d.getCode());
3096+
}
3097+
3098+
assertEquals("testCompactSourceFileMultiField1: " + codes,
3099+
List.of(),
3100+
codes);
3101+
}
3102+
3103+
@Test //JDK-8370865
3104+
void testCompactSourceFileMultiField2() throws IOException {
3105+
String code = """
3106+
int i, j = 0, k = 0;
3107+
void main() {}
3108+
""";
3109+
DiagnosticCollector<JavaFileObject> coll =
3110+
new DiagnosticCollector<>();
3111+
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll,
3112+
List.of("-XDrawDiagnostics"),
3113+
null, Arrays.asList(new MyFileObject(code)));
3114+
//no exceptions:
3115+
ct.parse().iterator().next();
3116+
List<String> codes = new LinkedList<>();
3117+
3118+
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
3119+
codes.add(d.getLineNumber() + ":" + d.getColumnNumber() + ":" + d.getCode());
3120+
}
3121+
3122+
assertEquals("testCompactSourceFileMultiField2: " + codes,
3123+
List.of(),
3124+
codes);
3125+
}
3126+
30793127
void run(String[] args) throws Exception {
30803128
int passed = 0, failed = 0;
30813129
final Pattern p = (args != null && args.length > 0)

0 commit comments

Comments
 (0)