Skip to content

Commit ee951bc

Browse files
kallentuCommit Queue
authored andcommitted
[analyzer] Dot shorthands: Error when calling an instance method using a shorthand.
Fixes an analyzer bug where if we are using a dot shorthand to invoke an instance method, it doesn't produce any errors. We were neglecting to check whether the element is static or not. Bug: #61954 Change-Id: I3bc7c3656c7fc1f3cfe4c7751bd40e5613926d1f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/461900 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent a16f481 commit ee951bc

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ class MethodInvocationResolver with ScopeHelpers {
12451245
}) {
12461246
var element = _resolveElement(receiver, node.memberName);
12471247
if (element != null) {
1248-
if (element is InternalExecutableElement) {
1248+
if (element is InternalExecutableElement && element.isStatic) {
12491249
node.memberName.element = element;
12501250
if (element is InternalPropertyAccessorElement) {
12511251
return _rewriteAsFunctionExpressionInvocation(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,22 @@ void main() {
396396
);
397397
}
398398

399+
test_error_notStatic() async {
400+
await assertErrorsInCode(
401+
r'''
402+
class C {
403+
C foo() => C();
404+
}
405+
406+
void main() {
407+
final C c = .foo();
408+
print(c);
409+
}
410+
''',
411+
[error(diag.dotShorthandUndefinedInvocation, 60, 3)],
412+
);
413+
}
414+
399415
test_error_unresolved() async {
400416
await assertErrorsInCode(
401417
r'''
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Using dot shorthand syntax on an instance method.
6+
7+
// SharedOptions=--enable-experiment=dot-shorthands
8+
9+
class C {
10+
C foo() => C();
11+
}
12+
13+
void main() {
14+
C c = .foo();
15+
// ^^^
16+
// [analyzer] COMPILE_TIME_ERROR.DOT_SHORTHAND_UNDEFINED_MEMBER
17+
// [cfe] The static method or constructor 'foo' isn't defined for the type 'C'.
18+
}

0 commit comments

Comments
 (0)