Improve parser recover to fix more code completion tests
Change-Id: I7bb03e18043b4951da7b63ffc146b6e7486f7e42
Reviewed-on: https://dart-review.googlesource.com/75601
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Dan Rubel <danrubel@google.com>
diff --git a/pkg/analysis_server/test/domain_completion_test.dart b/pkg/analysis_server/test/domain_completion_test.dart
index f8d4a77..5a877b3 100644
--- a/pkg/analysis_server/test/domain_completion_test.dart
+++ b/pkg/analysis_server/test/domain_completion_test.dart
@@ -189,7 +189,6 @@
relevance: DART_RELEVANCE_LOCAL_FIELD);
}
- @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/33992')
test_constructor2() async {
addTestFile('class A {bool foo; A() : s^;}');
await getSuggestions();
@@ -208,7 +207,6 @@
relevance: DART_RELEVANCE_LOCAL_FIELD);
}
- @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/33992')
test_constructor4() async {
addTestFile('class A {bool foo; A() : a=7,s^;}');
await getSuggestions();
@@ -218,7 +216,6 @@
relevance: DART_RELEVANCE_LOCAL_FIELD);
}
- @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/33992')
test_constructor5() async {
addTestFile('class A {bool foo; A() : a=7,s^}');
await getSuggestions();
diff --git a/pkg/analyzer/test/src/fasta/recovery/partial_code/constructor_test.dart b/pkg/analyzer/test/src/fasta/recovery/partial_code/constructor_test.dart
index 63bb9aa..3f834fd 100644
--- a/pkg/analyzer/test/src/fasta/recovery/partial_code/constructor_test.dart
+++ b/pkg/analyzer/test/src/fasta/recovery/partial_code/constructor_test.dart
@@ -31,7 +31,17 @@
ParserErrorCode.MISSING_ASSIGNMENT_IN_INITIALIZER,
ParserErrorCode.MISSING_FUNCTION_BODY
],
- 'C() : _s_ = f {}',
+ 'C() : f = _s_ {}',
+ adjustValidUnitBeforeComparison: setSeparator,
+ ),
+ new TestDescriptor(
+ 'colon_field_increment',
+ 'C() : f++',
+ [
+ ParserErrorCode.MISSING_ASSIGNMENT_IN_INITIALIZER,
+ ParserErrorCode.MISSING_FUNCTION_BODY
+ ],
+ 'C() : _s_ = f++ {}',
adjustValidUnitBeforeComparison: setSeparator,
),
new TestDescriptor(
diff --git a/pkg/front_end/lib/src/fasta/parser/parser.dart b/pkg/front_end/lib/src/fasta/parser/parser.dart
index dd3f1ce..0ffe218 100644
--- a/pkg/front_end/lib/src/fasta/parser/parser.dart
+++ b/pkg/front_end/lib/src/fasta/parser/parser.dart
@@ -2430,10 +2430,21 @@
}
// Fall through to recovery
} else if (next.isIdentifier) {
- if (optional('=', next.next)) {
+ Token next2 = next.next;
+ if (optional('=', next2)) {
return parseInitializerExpressionRest(token);
}
- // Fall through to recovery
+ // Recovery: If this looks like an expression,
+ // then fall through to insert the LHS and `=` of the assignment,
+ // otherwise insert an `=` and synthetic identifier.
+ if (!next2.isOperator && !optional('.', next2)) {
+ token = rewriter.insertToken(
+ next, new SyntheticToken(TokenType.EQ, next2.offset));
+ token = insertSyntheticIdentifier(token, IdentifierContext.expression,
+ message: fasta.messageMissingAssignmentInInitializer,
+ messageOnToken: next);
+ return parseInitializerExpressionRest(beforeExpression);
+ }
} else {
// Recovery: Insert a synthetic assignment.
token = insertSyntheticIdentifier(
@@ -2444,9 +2455,10 @@
token = rewriter.insertSyntheticIdentifier(token);
return parseInitializerExpressionRest(beforeExpression);
}
- // Recovery
- // Insert a sythetic assignment to ensure that the expression is indeed
- // an assignment. Failing to do so causes this test to fail:
+ // Recovery:
+ // Insert a synthetic identifier and assignment operator
+ // to ensure that the expression is indeed an assignment.
+ // Failing to do so causes this test to fail:
// pkg/front_end/testcases/regress/issue_31192.dart
// TODO(danrubel): Investigate better recovery.
token = insertSyntheticIdentifier(