Skip to content

Commit 8f1005f

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[analyzer] Dot shorthands: Handle constructor tearoffs.
Since `DotShorthandPropertyAccess` can be a tearoff or a static getter, I updated the logic to handle constructor tearoffs before trying to find a getter. co19 tests passing and unit tests added. Bug: #59835 Change-Id: Idbd9f98058d9b66347a825840f80c0e607f6b305 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/427520 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent d2ad38a commit 8f1005f

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

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

+8-16
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,21 @@ class PropertyElementResolver with ScopeHelpers {
5656
// `FutureOr<S>`.
5757
context = _resolver.typeSystem.futureOrBase(context);
5858

59-
// TODO(kallentu): Support other context types
6059
if (context is InterfaceTypeImpl) {
6160
var identifier = node.propertyName;
62-
if (identifier.name == 'new') {
63-
var element = context.lookUpConstructor2(
64-
identifier.name,
65-
_definingLibrary,
66-
);
67-
// We didn't resolve to any static getter or static field using the
68-
// context type.
69-
if (element == null) {
70-
errorReporter.atNode(
71-
node,
72-
CompileTimeErrorCode.DOT_SHORTHAND_UNDEFINED_GETTER,
73-
arguments: [node.propertyName.name, context.getDisplayString()],
74-
);
75-
return PropertyElementResolverResult();
76-
}
61+
// Find constructor tearoffs.
62+
var element = context.lookUpConstructor2(
63+
identifier.name,
64+
_definingLibrary,
65+
);
66+
if (element != null) {
7767
return PropertyElementResolverResult(
7868
readElementRequested2: element,
7969
getType: element.returnType,
8070
);
8171
}
72+
73+
// Didn't find any constructor tearoffs, look for static getters.
8274
var contextElement = context.element3;
8375
return _resolveTargetInterfaceElement(
8476
typeReference: contextElement,

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ void main() {
370370
print(c);
371371
}
372372
''',
373-
[error(CompileTimeErrorCode.DOT_SHORTHAND_UNDEFINED_GETTER, 48, 4)],
373+
[error(CompileTimeErrorCode.DOT_SHORTHAND_UNDEFINED_GETTER, 49, 3)],
374374
);
375375
}
376376

@@ -512,7 +512,35 @@ DotShorthandPropertyAccess
512512
''');
513513
}
514514

515-
test_object_new() async {
515+
test_tearOff_constructor() async {
516+
await assertNoErrorsInCode(r'''
517+
class C1 {
518+
C1.id();
519+
520+
@override
521+
bool operator ==(Object other) => identical(C1.id, other);
522+
}
523+
524+
main() {
525+
bool x = C1.id() == .id;
526+
print(x);
527+
}
528+
''');
529+
530+
var identifier = findNode.singleDotShorthandPropertyAccess;
531+
assertResolvedNodeText(identifier, r'''
532+
DotShorthandPropertyAccess
533+
period: .
534+
propertyName: SimpleIdentifier
535+
token: id
536+
element: <testLibraryFragment>::@class::C1::@constructor::id#element
537+
staticType: C1 Function()
538+
correspondingParameter: <testLibraryFragment>::@class::C1::@method::==::@parameter::other#element
539+
staticType: C1 Function()
540+
''');
541+
}
542+
543+
test_tearOff_constructor_new() async {
516544
await assertNoErrorsInCode('''
517545
void main() {
518546
Object o = .new;

0 commit comments

Comments
 (0)