diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart index d6c7ecda02b8c..1d969028ba208 100644 --- a/pkg/analyzer/lib/src/generated/resolver.dart +++ b/pkg/analyzer/lib/src/generated/resolver.dart @@ -771,26 +771,29 @@ class ResolverVisitor extends ScopedVisitor { } void startNullAwareIndexExpression(IndexExpression node) { - if (_migratableAstInfoProvider.isIndexExpressionNullAware(node) && - _flowAnalysis != null) { - _flowAnalysis.flow.nullAwareAccess_rightBegin( - node.target, node.realTarget.staticType ?? typeProvider.dynamicType); - _unfinishedNullShorts.add(node.nullShortingTermination); + if (_migratableAstInfoProvider.isIndexExpressionNullAware(node)) { + var flow = _flowAnalysis?.flow; + if (flow != null) { + flow.nullAwareAccess_rightBegin(node.target, + node.realTarget.staticType ?? typeProvider.dynamicType); + _unfinishedNullShorts.add(node.nullShortingTermination); + } } } - void startNullAwarePropertyAccess( - PropertyAccess node, - ) { - if (_migratableAstInfoProvider.isPropertyAccessNullAware(node) && - _flowAnalysis != null) { - var target = node.target; - if (target is SimpleIdentifier && target.staticElement is ClassElement) { - // `?.` to access static methods is equivalent to `.`, so do nothing. - } else { - _flowAnalysis.flow.nullAwareAccess_rightBegin( - target, node.realTarget.staticType ?? typeProvider.dynamicType); - _unfinishedNullShorts.add(node.nullShortingTermination); + void startNullAwarePropertyAccess(PropertyAccess node) { + if (_migratableAstInfoProvider.isPropertyAccessNullAware(node)) { + var flow = _flowAnalysis?.flow; + if (flow != null) { + var target = node.target; + if (target is SimpleIdentifier && + target.staticElement is ClassElement) { + // `?.` to access static methods is equivalent to `.`, so do nothing. + } else { + flow.nullAwareAccess_rightBegin( + target, node.realTarget.staticType ?? typeProvider.dynamicType); + _unfinishedNullShorts.add(node.nullShortingTermination); + } } } } @@ -1684,14 +1687,17 @@ class ResolverVisitor extends ScopedVisitor { var target = node.target; target?.accept(this); - if (_migratableAstInfoProvider.isMethodInvocationNullAware(node) && - _flowAnalysis != null) { - if (target is SimpleIdentifier && target.staticElement is ClassElement) { - // `?.` to access static methods is equivalent to `.`, so do nothing. - } else { - _flowAnalysis.flow.nullAwareAccess_rightBegin( - target, node.realTarget.staticType ?? typeProvider.dynamicType); - _unfinishedNullShorts.add(node.nullShortingTermination); + if (_migratableAstInfoProvider.isMethodInvocationNullAware(node)) { + var flow = _flowAnalysis?.flow; + if (flow != null) { + if (target is SimpleIdentifier && + target.staticElement is ClassElement) { + // `?.` to access static methods is equivalent to `.`, so do nothing. + } else { + flow.nullAwareAccess_rightBegin( + target, node.realTarget.staticType ?? typeProvider.dynamicType); + _unfinishedNullShorts.add(node.nullShortingTermination); + } } } diff --git a/pkg/analyzer/test/src/dart/resolution/index_expression_test.dart b/pkg/analyzer/test/src/dart/resolution/index_expression_test.dart index fc67be62e30dd..755806764ce16 100644 --- a/pkg/analyzer/test/src/dart/resolution/index_expression_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/index_expression_test.dart @@ -29,6 +29,19 @@ void f({a = b?[0]}) {} ); } + test_invalid_inDefaultValue_nullAware2() async { + await assertInvalidTestCode(r''' +typedef void F({a = b?[0]}); +'''); + + assertIndexExpression( + findNode.index('[0]'), + readElement: null, + writeElement: null, + type: 'dynamic', + ); + } + test_read() async { await assertNoErrorsInCode(r''' class A { diff --git a/pkg/analyzer/test/src/dart/resolution/method_invocation_test.dart b/pkg/analyzer/test/src/dart/resolution/method_invocation_test.dart index 4421117ecf72d..51b521992ae46 100644 --- a/pkg/analyzer/test/src/dart/resolution/method_invocation_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/method_invocation_test.dart @@ -1718,6 +1718,20 @@ void f({a = b?.foo()}) {} ); } + test_invalid_inDefaultValue_nullAware2() async { + await assertInvalidTestCode(''' +typedef void F({a = b?.foo()}); +'''); + + assertMethodInvocation2( + findNode.methodInvocation('?.foo()'), + element: null, + typeArgumentTypes: [], + invokeType: 'dynamic', + type: 'dynamic', + ); + } + test_namedArgument() async { var question = typeToStringWithNullability ? '?' : ''; await assertNoErrorsInCode(''' diff --git a/pkg/analyzer/test/src/dart/resolution/property_access_test.dart b/pkg/analyzer/test/src/dart/resolution/property_access_test.dart index 645d77ac4b1c8..e1724a4e5e0c4 100644 --- a/pkg/analyzer/test/src/dart/resolution/property_access_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/property_access_test.dart @@ -250,6 +250,18 @@ void f({a = b?.foo}) {} ); } + test_invalid_inDefaultValue_nullAware2() async { + await assertInvalidTestCode(''' +typedef void F({a = b?.foo}); +'''); + + assertPropertyAccess2( + findNode.propertyAccess('?.foo'), + element: null, + type: 'dynamic', + ); + } + test_invalid_inDefaultValue_nullAware_cascade() async { await assertInvalidTestCode(''' void f({a = b?..foo}) {}