Skip to content

Commit 60db5f2

Browse files
author
Vicente Romero
committed
8294020: improve errors for record declarations
Reviewed-by: jlahoda
1 parent 520db1e commit 60db5f2

File tree

6 files changed

+32
-30
lines changed

6 files changed

+32
-30
lines changed

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

+14-23
Original file line numberDiff line numberDiff line change
@@ -3925,29 +3925,23 @@ protected JCStatement classOrRecordOrInterfaceOrEnumDeclaration(JCModifiers mods
39253925
} else {
39263926
int pos = token.pos;
39273927
List<JCTree> errs;
3928-
if (token.kind == IDENTIFIER && token.name() == names.record) {
3929-
checkSourceLevel(Feature.RECORDS);
3930-
JCErroneous erroneousTree = syntaxError(token.pos, List.of(mods), Errors.RecordHeaderExpected);
3931-
return toP(F.Exec(erroneousTree));
3928+
if (LAX_IDENTIFIER.test(token.kind)) {
3929+
errs = List.of(mods, toP(F.at(pos).Ident(ident())));
3930+
setErrorEndPos(token.pos);
39323931
} else {
3933-
if (LAX_IDENTIFIER.test(token.kind)) {
3934-
errs = List.of(mods, toP(F.at(pos).Ident(ident())));
3935-
setErrorEndPos(token.pos);
3936-
} else {
3937-
errs = List.of(mods);
3938-
}
3939-
final JCErroneous erroneousTree;
3940-
if (parseModuleInfo) {
3941-
erroneousTree = syntaxError(pos, errs, Errors.ExpectedModuleOrOpen);
3932+
errs = List.of(mods);
3933+
}
3934+
final JCErroneous erroneousTree;
3935+
if (parseModuleInfo) {
3936+
erroneousTree = syntaxError(pos, errs, Errors.ExpectedModuleOrOpen);
3937+
} else {
3938+
if (allowRecords) {
3939+
erroneousTree = syntaxError(pos, errs, Errors.Expected4(CLASS, INTERFACE, ENUM, "record"));
39423940
} else {
3943-
if (allowRecords) {
3944-
erroneousTree = syntaxError(pos, errs, Errors.Expected4(CLASS, INTERFACE, ENUM, "record"));
3945-
} else {
3946-
erroneousTree = syntaxError(pos, errs, Errors.Expected3(CLASS, INTERFACE, ENUM));
3947-
}
3941+
erroneousTree = syntaxError(pos, errs, Errors.Expected3(CLASS, INTERFACE, ENUM));
39483942
}
3949-
return toP(F.Exec(erroneousTree));
39503943
}
3944+
return toP(F.Exec(erroneousTree));
39513945
}
39523946
}
39533947

@@ -4426,10 +4420,7 @@ else if (annosAfterParams.nonEmpty())
44264420
}
44274421

44284422
protected boolean isRecordStart() {
4429-
if (token.kind == IDENTIFIER && token.name() == names.record &&
4430-
(peekToken(TokenKind.IDENTIFIER, TokenKind.LPAREN) ||
4431-
peekToken(TokenKind.IDENTIFIER, TokenKind.EOF) ||
4432-
peekToken(TokenKind.IDENTIFIER, TokenKind.LT))) {
4423+
if (token.kind == IDENTIFIER && token.name() == names.record && peekToken(TokenKind.IDENTIFIER)) {
44334424
checkSourceLevel(Feature.RECORDS);
44344425
return true;
44354426
} else {

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

-3
Original file line numberDiff line numberDiff line change
@@ -3774,9 +3774,6 @@ compiler.err.instance.initializer.not.allowed.in.records=\
37743774
compiler.err.static.declaration.not.allowed.in.inner.classes=\
37753775
static declarations not allowed in inner classes
37763776

3777-
compiler.err.record.header.expected=\
3778-
record header expected
3779-
37803777
############################################
37813778
# messages previously at javac.properties
37823779

test/langtools/tools/javac/diags/examples/IncorrectRecordDeclaration.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* questions.
2222
*/
2323

24-
// key: compiler.err.record.header.expected
24+
// key: compiler.err.expected
25+
// key: compiler.err.illegal.start.of.type
2526

2627
record R {}

test/langtools/tools/javac/records/RecordCompilationTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,7 @@
2525
* RecordCompilationTests
2626
*
2727
* @test
28-
* @bug 8250629 8252307 8247352 8241151 8246774 8259025 8288130 8282714 8289647
28+
* @bug 8250629 8252307 8247352 8241151 8246774 8259025 8288130 8282714 8289647 8294020
2929
* @summary Negative compilation tests, and positive compilation (smoke) tests for records
3030
* @library /lib/combo /tools/lib /tools/javac/lib
3131
* @modules
@@ -149,7 +149,7 @@ public void testMalformedDeclarations() {
149149
assertFail("compiler.err.expected", "record R();");
150150
assertFail("compiler.err.illegal.start.of.type", "record R(,) { }");
151151
assertFail("compiler.err.illegal.start.of.type", "record R((int x)) { }");
152-
assertFail("compiler.err.record.header.expected", "record R { }");
152+
assertFail("compiler.err.expected", "record R { }");
153153
assertFail("compiler.err.expected", "record R(foo) { }");
154154
assertFail("compiler.err.expected", "record R(int int) { }");
155155
assertFail("compiler.err.mod.not.allowed.here", "abstract record R(String foo) { }");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @bug 8294020
4+
* @summary improve error position for records without header
5+
* @compile/fail/ref=RecordDeclarationSyntaxTest.out -XDrawDiagnostics RecordDeclarationSyntaxTest.java
6+
*/
7+
8+
class RecordDeclarationSyntaxTest {
9+
record R {} // no header
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RecordDeclarationSyntaxTest.java:9:13: compiler.err.expected: '('
2+
RecordDeclarationSyntaxTest.java:9:14: compiler.err.illegal.start.of.type
3+
2 errors

0 commit comments

Comments
 (0)