Skip to content

Commit

Permalink
Fix for crash when function expression has empty body.
Browse files Browse the repository at this point in the history
FAILURE: _(:<>(o;?(a(@([
[NoSuchMethodError: The getter 'contextType' was called on null.
Receiver: null
Tried calling: contextType, #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
flutter#1      FunctionExpressionResolver.resolve (package:analyzer/src/dart/resolver/function_expression_resolver.dart:72:35)
flutter#2      ResolverVisitor.visitFunctionExpression (package:analyzer/src/generated/resolver.dart:1424:37)
flutter#3      FunctionExpressionImpl.accept (package:analyzer/src/dart/ast/ast.dart:5004:49)
flutter#4      ResolverVisitor.visitConditionalExpression (package:analyzer/src/generated/resolver.dart:1068:16)
#5      ConditionalExpressionImpl.accept (package:analyzer/src/dart/ast/ast.dart:2265:15)
flutter#6      DefaultFormalParameterImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:3008:20)
flutter#7      ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1710:10)
flutter#8      UnifyingAstVisitor.visitDefaultFormalParameter (package:analyzer/dart/ast/visitor.dart:3198:65)
flutter#9      ResolverVisitor.visitDefaultFormalParameter (package:analyzer/src/generated/resolver.dart:1206:11)
flutter#10     DefaultFormalParameterImpl.accept (package:analyzer/src/dart/ast/ast.dart:3003:15)
flutter#11     NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7611:20)
flutter#12     FormalParameterListImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:4493:17)
flutter#13     ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1710:10)
flutter#14     UnifyingAstVisitor.visitFormalParameterList (package:analyzer/dart/ast/visitor.dart:3258:59)
flutter#15     ScopedVisitor.visitFormalParameterList (package:analyzer/src/generated/resolver.dart:2684:11)
flutter#16     FormalParameterListImpl.accept (package:analyzer/src/dart/ast/ast.dart:4489:49)
flutter#17     FunctionExpressionImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:5009:18)
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:1415:35)
flutter#20     FunctionExpressionImpl.accept (package:analyzer/src/dart/ast/ast.dart:5004:49)
#21     FunctionDeclarationImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:4873:26)
#22     ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1710:10)
#23     UnifyingAstVisitor.visitFunctionDeclaration (package:analyzer/dart/ast/visitor.dart:3271:59)
#24     ScopedVisitor.visitFunctionDeclarationInScope (package:analyzer/src/generated/resolver.dart:2749:11)
#25     ResolverVisitor.visitFunctionDeclarationInScope (package:analyzer/src/generated/resolver.dart:1405:11)
#26     ScopedVisitor.visitFunctionDeclaration (package:analyzer/src/generated/resolver.dart:2742:7)
#27     ResolverVisitor.visitFunctionDeclaration (package:analyzer/src/generated/resolver.dart:1374:11)
#28     FunctionDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:4866:49)
#29     ResolverVisitor.visitCompilationUnit (package:analyzer/src/generated/resolver.dart:1055:23)
#30     CompilationUnitImpl.accept (package:analyzer/src/dart/ast/ast.dart:2134:49)
#31     LibraryAnalyzer._resolveFile (package:analyzer/src/dart/analysis/library_analyzer.dart:706:10)


Change-Id: I62cde978d075bbafacb1b429e87049ca5d54d268
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175907
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and [email protected] committed Dec 12, 2020
1 parent bb5b37e commit 3c2e851
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class FunctionExpressionResolver {
if (_flowAnalysis.flow != null && !isFunctionDeclaration) {
var bodyContext = BodyInferenceContext.of(node.body);
_resolver.checkForBodyMayCompleteNormally(
returnType: bodyContext.contextType,
returnType: bodyContext?.contextType,
body: body,
errorNode: body,
);
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/generated/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1166,8 +1166,8 @@ class ResolverVisitor extends ScopedVisitor {
super.visitConstructorDeclaration(node);

if (_flowAnalysis != null) {
var bodyContext = BodyInferenceContext.of(node.body);
if (node.factoryKeyword != null) {
var bodyContext = BodyInferenceContext.of(node.body);
checkForBodyMayCompleteNormally(
returnType: bodyContext?.contextType,
body: node.body,
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/test/generated/invalid_code_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,12 @@ class B {
@reflectiveTest
class InvalidCodeWithNullSafetyTest extends PubPackageResolutionTest
with WithNullSafetyMixin {
test_functionExpression_emptyBody() async {
await _assertCanBeAnalyzed(r'''
var v = <T>();
''');
}

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

0 comments on commit 3c2e851

Please sign in to comment.