improve missing body error messages
This builds on the prior CL to improve the error messages
for missing switch statement body, missing finally clause body,
and others.
Change-Id: Ia2def1f3a75d5581356c12aee4705ef21870f749
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103571
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Dan Rubel <danrubel@google.com>
diff --git a/pkg/analyzer/test/generated/parser_test.dart b/pkg/analyzer/test/generated/parser_test.dart
index 17c272f..857a19d 100644
--- a/pkg/analyzer/test/generated/parser_test.dart
+++ b/pkg/analyzer/test/generated/parser_test.dart
@@ -5329,7 +5329,7 @@
parseStatement('switch (a) return;', expectedEndOffset: 11);
expect(statement, isNotNull);
listener.assertErrors(usingFastaParser
- ? [expectedError(ParserErrorCode.EXPECTED_TOKEN, 11, 6)]
+ ? [expectedError(ParserErrorCode.EXPECTED_BODY, 9, 1)]
: [
expectedError(ParserErrorCode.EXPECTED_TOKEN, 11, 6),
expectedError(ParserErrorCode.EXPECTED_CASE_OR_DEFAULT, 11, 6),
diff --git a/pkg/analyzer/test/src/fasta/recovery/partial_code/switch_statement_test.dart b/pkg/analyzer/test/src/fasta/recovery/partial_code/switch_statement_test.dart
index f8a05c4..914e1f6 100644
--- a/pkg/analyzer/test/src/fasta/recovery/partial_code/switch_statement_test.dart
+++ b/pkg/analyzer/test/src/fasta/recovery/partial_code/switch_statement_test.dart
@@ -22,7 +22,7 @@
'switch',
[
ParserErrorCode.MISSING_IDENTIFIER,
- ParserErrorCode.EXPECTED_TOKEN,
+ ParserErrorCode.EXPECTED_BODY,
ParserErrorCode.EXPECTED_TOKEN
],
"switch (_s_) {}",
@@ -32,7 +32,7 @@
'switch (',
[
ParserErrorCode.MISSING_IDENTIFIER,
- ParserErrorCode.EXPECTED_TOKEN,
+ ParserErrorCode.EXPECTED_BODY,
ScannerErrorCode.EXPECTED_TOKEN
],
"switch (_s_) {}",
@@ -47,11 +47,11 @@
new TestDescriptor(
'expression',
'switch (a',
- [ParserErrorCode.EXPECTED_TOKEN, ScannerErrorCode.EXPECTED_TOKEN],
+ [ParserErrorCode.EXPECTED_BODY, ScannerErrorCode.EXPECTED_TOKEN],
"switch (a) {}",
failing: ['block']),
new TestDescriptor('rightParen', 'switch (a)',
- [ParserErrorCode.EXPECTED_TOKEN], "switch (a) {}",
+ [ParserErrorCode.EXPECTED_BODY], "switch (a) {}",
failing: ['block']),
new TestDescriptor('leftBrace', 'switch (a) {',
[ScannerErrorCode.EXPECTED_TOKEN], "switch (a) {}",
diff --git a/pkg/analyzer/test/src/fasta/recovery/partial_code/try_statement_test.dart b/pkg/analyzer/test/src/fasta/recovery/partial_code/try_statement_test.dart
index ca80f4c..1e2f8a5 100644
--- a/pkg/analyzer/test/src/fasta/recovery/partial_code/try_statement_test.dart
+++ b/pkg/analyzer/test/src/fasta/recovery/partial_code/try_statement_test.dart
@@ -22,7 +22,7 @@
'keyword',
'try',
[
- ParserErrorCode.EXPECTED_TOKEN,
+ ParserErrorCode.EXPECTED_BODY,
ParserErrorCode.MISSING_CATCH_OR_FINALLY
],
"try {} finally {}",
@@ -38,7 +38,7 @@
'try {} on',
[
ParserErrorCode.EXPECTED_TYPE_NAME,
- ParserErrorCode.EXPECTED_TOKEN
+ ParserErrorCode.EXPECTED_BODY
],
"try {} on _s_ {}",
failing: [
@@ -48,7 +48,7 @@
'localFunctionVoid'
]),
new TestDescriptor('on_identifier', 'try {} on A',
- [ParserErrorCode.EXPECTED_TOKEN], "try {} on A {}",
+ [ParserErrorCode.EXPECTED_BODY], "try {} on A {}",
failing: ['block']),
//
// Single catch clause.
@@ -56,7 +56,7 @@
new TestDescriptor(
'catch',
'try {} catch',
- [ParserErrorCode.CATCH_SYNTAX, ParserErrorCode.EXPECTED_TOKEN],
+ [ParserErrorCode.CATCH_SYNTAX, ParserErrorCode.EXPECTED_BODY],
"try {} catch (e) {}",
failing: ['block']),
new TestDescriptor(
@@ -65,7 +65,7 @@
[
ScannerErrorCode.EXPECTED_TOKEN,
ParserErrorCode.CATCH_SYNTAX,
- ParserErrorCode.EXPECTED_TOKEN
+ ParserErrorCode.EXPECTED_BODY
],
"try {} catch (e) {}",
failing: ['block', 'labeled', 'localFunctionNonVoid']),
@@ -74,7 +74,7 @@
'try {} catch (e',
[
ParserErrorCode.CATCH_SYNTAX,
- ParserErrorCode.EXPECTED_TOKEN,
+ ParserErrorCode.EXPECTED_BODY,
ScannerErrorCode.EXPECTED_TOKEN
],
"try {} catch (e) {}",
@@ -84,7 +84,7 @@
'try {} catch (e, ',
[
ParserErrorCode.CATCH_SYNTAX,
- ParserErrorCode.EXPECTED_TOKEN,
+ ParserErrorCode.EXPECTED_BODY,
ScannerErrorCode.EXPECTED_TOKEN
],
"try {} catch (e, _s_) {}",
@@ -96,13 +96,13 @@
// TODO(danrubel): Update parser to generate CATCH_SYNTAX
// because in this situation there are not any extra parameters.
ParserErrorCode.CATCH_SYNTAX_EXTRA_PARAMETERS,
- ParserErrorCode.EXPECTED_TOKEN,
+ ParserErrorCode.EXPECTED_BODY,
ScannerErrorCode.EXPECTED_TOKEN
],
"try {} catch (e, s) {}",
failing: ['eof', 'block']),
new TestDescriptor('catch_rightParen', 'try {} catch (e, s)',
- [ParserErrorCode.EXPECTED_TOKEN], "try {} catch (e, s) {}",
+ [ParserErrorCode.EXPECTED_BODY], "try {} catch (e, s) {}",
failing: ['block']),
//
// Single catch clause after an on clause.
@@ -110,7 +110,7 @@
new TestDescriptor(
'on_catch',
'try {} on A catch',
- [ParserErrorCode.CATCH_SYNTAX, ParserErrorCode.EXPECTED_TOKEN],
+ [ParserErrorCode.CATCH_SYNTAX, ParserErrorCode.EXPECTED_BODY],
"try {} on A catch (e) {}",
failing: ['block']),
new TestDescriptor(
@@ -118,7 +118,7 @@
'try {} on A catch (',
[
ParserErrorCode.CATCH_SYNTAX,
- ParserErrorCode.EXPECTED_TOKEN,
+ ParserErrorCode.EXPECTED_BODY,
ScannerErrorCode.EXPECTED_TOKEN
],
"try {} on A catch (e) {}",
@@ -128,7 +128,7 @@
'try {} on A catch (e',
[
ParserErrorCode.CATCH_SYNTAX,
- ParserErrorCode.EXPECTED_TOKEN,
+ ParserErrorCode.EXPECTED_BODY,
ScannerErrorCode.EXPECTED_TOKEN
],
"try {} on A catch (e) {}",
@@ -138,7 +138,7 @@
'try {} on A catch (e, ',
[
ParserErrorCode.CATCH_SYNTAX,
- ParserErrorCode.EXPECTED_TOKEN,
+ ParserErrorCode.EXPECTED_BODY,
ScannerErrorCode.EXPECTED_TOKEN
],
"try {} on A catch (e, _s_) {}",
@@ -150,19 +150,19 @@
// TODO(danrubel): Update parser to generate CATCH_SYNTAX
// because in this situation there are not any extra parameters.
ParserErrorCode.CATCH_SYNTAX_EXTRA_PARAMETERS,
- ParserErrorCode.EXPECTED_TOKEN,
+ ParserErrorCode.EXPECTED_BODY,
ScannerErrorCode.EXPECTED_TOKEN
],
"try {} on A catch (e, s) {}",
failing: ['eof', 'block']),
new TestDescriptor('on_catch_rightParen', 'try {} on A catch (e, s)',
- [ParserErrorCode.EXPECTED_TOKEN], "try {} on A catch (e, s) {}",
+ [ParserErrorCode.EXPECTED_BODY], "try {} on A catch (e, s) {}",
failing: ['block']),
//
// Only a finally clause.
//
new TestDescriptor('finally_noCatch_noBlock', 'try {} finally',
- [ParserErrorCode.EXPECTED_TOKEN], "try {} finally {}",
+ [ParserErrorCode.EXPECTED_BODY], "try {} finally {}",
failing: ['block']),
//
// A catch and finally clause.
@@ -170,7 +170,7 @@
new TestDescriptor(
'finally_catch_noBlock',
'try {} catch (e) {} finally',
- [ParserErrorCode.EXPECTED_TOKEN],
+ [ParserErrorCode.EXPECTED_BODY],
"try {} catch (e) {} finally {}",
failing: ['block']),
],
diff --git a/pkg/front_end/lib/src/fasta/parser/parser.dart b/pkg/front_end/lib/src/fasta/parser/parser.dart
index 58aeff4..795412c 100644
--- a/pkg/front_end/lib/src/fasta/parser/parser.dart
+++ b/pkg/front_end/lib/src/fasta/parser/parser.dart
@@ -5789,8 +5789,7 @@
Token tryKeyword = token.next;
assert(optional('try', tryKeyword));
listener.beginTryStatement(tryKeyword);
- // TODO(danrubel): pass 'try statement' for blockKind and update tests
- Token lastConsumed = parseBlock(tryKeyword, null);
+ Token lastConsumed = parseBlock(tryKeyword, 'try statement');
token = lastConsumed.next;
int catchCount = 0;
@@ -5885,8 +5884,7 @@
token = lastConsumed.next;
}
listener.endCatchClause(token);
- // TODO(danrubel): pass 'catch clause' for blockKind and update tests
- lastConsumed = parseBlock(lastConsumed, null);
+ lastConsumed = parseBlock(lastConsumed, 'catch clause');
token = lastConsumed.next;
++catchCount;
listener.handleCatchBlock(onKeyword, catchKeyword, comma);
@@ -5896,8 +5894,7 @@
Token finallyKeyword = null;
if (optional('finally', token)) {
finallyKeyword = token;
- // TODO(danrubel): pass 'finally clause' for blockKind and update tests
- lastConsumed = parseBlock(token, null);
+ lastConsumed = parseBlock(token, 'finally clause');
token = lastConsumed.next;
listener.handleFinallyBlock(finallyKeyword);
} else {
@@ -5935,8 +5932,7 @@
/// ;
/// ```
Token parseSwitchBlock(Token token) {
- // TODO(danrubel): pass 'switch statement' for blockKind and update tests
- Token beginSwitch = token = ensureBlock(token, null, null);
+ Token beginSwitch = token = ensureBlock(token, null, 'switch statement');
listener.beginSwitchBlock(beginSwitch);
int caseCount = 0;
Token defaultKeyword = null;