Skip to content

Commit ef85087

Browse files
kallentuCommit Queue
authored andcommitted
[analyzer] Dot Shorthands: Add support for FutureOr.
Finds the namespace of `S` for a context type of `FutureOr<S>` and uses that to resolve the shorthand. Unit tests written and language tests cover this behavior. Bug: #59835 Change-Id: Iec7bfeaef2c0ab20ee2031c4dc0b3ef0b9e1fc66 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/422363 Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 3dd7e22 commit ef85087

File tree

4 files changed

+127
-4
lines changed

4 files changed

+127
-4
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,13 @@ class MethodInvocationResolver with ScopeHelpers {
209209
List<WhyNotPromotedGetter> whyNotPromotedArguments) {
210210
_invocation = node;
211211

212-
var contextType = _resolver.getDotShorthandContext().unwrapTypeSchemaView();
212+
TypeImpl contextType =
213+
_resolver.getDotShorthandContext().unwrapTypeSchemaView();
214+
215+
// The static namespace denoted by `S` is also the namespace denoted by
216+
// `FutureOr<S>`.
217+
contextType = _resolver.typeSystem.futureOrBase(contextType);
218+
213219
// TODO(kallentu): Dot shorthands work - Support other context types
214220
if (contextType is InterfaceTypeImpl) {
215221
var contextElement = contextType.element3;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class PropertyElementResolver with ScopeHelpers {
4747
}
4848
TypeImpl context =
4949
_resolver.getDotShorthandContext().unwrapTypeSchemaView();
50+
51+
// The static namespace denoted by `S` is also the namespace denoted by
52+
// `FutureOr<S>`.
53+
context = _resolver.typeSystem.futureOrBase(context);
54+
5055
// TODO(kallentu): Support other context types
5156
if (context is InterfaceTypeImpl) {
5257
var identifier = node.propertyName;

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

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ main() {
1616

1717
@reflectiveTest
1818
class DotShorthandInvocationResolutionTest extends PubPackageResolutionTest {
19-
test_dotShorthand_basic() async {
19+
test_basic() async {
2020
await assertNoErrorsInCode(r'''
2121
class C {
2222
static C member() => C(1);
@@ -46,7 +46,7 @@ DotShorthandInvocation
4646
''');
4747
}
4848

49-
test_dotShorthand_basic_generic() async {
49+
test_basic_generic() async {
5050
await assertNoErrorsInCode(r'''
5151
class C<T> {
5252
static C member<U>(U x) => C(x);
@@ -93,7 +93,7 @@ DotShorthandInvocation
9393
''');
9494
}
9595

96-
test_dotShorthand_basic_parameter() async {
96+
test_basic_parameter() async {
9797
await assertNoErrorsInCode(r'''
9898
class C {
9999
static C member(int x) => C(x);
@@ -125,6 +125,70 @@ DotShorthandInvocation
125125
rightParenthesis: )
126126
staticInvokeType: C Function(int)
127127
staticType: C
128+
''');
129+
}
130+
131+
test_futureOr() async {
132+
await assertNoErrorsInCode(r'''
133+
import 'dart:async';
134+
135+
class C {
136+
static C member() => C(1);
137+
int x;
138+
C(this.x);
139+
}
140+
141+
void main() {
142+
FutureOr<C> c = .member();
143+
print(c);
144+
}
145+
''');
146+
147+
var node = findNode.singleDotShorthandInvocation;
148+
assertResolvedNodeText(node, r'''
149+
DotShorthandInvocation
150+
period: .
151+
memberName: SimpleIdentifier
152+
token: member
153+
element: <testLibraryFragment>::@class::C::@method::member#element
154+
staticType: C Function()
155+
argumentList: ArgumentList
156+
leftParenthesis: (
157+
rightParenthesis: )
158+
staticInvokeType: C Function()
159+
staticType: C
160+
''');
161+
}
162+
163+
test_futureOr_nested() async {
164+
await assertNoErrorsInCode(r'''
165+
import 'dart:async';
166+
167+
class C {
168+
static C member() => C(1);
169+
int x;
170+
C(this.x);
171+
}
172+
173+
void main() {
174+
FutureOr<FutureOr<C>> c = .member();
175+
print(c);
176+
}
177+
''');
178+
179+
var node = findNode.singleDotShorthandInvocation;
180+
assertResolvedNodeText(node, r'''
181+
DotShorthandInvocation
182+
period: .
183+
memberName: SimpleIdentifier
184+
token: member
185+
element: <testLibraryFragment>::@class::C::@method::member#element
186+
staticType: C Function()
187+
argumentList: ArgumentList
188+
leftParenthesis: (
189+
rightParenthesis: )
190+
staticInvokeType: C Function()
191+
staticType: C
128192
''');
129193
}
130194
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,54 @@ DotShorthandPropertyAccess
6565
''');
6666
}
6767

68+
test_futureOr() async {
69+
await assertNoErrorsInCode('''
70+
import 'dart:async';
71+
72+
enum C { red }
73+
74+
void main() {
75+
FutureOr<C> c = .red;
76+
print(c);
77+
}
78+
''');
79+
80+
var identifier = findNode.singleDotShorthandPropertyAccess;
81+
assertResolvedNodeText(identifier, r'''
82+
DotShorthandPropertyAccess
83+
period: .
84+
propertyName: SimpleIdentifier
85+
token: red
86+
element: <testLibraryFragment>::@enum::C::@getter::red#element
87+
staticType: C
88+
staticType: C
89+
''');
90+
}
91+
92+
test_futureOr_nested() async {
93+
await assertNoErrorsInCode('''
94+
import 'dart:async';
95+
96+
enum C { red }
97+
98+
void main() {
99+
FutureOr<FutureOr<C>> c = .red;
100+
print(c);
101+
}
102+
''');
103+
104+
var identifier = findNode.singleDotShorthandPropertyAccess;
105+
assertResolvedNodeText(identifier, r'''
106+
DotShorthandPropertyAccess
107+
period: .
108+
propertyName: SimpleIdentifier
109+
token: red
110+
element: <testLibraryFragment>::@enum::C::@getter::red#element
111+
staticType: C
112+
staticType: C
113+
''');
114+
}
115+
68116
test_object_new() async {
69117
await assertNoErrorsInCode('''
70118
void main() {

0 commit comments

Comments
 (0)