Issue 42385. Fix for analyzing 'prefix?.foo'.

Bug: https://github.com/dart-lang/sdk/issues/42385
Change-Id: I4dabfbda01a37210c57cb5ba6b5a7d530752e029
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151628
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Konstantin Shcheglov
2020-06-18 21:22:42 +00:00
committed by commit-bot@chromium.org
parent 1596bf3cd4
commit 62cc07dbca
2 changed files with 38 additions and 4 deletions
@@ -5395,12 +5395,10 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
return false;
}
// For `C?.foo()` the type of `C` is not set, it is not an expression.
// For `foo?.bar`, `foo` must be an identifier with a value.
if (node is Identifier) {
var element = node.staticElement;
if (element is ClassElement || element is ExtensionElement) {
return false;
}
return element is PropertyAccessorElement || element is VariableElement;
}
return true;
@@ -97,6 +97,24 @@ f(int? x) {
''');
}
/// Here we test that analysis does not crash while checking whether to
/// report [StaticWarningCode.INVALID_NULL_AWARE_OPERATOR]. But we also
/// report another error.
test_getter_prefix() async {
newFile('/test/lib/a.dart', content: r'''
int x = 0;
''');
await assertErrorsInCode('''
import 'a.dart' as p;
f() {
p?.x;
}
''', [
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 31, 1),
]);
}
test_index_legacy() async {
newFile('/test/lib/a.dart', content: r'''
// @dart = 2.5
@@ -287,4 +305,22 @@ f() {
}
''');
}
/// Here we test that analysis does not crash while checking whether to
/// report [StaticWarningCode.INVALID_NULL_AWARE_OPERATOR]. But we also
/// report another error.
test_setter_prefix() async {
newFile('/test/lib/a.dart', content: r'''
int x = 0;
''');
await assertErrorsInCode('''
import 'a.dart' as p;
f() {
p?.x = 0;
}
''', [
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 31, 1),
]);
}
}