[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: https://github.com/dart-lang/sdk/issues/61954
Change-Id: I3bc7c3656c7fc1f3cfe4c7751bd40e5613926d1f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/461900
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
diff --git a/pkg/analyzer/lib/src/dart/resolver/method_invocation_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/method_invocation_resolver.dart
index ebaf493..6d8e370 100644
--- a/pkg/analyzer/lib/src/dart/resolver/method_invocation_resolver.dart
+++ b/pkg/analyzer/lib/src/dart/resolver/method_invocation_resolver.dart
@@ -1245,7 +1245,7 @@
}) {
var element = _resolveElement(receiver, node.memberName);
if (element != null) {
- if (element is InternalExecutableElement) {
+ if (element is InternalExecutableElement && element.isStatic) {
node.memberName.element = element;
if (element is InternalPropertyAccessorElement) {
return _rewriteAsFunctionExpressionInvocation(
diff --git a/pkg/analyzer/test/src/dart/resolution/dot_shorthand_invocation_test.dart b/pkg/analyzer/test/src/dart/resolution/dot_shorthand_invocation_test.dart
index 16a7e86..0352581 100644
--- a/pkg/analyzer/test/src/dart/resolution/dot_shorthand_invocation_test.dart
+++ b/pkg/analyzer/test/src/dart/resolution/dot_shorthand_invocation_test.dart
@@ -396,6 +396,22 @@
);
}
+ test_error_notStatic() async {
+ await assertErrorsInCode(
+ r'''
+class C {
+ C foo() => C();
+}
+
+void main() {
+ final C c = .foo();
+ print(c);
+}
+''',
+ [error(diag.dotShorthandUndefinedInvocation, 60, 3)],
+ );
+ }
+
test_error_unresolved() async {
await assertErrorsInCode(
r'''
diff --git a/tests/language/dot_shorthands/member/static_method_error_test.dart b/tests/language/dot_shorthands/member/static_method_error_test.dart
new file mode 100644
index 0000000..ea3395c
--- /dev/null
+++ b/tests/language/dot_shorthands/member/static_method_error_test.dart
@@ -0,0 +1,18 @@
+// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// Using dot shorthand syntax on an instance method.
+
+// SharedOptions=--enable-experiment=dot-shorthands
+
+class C {
+ C foo() => C();
+}
+
+void main() {
+ C c = .foo();
+ // ^^^
+ // [analyzer] COMPILE_TIME_ERROR.DOT_SHORTHAND_UNDEFINED_MEMBER
+ // [cfe] The static method or constructor 'foo' isn't defined for the type 'C'.
+}