Skip to content

Commit 089f098

Browse files
Dan Rubelcommit-bot@chromium.org
authored andcommitted
Auto generate more analyzer error codes
* MISSING_CATCH_OR_FINALLY * STACK_OVERFLOW * INVALID_OPERATOR_FOR_SUPER * VAR_AND_TYPE Change-Id: Ib308cd979f9c5644c28ab1f927fa9cc8add38269 Reviewed-on: https://dart-review.googlesource.com/c/86462 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Dan Rubel <[email protected]>
1 parent fb471c6 commit 089f098

File tree

9 files changed

+81
-48
lines changed

9 files changed

+81
-48
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ const List<ErrorCode> errorCodeValues = const [
439439
ParserErrorCode.INVALID_LITERAL_IN_CONFIGURATION,
440440
ParserErrorCode.INVALID_OPERATOR,
441441
ParserErrorCode.INVALID_OPERATOR_FOR_SUPER,
442+
ParserErrorCode.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER,
442443
ParserErrorCode.INVALID_STAR_AFTER_ASYNC,
443444
ParserErrorCode.INVALID_SYNC,
444445
ParserErrorCode.INVALID_UNICODE_ESCAPE,

pkg/analyzer/lib/src/dart/error/syntactic_errors.dart

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,17 @@ class ParserErrorCode extends ErrorCode {
359359
/**
360360
* Parameters:
361361
* 0: the operator being applied to 'super'
362+
*
363+
* Only generated by the old parser.
364+
* Replaced by INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER.
362365
*/
363366
static const ParserErrorCode INVALID_OPERATOR_FOR_SUPER =
364367
const ParserErrorCode('INVALID_OPERATOR_FOR_SUPER',
365368
"The operator '{0}' can't be used with 'super'.");
366369

370+
static const ParserErrorCode INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER =
371+
_INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER;
372+
367373
static const ParserErrorCode INVALID_STAR_AFTER_ASYNC = const ParserErrorCode(
368374
'INVALID_STAR_AFTER_ASYNC',
369375
"The modifier 'async*' isn't allowed for an expression function body.",
@@ -390,11 +396,8 @@ class ParserErrorCode extends ErrorCode {
390396
static const ParserErrorCode MISSING_ASSIGNMENT_IN_INITIALIZER =
391397
_MISSING_ASSIGNMENT_IN_INITIALIZER;
392398

393-
static const ParserErrorCode MISSING_CATCH_OR_FINALLY = const ParserErrorCode(
394-
'MISSING_CATCH_OR_FINALLY',
395-
"A try statement must have either a catch or finally clause.",
396-
correction: "Try adding either a catch or finally clause, or "
397-
"remove the try statement.");
399+
static const ParserErrorCode MISSING_CATCH_OR_FINALLY =
400+
_MISSING_CATCH_OR_FINALLY;
398401

399402
/// TODO(danrubel): Consider splitting this into two separate error messages.
400403
static const ParserErrorCode MISSING_CLASS_BODY = const ParserErrorCode(
@@ -642,10 +645,7 @@ class ParserErrorCode extends ErrorCode {
642645
"Setters can't be defined within methods or functions.",
643646
correction: "Try moving the setter outside the method or function.");
644647

645-
static const ParserErrorCode STACK_OVERFLOW = const ParserErrorCode(
646-
'STACK_OVERFLOW',
647-
"The file has too many nested expressions or statements.",
648-
correction: "Try simplifying the code.");
648+
static const ParserErrorCode STACK_OVERFLOW = _STACK_OVERFLOW;
649649

650650
static const ParserErrorCode STATIC_AFTER_CONST = _STATIC_AFTER_CONST;
651651

@@ -719,10 +719,7 @@ class ParserErrorCode extends ErrorCode {
719719
"Expected '{0}' to close parameter group.",
720720
correction: "Try replacing '{0}' with '{1}'.");
721721

722-
static const ParserErrorCode VAR_AND_TYPE = const ParserErrorCode(
723-
'VAR_AND_TYPE',
724-
"Variables can't be declared using both 'var' and a type name.",
725-
correction: "Try removing the keyword 'var'.");
722+
static const ParserErrorCode VAR_AND_TYPE = _VAR_AND_TYPE;
726723

727724
static const ParserErrorCode VAR_AS_TYPE_NAME = _VAR_AS_TYPE_NAME;
728725

pkg/analyzer/lib/src/dart/error/syntactic_errors.g.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ final fastaAnalyzerErrorCodes = <ErrorCode>[
9696
_EXTERNAL_FACTORY_WITH_BODY,
9797
_EXTERNAL_CONSTRUCTOR_WITH_BODY,
9898
_FIELD_INITIALIZED_OUTSIDE_DECLARING_CLASS,
99+
_VAR_AND_TYPE,
100+
_INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER,
101+
_STACK_OVERFLOW,
102+
_MISSING_CATCH_OR_FINALLY,
99103
];
100104

101105
const ParserErrorCode _ABSTRACT_CLASS_MEMBER = const ParserErrorCode(
@@ -366,6 +370,11 @@ const ParserErrorCode _INVALID_OPERATOR = const ParserErrorCode(
366370
'INVALID_OPERATOR',
367371
r"The string '#lexeme' isn't a user-definable operator.");
368372

373+
const ParserErrorCode _INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER =
374+
const ParserErrorCode('INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER',
375+
r"The operator '?.' cannot be used with 'super' because 'super' cannot be null.",
376+
correction: "Try replacing '?.' with '.'");
377+
369378
const ParserErrorCode _INVALID_UNICODE_ESCAPE = const ParserErrorCode(
370379
'INVALID_UNICODE_ESCAPE',
371380
r"An escape sequence starting with '\u' must be followed by 4 hexadecimal digits or from 1 to 6 digits between '{' and '}'.");
@@ -386,6 +395,12 @@ const ParserErrorCode _MISSING_ASSIGNMENT_IN_INITIALIZER =
386395
r"Expected an assignment after the field name.",
387396
correction: "To initialize a field, use the syntax 'name = value'.");
388397

398+
const ParserErrorCode _MISSING_CATCH_OR_FINALLY = const ParserErrorCode(
399+
'MISSING_CATCH_OR_FINALLY',
400+
r"A try block must be followed by an 'on', 'catch', or 'finally' clause.",
401+
correction:
402+
"Try adding either a catch or finally clause, or remove the try statement.");
403+
389404
const ParserErrorCode _MISSING_CONST_FINAL_VAR_OR_TYPE = const ParserErrorCode(
390405
'MISSING_CONST_FINAL_VAR_OR_TYPE',
391406
r"Variables must be declared using the keywords 'const', 'final', 'var' or a type name.",
@@ -462,6 +477,10 @@ const ParserErrorCode _REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR =
462477
correction:
463478
"Try making this a factory constructor, or remove the redirection.");
464479

480+
const ParserErrorCode _STACK_OVERFLOW = const ParserErrorCode('STACK_OVERFLOW',
481+
r"The file has too many nested expressions or statements.",
482+
correction: "Try simplifying the code.");
483+
465484
const ParserErrorCode _STATIC_AFTER_CONST = const ParserErrorCode(
466485
'STATIC_AFTER_CONST',
467486
r"The modifier 'static' should be before the modifier 'const'.",
@@ -510,6 +529,10 @@ const ParserErrorCode _TYPE_ARGUMENTS_ON_TYPE_VARIABLE = const ParserErrorCode(
510529
r"Can't use type arguments with type variable '#name'.",
511530
correction: "Try removing the type arguments.");
512531

532+
const ParserErrorCode _VAR_AND_TYPE = const ParserErrorCode('VAR_AND_TYPE',
533+
r"Variables can't be declared using both 'var' and a type name.",
534+
correction: "Try removing 'var.'");
535+
513536
const ParserErrorCode _VAR_AS_TYPE_NAME = const ParserErrorCode(
514537
'VAR_AS_TYPE_NAME', r"The keyword 'var' can't be used as a type name.");
515538

pkg/analyzer/lib/src/fasta/error_converter.dart

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,6 @@ class FastaErrorReporter {
201201
errorReporter?.reportErrorForOffset(
202202
StrongModeCode.INVALID_SUPER_INVOCATION, offset, length);
203203
return;
204-
case "MISSING_CATCH_OR_FINALLY":
205-
errorReporter?.reportErrorForOffset(
206-
ParserErrorCode.MISSING_CATCH_OR_FINALLY, offset, length);
207-
return;
208204
case "MISSING_CLASS_BODY":
209205
errorReporter?.reportErrorForOffset(
210206
ParserErrorCode.MISSING_CLASS_BODY, offset, length);
@@ -282,10 +278,6 @@ class FastaErrorReporter {
282278
// involved in this error... either async* or sync*
283279
['async*']);
284280
return;
285-
case "STACK_OVERFLOW":
286-
errorReporter?.reportErrorForOffset(
287-
ParserErrorCode.STACK_OVERFLOW, offset, length);
288-
return;
289281
case "SUPER_IN_REDIRECTING_CONSTRUCTOR":
290282
errorReporter?.reportErrorForOffset(
291283
CompileTimeErrorCode.SUPER_IN_REDIRECTING_CONSTRUCTOR,
@@ -328,10 +320,6 @@ class FastaErrorReporter {
328320
errorReporter?.reportErrorForOffset(
329321
ScannerErrorCode.UNTERMINATED_STRING_LITERAL, offset, length);
330322
return;
331-
case "VAR_AND_TYPE":
332-
errorReporter?.reportErrorForOffset(
333-
ParserErrorCode.VAR_AND_TYPE, offset, length);
334-
return;
335323
case "WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER":
336324
errorReporter?.reportErrorForOffset(
337325
CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER,

pkg/analyzer/test/generated/parser_fasta_test.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ class ErrorParserTest_Fasta extends FastaParserTestCase
144144
void test_invalidOperatorAfterSuper_constructorInitializer2() {
145145
parseCompilationUnit('class C { C() : super?.namedConstructor(); }',
146146
errors: [
147-
expectedError(ParserErrorCode.INVALID_OPERATOR_FOR_SUPER, 21, 2)
147+
expectedError(
148+
ParserErrorCode.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER,
149+
21,
150+
2)
148151
]);
149152
}
150153

pkg/analyzer/test/generated/parser_test.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4004,13 +4004,24 @@ class Wrong<T> {
40044004
void test_invalidOperatorAfterSuper_assignableExpression() {
40054005
Expression expression = parseAssignableExpression('super?.v', false);
40064006
expectNotNullIfNoErrors(expression);
4007-
listener.assertErrors(
4008-
[expectedError(ParserErrorCode.INVALID_OPERATOR_FOR_SUPER, 5, 2)]);
4007+
listener.assertErrors([
4008+
expectedError(
4009+
usingFastaParser
4010+
? ParserErrorCode.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
4011+
: ParserErrorCode.INVALID_OPERATOR_FOR_SUPER,
4012+
5,
4013+
2)
4014+
]);
40094015
}
40104016

40114017
void test_invalidOperatorAfterSuper_primaryExpression() {
40124018
Expression expression = parseExpression('super?.v', errors: [
4013-
expectedError(ParserErrorCode.INVALID_OPERATOR_FOR_SUPER, 5, 2)
4019+
expectedError(
4020+
usingFastaParser
4021+
? ParserErrorCode.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
4022+
: ParserErrorCode.INVALID_OPERATOR_FOR_SUPER,
4023+
5,
4024+
2)
40144025
]);
40154026
expectNotNullIfNoErrors(expression);
40164027
}

pkg/analyzer/test/src/fasta/recovery/partial_code/constructor_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ class ConstructorTest extends PartialCodeTest {
9797
'super_qdot',
9898
'C() : super?.',
9999
[
100-
ParserErrorCode.INVALID_OPERATOR_FOR_SUPER,
100+
ParserErrorCode.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER,
101101
ParserErrorCode.EXPECTED_TOKEN,
102102
ParserErrorCode.MISSING_FUNCTION_BODY,
103103
],
104104
'C() : super?._s_() {}',
105105
expectedErrorsInValidCode: [
106-
ParserErrorCode.INVALID_OPERATOR_FOR_SUPER
106+
ParserErrorCode.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
107107
],
108108
failing: ['methodNonVoid', 'getter', 'setter'],
109109
),

pkg/front_end/lib/src/fasta/fasta_codes_generated.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6702,10 +6702,11 @@ const Code<Null> codeOnlyTry = messageOnlyTry;
67026702

67036703
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
67046704
const MessageCode messageOnlyTry = const MessageCode("OnlyTry",
6705-
analyzerCodes: <String>["MISSING_CATCH_OR_FINALLY"],
6705+
index: 92,
67066706
message:
6707-
r"""Try block should be followed by 'on', 'catch', or 'finally' block.""",
6708-
tip: r"""Did you forget to add a 'finally' block?""");
6707+
r"""A try block must be followed by an 'on', 'catch', or 'finally' clause.""",
6708+
tip:
6709+
r"""Try adding either a catch or finally clause, or remove the try statement.""");
67096710

67106711
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
67116712
const Template<
@@ -7986,7 +7987,9 @@ const Code<Null> codeStackOverflow = messageStackOverflow;
79867987

79877988
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
79887989
const MessageCode messageStackOverflow = const MessageCode("StackOverflow",
7989-
analyzerCodes: <String>["STACK_OVERFLOW"], message: r"""Stack overflow.""");
7990+
index: 91,
7991+
message: r"""The file has too many nested expressions or statements.""",
7992+
tip: r"""Try simplifying the code.""");
79907993

79917994
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
79927995
const Code<Null> codeStaticAfterConst = messageStaticAfterConst;
@@ -8072,8 +8075,9 @@ const Code<Null> codeSuperNullAware = messageSuperNullAware;
80728075

80738076
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
80748077
const MessageCode messageSuperNullAware = const MessageCode("SuperNullAware",
8075-
analyzerCodes: <String>["INVALID_OPERATOR_FOR_SUPER"],
8076-
message: r"""'super' can't be null.""",
8078+
index: 90,
8079+
message:
8080+
r"""The operator '?.' cannot be used with 'super' because 'super' cannot be null.""",
80778081
tip: r"""Try replacing '?.' with '.'""");
80788082

80798083
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
@@ -8496,8 +8500,9 @@ const Code<Null> codeTypeAfterVar = messageTypeAfterVar;
84968500

84978501
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
84988502
const MessageCode messageTypeAfterVar = const MessageCode("TypeAfterVar",
8499-
analyzerCodes: <String>["VAR_AND_TYPE"],
8500-
message: r"""Can't have both a type and 'var'.""",
8503+
index: 89,
8504+
message:
8505+
r"""Variables can't be declared using both 'var' and a type name.""",
85018506
tip: r"""Try removing 'var.'""");
85028507

85038508
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.

pkg/front_end/messages.yaml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,10 @@ RequiredParameterWithDefault:
10011001
}
10021002
10031003
StackOverflow:
1004-
template: "Stack overflow."
1005-
analyzerCode: STACK_OVERFLOW
1004+
index: 91
1005+
template: "The file has too many nested expressions or statements."
1006+
tip: "Try simplifying the code."
1007+
analyzerCode: ParserErrorCode.STACK_OVERFLOW
10061008

10071009
InvalidCodePoint:
10081010
template: "The escape sequence starting with '\\u' isn't a valid code point."
@@ -1182,15 +1184,17 @@ YieldNotGenerator:
11821184
analyzerCode: YIELD_IN_NON_GENERATOR
11831185

11841186
OnlyTry:
1185-
template: "Try block should be followed by 'on', 'catch', or 'finally' block."
1186-
tip: "Did you forget to add a 'finally' block?"
1187-
analyzerCode: MISSING_CATCH_OR_FINALLY
1187+
index: 92
1188+
template: "A try block must be followed by an 'on', 'catch', or 'finally' clause."
1189+
tip: "Try adding either a catch or finally clause, or remove the try statement."
1190+
analyzerCode: ParserErrorCode.MISSING_CATCH_OR_FINALLY
11881191
statement: "try {}"
11891192

11901193
TypeAfterVar:
1191-
template: "Can't have both a type and 'var'."
1194+
index: 89
1195+
template: "Variables can't be declared using both 'var' and a type name."
11921196
tip: "Try removing 'var.'"
1193-
analyzerCode: VAR_AND_TYPE
1197+
analyzerCode: ParserErrorCode.VAR_AND_TYPE
11941198

11951199
AssertExtraneousArgument:
11961200
template: "`assert` can't have more than two arguments."
@@ -1248,9 +1252,10 @@ CatchSyntaxExtraParameters:
12481252
- "try {} catch (e, s, x) {}"
12491253

12501254
SuperNullAware:
1251-
template: "'super' can't be null."
1255+
index: 90
1256+
template: "The operator '?.' cannot be used with 'super' because 'super' cannot be null."
12521257
tip: "Try replacing '?.' with '.'"
1253-
analyzerCode: INVALID_OPERATOR_FOR_SUPER
1258+
analyzerCode: ParserErrorCode.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
12541259

12551260
ConstFieldWithoutInitializer:
12561261
template: "The const variable '#name' must be initialized."

0 commit comments

Comments
 (0)