8294020: improve errors for record declarations

Reviewed-by: jlahoda
This commit is contained in:
Vicente Romero 2022-11-07 20:16:04 +00:00
parent 520db1eeb1
commit 60db5f2ba2
6 changed files with 34 additions and 32 deletions

View File

@ -3925,29 +3925,23 @@ public class JavacParser implements Parser {
} else {
int pos = token.pos;
List<JCTree> errs;
if (token.kind == IDENTIFIER && token.name() == names.record) {
checkSourceLevel(Feature.RECORDS);
JCErroneous erroneousTree = syntaxError(token.pos, List.of(mods), Errors.RecordHeaderExpected);
return toP(F.Exec(erroneousTree));
if (LAX_IDENTIFIER.test(token.kind)) {
errs = List.of(mods, toP(F.at(pos).Ident(ident())));
setErrorEndPos(token.pos);
} else {
if (LAX_IDENTIFIER.test(token.kind)) {
errs = List.of(mods, toP(F.at(pos).Ident(ident())));
setErrorEndPos(token.pos);
} else {
errs = List.of(mods);
}
final JCErroneous erroneousTree;
if (parseModuleInfo) {
erroneousTree = syntaxError(pos, errs, Errors.ExpectedModuleOrOpen);
} else {
if (allowRecords) {
erroneousTree = syntaxError(pos, errs, Errors.Expected4(CLASS, INTERFACE, ENUM, "record"));
} else {
erroneousTree = syntaxError(pos, errs, Errors.Expected3(CLASS, INTERFACE, ENUM));
}
}
return toP(F.Exec(erroneousTree));
errs = List.of(mods);
}
final JCErroneous erroneousTree;
if (parseModuleInfo) {
erroneousTree = syntaxError(pos, errs, Errors.ExpectedModuleOrOpen);
} else {
if (allowRecords) {
erroneousTree = syntaxError(pos, errs, Errors.Expected4(CLASS, INTERFACE, ENUM, "record"));
} else {
erroneousTree = syntaxError(pos, errs, Errors.Expected3(CLASS, INTERFACE, ENUM));
}
}
return toP(F.Exec(erroneousTree));
}
}
@ -4426,10 +4420,7 @@ public class JavacParser implements Parser {
}
protected boolean isRecordStart() {
if (token.kind == IDENTIFIER && token.name() == names.record &&
(peekToken(TokenKind.IDENTIFIER, TokenKind.LPAREN) ||
peekToken(TokenKind.IDENTIFIER, TokenKind.EOF) ||
peekToken(TokenKind.IDENTIFIER, TokenKind.LT))) {
if (token.kind == IDENTIFIER && token.name() == names.record && peekToken(TokenKind.IDENTIFIER)) {
checkSourceLevel(Feature.RECORDS);
return true;
} else {

View File

@ -3774,9 +3774,6 @@ compiler.err.instance.initializer.not.allowed.in.records=\
compiler.err.static.declaration.not.allowed.in.inner.classes=\
static declarations not allowed in inner classes
compiler.err.record.header.expected=\
record header expected
############################################
# messages previously at javac.properties

View File

@ -21,6 +21,7 @@
* questions.
*/
// key: compiler.err.record.header.expected
// key: compiler.err.expected
// key: compiler.err.illegal.start.of.type
record R {}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, 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
@ -25,7 +25,7 @@
* RecordCompilationTests
*
* @test
* @bug 8250629 8252307 8247352 8241151 8246774 8259025 8288130 8282714 8289647
* @bug 8250629 8252307 8247352 8241151 8246774 8259025 8288130 8282714 8289647 8294020
* @summary Negative compilation tests, and positive compilation (smoke) tests for records
* @library /lib/combo /tools/lib /tools/javac/lib
* @modules
@ -149,7 +149,7 @@ public class RecordCompilationTests extends CompilationTestCase {
assertFail("compiler.err.expected", "record R();");
assertFail("compiler.err.illegal.start.of.type", "record R(,) { }");
assertFail("compiler.err.illegal.start.of.type", "record R((int x)) { }");
assertFail("compiler.err.record.header.expected", "record R { }");
assertFail("compiler.err.expected", "record R { }");
assertFail("compiler.err.expected", "record R(foo) { }");
assertFail("compiler.err.expected", "record R(int int) { }");
assertFail("compiler.err.mod.not.allowed.here", "abstract record R(String foo) { }");

View File

@ -0,0 +1,10 @@
/*
* @test /nodynamiccopyright/
* @bug 8294020
* @summary improve error position for records without header
* @compile/fail/ref=RecordDeclarationSyntaxTest.out -XDrawDiagnostics RecordDeclarationSyntaxTest.java
*/
class RecordDeclarationSyntaxTest {
record R {} // no header
}

View File

@ -0,0 +1,3 @@
RecordDeclarationSyntaxTest.java:9:13: compiler.err.expected: '('
RecordDeclarationSyntaxTest.java:9:14: compiler.err.illegal.start.of.type
2 errors