Skip to content

Commit

Permalink
Fix for NPE when no flow for 'if' statement.
Browse files Browse the repository at this point in the history
FAILURE: typedef:(:<>(s{if
[NoSuchMethodError: The method 'ifStatement_thenBegin' was called on null.
Receiver: null
Tried calling: ifStatement_thenBegin(Instance of 'SimpleIdentifierImpl'), #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
flutter#1      ResolverVisitor.visitIfStatement (package:analyzer/src/generated/resolver.dart:1556:26)
flutter#2      IfStatementImpl.accept (package:analyzer/src/dart/ast/ast.dart:5705:49)
flutter#3      NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7611:20)
flutter#4      BlockImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:1087:17)
#5      ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1743:10)
flutter#6      UnifyingAstVisitor.visitBlock (package:analyzer/dart/ast/visitor.dart:3143:31)
flutter#7      ScopedVisitor.visitBlock.<anonymous closure> (package:analyzer/src/generated/resolver.dart:2467:13)
flutter#8      ScopedVisitor._withDeclaredLocals (package:analyzer/src/generated/resolver.dart:3061:8)
flutter#9      ScopedVisitor.visitBlock (package:analyzer/src/generated/resolver.dart:2466:5)
flutter#10     BlockImpl.accept (package:analyzer/src/dart/ast/ast.dart:1083:49)
flutter#11     BlockFunctionBodyImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:1042:13)
flutter#12     ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1743:10)
flutter#13     UnifyingAstVisitor.visitBlockFunctionBody (package:analyzer/dart/ast/visitor.dart:3146:55)
flutter#14     ScopedVisitor.visitBlockFunctionBody (package:analyzer/src/generated/resolver.dart:2476:13)
flutter#15     ResolverVisitor.visitBlockFunctionBody (package:analyzer/src/generated/resolver.dart:954:13)
flutter#16     BlockFunctionBodyImpl.accept (package:analyzer/src/dart/ast/ast.dart:1038:49)
flutter#17     FunctionExpressionImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:5010:12)
flutter#18     FunctionExpressionResolver.resolve (package:analyzer/src/dart/resolver/function_expression_resolver.dart:65:10)
flutter#19     ResolverVisitor.visitFunctionExpression (package:analyzer/src/generated/resolver.dart:1457:37)
flutter#20     FunctionExpressionImpl.accept (package:analyzer/src/dart/ast/ast.dart:5004:49)
#21     DefaultFormalParameterImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:3008:20)
#22     ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1743:10)
#23     UnifyingAstVisitor.visitDefaultFormalParameter (package:analyzer/dart/ast/visitor.dart:3198:65)
#24     ResolverVisitor.visitDefaultFormalParameter (package:analyzer/src/generated/resolver.dart:1239:11)
#25     DefaultFormalParameterImpl.accept (package:analyzer/src/dart/ast/ast.dart:3003:15)
#26     NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7611:20)
#27     FormalParameterListImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:4493:17)
#28     ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1743:10)
#29     UnifyingAstVisitor.visitFormalParameterList (package:analyzer/dart/ast/visitor.dart:3258:59)
#30     ScopedVisitor.visitFormalParameterList (package:analyzer/src/generated/resolver.dart:2717:11)
#31     FormalParameterListImpl.accept (package:analyzer/src/dart/ast/ast.dart:4489:49)
#32     FunctionTypeAliasImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:5166:18)
#33     ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1743:10)
#34     UnifyingAstVisitor.visitFunctionTypeAlias (package:analyzer/dart/ast/visitor.dart:3285:55)
#35     ScopedVisitor.visitFunctionTypeAliasInScope (package:analyzer/src/generated/resolver.dart:2819:11)
#36     ResolverVisitor.visitFunctionTypeAliasInScope (package:analyzer/src/generated/resolver.dart:1490:11)
#37     ScopedVisitor.visitFunctionTypeAlias (package:analyzer/src/generated/resolver.dart:2812:7)
#38     ResolverVisitor.visitFunctionTypeAlias (package:analyzer/src/generated/resolver.dart:1482:13)
#39     FunctionTypeAliasImpl.accept (package:analyzer/src/dart/ast/ast.dart:5158:49)
#40     ResolverVisitor.visitCompilationUnit (package:analyzer/src/generated/resolver.dart:1088:23)
#41     CompilationUnitImpl.accept (package:analyzer/src/dart/ast/ast.dart:2134:49)
#42     LibraryAnalyzer._resolveFile (package:analyzer/src/dart/analysis/library_analyzer.dart:706:10)


[email protected]

Change-Id: I72bf7219d55d0bf53beae11f977f8fb840dd8e2e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175908
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
scheglov authored and [email protected] committed Dec 12, 2020
1 parent 3c2e851 commit b21913b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/analyzer/lib/src/generated/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,7 @@ class ResolverVisitor extends ScopedVisitor {

CollectionElement thenElement = node.thenElement;
if (_flowAnalysis != null) {
_flowAnalysis.flow.ifStatement_thenBegin(condition);
_flowAnalysis.flow?.ifStatement_thenBegin(condition);
thenElement.accept(this);
} else {
_promoteManager.visitIfElement_thenElement(
Expand Down Expand Up @@ -1556,7 +1556,7 @@ class ResolverVisitor extends ScopedVisitor {

Statement thenStatement = node.thenStatement;
if (_flowAnalysis != null) {
_flowAnalysis.flow.ifStatement_thenBegin(condition);
_flowAnalysis.flow?.ifStatement_thenBegin(condition);
visitStatementInScope(thenStatement);
nullSafetyDeadCodeVerifier?.flowEnd(thenStatement);
} else {
Expand Down
12 changes: 12 additions & 0 deletions pkg/analyzer/test/generated/invalid_code_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,18 @@ var v = <T>();
''');
}

test_inDefaultValue_noFlow_ifExpression() async {
await _assertCanBeAnalyzed('''
typedef void F({a = [if (true) 0]});
''');
}

test_inDefaultValue_noFlow_ifStatement() async {
await _assertCanBeAnalyzed('''
typedef void F([a = () { if (true) 0; }]);
''');
}

test_issue_40837() async {
await _assertCanBeAnalyzed('''
class A {
Expand Down

0 comments on commit b21913b

Please sign in to comment.