Skip to content

Commit de6b38c

Browse files
fshcheglovCommit Queue
authored andcommitted
Fix an issue with null access when resolving receiver type.
Bug: #61854 Change-Id: I279d164711160fbf123034deaeb5e09aac92cf57 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/457941 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 421d653 commit de6b38c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pkg/analyzer/lib/src/dart/resolver/method_invocation_resolver.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,11 @@ class MethodInvocationResolver with ScopeHelpers {
11561156

11571157
String receiverClassName = '<unknown>';
11581158
if (receiverType is InterfaceTypeImpl) {
1159-
receiverClassName = receiverType.element.name!;
1159+
if (receiverType.element.name case var name?) {
1160+
receiverClassName = name;
1161+
} else {
1162+
return null;
1163+
}
11601164
} else if (receiverType is FunctionType) {
11611165
receiverClassName = 'Function';
11621166
}

pkg/analyzer/test/src/dart/resolution/method_invocation_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,38 @@ MethodInvocation
25902590
''');
25912591
}
25922592

2593+
test_error_undefinedMethod_noTarget_synthetic_class() async {
2594+
await assertErrorsInCode(
2595+
r'''
2596+
class {
2597+
void f() {
2598+
foo(0);
2599+
}
2600+
}
2601+
''',
2602+
[error(ParserErrorCode.missingIdentifier, 6, 1)],
2603+
);
2604+
2605+
var node = findNode.methodInvocation('foo(0);');
2606+
assertResolvedNodeText(node, r'''
2607+
MethodInvocation
2608+
methodName: SimpleIdentifier
2609+
token: foo
2610+
element: <null>
2611+
staticType: InvalidType
2612+
argumentList: ArgumentList
2613+
leftParenthesis: (
2614+
arguments
2615+
IntegerLiteral
2616+
literal: 0
2617+
correspondingParameter: <null>
2618+
staticType: int
2619+
rightParenthesis: )
2620+
staticInvokeType: InvalidType
2621+
staticType: InvalidType
2622+
''');
2623+
}
2624+
25932625
test_error_undefinedMethod_null() async {
25942626
await assertErrorsInCode(
25952627
r'''

0 commit comments

Comments
 (0)