From 62cc07dbca3bbfc42e99b821d9ea512f0187bcd0 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Thu, 18 Jun 2020 21:22:42 +0000 Subject: [PATCH] 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 Reviewed-by: Brian Wilkerson --- .../lib/src/generated/error_verifier.dart | 6 ++-- .../invalid_null_aware_operator_test.dart | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index 3292789e4f1..17069dad16c 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -5395,12 +5395,10 @@ class ErrorVerifier extends RecursiveAstVisitor { 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; diff --git a/pkg/analyzer/test/src/diagnostics/invalid_null_aware_operator_test.dart b/pkg/analyzer/test/src/diagnostics/invalid_null_aware_operator_test.dart index 1d72a87c580..d36cb8fc789 100644 --- a/pkg/analyzer/test/src/diagnostics/invalid_null_aware_operator_test.dart +++ b/pkg/analyzer/test/src/diagnostics/invalid_null_aware_operator_test.dart @@ -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), + ]); + } }