Skip to content

Commit

Permalink
Flow analysis is not available outside of function bodies and initial…
Browse files Browse the repository at this point in the history
…izers.

This fixes a group of crashes during fuzz testing.

FAILURE: typedef`(=<?[
[NoSuchMethodError: The method 'nullAwareAccess_rightBegin' was called on null.
Receiver: null
Tried calling: nullAwareAccess_rightBegin(Instance of 'ListLiteralImpl', Instance of 'InterfaceTypeImpl'), #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
flutter#1      ResolverVisitor.startNullAwareIndexExpression (package:analyzer/src/generated/resolver.dart:776:26)
flutter#2      ResolverVisitor.visitIndexExpression (package:analyzer/src/generated/resolver.dart:1585:5)
flutter#3      IndexExpressionImpl.accept (package:analyzer/src/dart/ast/ast.dart:5993:49)
flutter#4      DefaultFormalParameterImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:3008:20)
#5      ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1743:10)
flutter#6      UnifyingAstVisitor.visitDefaultFormalParameter (package:analyzer/dart/ast/visitor.dart:3198:65)
flutter#7      ResolverVisitor.visitDefaultFormalParameter (package:analyzer/src/generated/resolver.dart:1239:11)
flutter#8      DefaultFormalParameterImpl.accept (package:analyzer/src/dart/ast/ast.dart:3003:15)
flutter#9      NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7611:20)
flutter#10     FormalParameterListImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:4493:17)
flutter#11     ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1743:10)
flutter#12     UnifyingAstVisitor.visitFormalParameterList (package:analyzer/dart/ast/visitor.dart:3258:59)
flutter#13     ScopedVisitor.visitFormalParameterList (package:analyzer/src/generated/resolver.dart:2717:11)
flutter#14     FormalParameterListImpl.accept (package:analyzer/src/dart/ast/ast.dart:4489:49)
flutter#15     FunctionTypeAliasImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:5166:18)
flutter#16     ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1743:10)
flutter#17     UnifyingAstVisitor.visitFunctionTypeAlias (package:analyzer/dart/ast/visitor.dart:3285:55)
flutter#18     ScopedVisitor.visitFunctionTypeAliasInScope (package:analyzer/src/generated/resolver.dart:2819:11)
flutter#19     ResolverVisitor.visitFunctionTypeAliasInScope (package:analyzer/src/generated/resolver.dart:1490:11)
flutter#20     ScopedVisitor.visitFunctionTypeAlias (package:analyzer/src/generated/resolver.dart:2812:7)
#21     ResolverVisitor.visitFunctionTypeAlias (package:analyzer/src/generated/resolver.dart:1482:13)
#22     FunctionTypeAliasImpl.accept (package:analyzer/src/dart/ast/ast.dart:5158:49)
#23     ResolverVisitor.visitCompilationUnit (package:analyzer/src/generated/resolver.dart:1088:23)
#24     CompilationUnitImpl.accept (package:analyzer/src/dart/ast/ast.dart:2134:49)
#25     LibraryAnalyzer._resolveFile (package:analyzer/src/dart/analysis/library_analyzer.dart:706:10)

Continues //dart-review.googlesource.com/c/sdk/+/175187

Change-Id: I9518363961b4adcf7f57c2f0661151ef74efc2f5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175906
Reviewed-by: Brian Wilkerson <[email protected]>
Reviewed-by: Samuel Rawlins <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and [email protected] committed Dec 12, 2020
1 parent 140afee commit bb5b37e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 25 deletions.
56 changes: 31 additions & 25 deletions pkg/analyzer/lib/src/generated/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/analyzer/test/src/dart/resolution/index_expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions pkg/analyzer/test/src/dart/resolution/method_invocation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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('''
Expand Down
12 changes: 12 additions & 0 deletions pkg/analyzer/test/src/dart/resolution/property_access_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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}) {}
Expand Down

0 comments on commit bb5b37e

Please sign in to comment.