Skip to content

Commit 2c4fd4e

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[analyzer] Dot shorthands: Allow index expressions.
`IndexExpressionImpl`s now have the `DotShorthandMixin` applied. We save the context at the point of resolving an index expression to be used for resolving a dot shorthand head later. Unit tests added. There's multiple co19 tests that will start passing, but rely on https://dart-review.googlesource.com/c/sdk/+/425181. Bug: #59835 Change-Id: I08076bb437bad1955d353a24fc119466b7dfad61 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/425360 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent c31a282 commit 2c4fd4e

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

pkg/analyzer/lib/src/dart/ast/ast.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -10091,7 +10091,7 @@ abstract final class IndexExpression
1009110091
}
1009210092

1009310093
final class IndexExpressionImpl extends ExpressionImpl
10094-
with NullShortableExpressionImpl
10094+
with NullShortableExpressionImpl, DotShorthandMixin
1009510095
implements IndexExpression {
1009610096
@override
1009710097
Token? period;

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

+11
Original file line numberDiff line numberDiff line change
@@ -3074,6 +3074,12 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
30743074
TypeImpl contextType = UnknownInferredType.instance,
30753075
}) {
30763076
inferenceLogWriter?.enterExpression(node, contextType);
3077+
3078+
// If [isDotShorthand] is set, cache the context type for resolution.
3079+
if (isDotShorthand(node)) {
3080+
pushDotShorthandContext(node, SharedTypeSchemaView(contextType));
3081+
}
3082+
30773083
checkUnreachableNode(node);
30783084

30793085
var target = node.target;
@@ -3132,6 +3138,11 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
31323138
nullShortingTermination(node, rewrittenExpression: replacement);
31333139
_insertImplicitCallReference(replacement, contextType: contextType);
31343140
nullSafetyDeadCodeVerifier.verifyIndexExpression(node);
3141+
3142+
if (isDotShorthand(node)) {
3143+
popDotShorthandContext();
3144+
}
3145+
31353146
inferenceLogWriter?.exitExpression(node);
31363147
}
31373148

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

+29
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,35 @@ DotShorthandInvocation
321321
''');
322322
}
323323

324+
test_equality_indexExpression() async {
325+
await assertNoErrorsInCode(r'''
326+
class C {
327+
int x;
328+
C(this.x);
329+
static List<C> instances() => [C(1)];
330+
}
331+
332+
void main() {
333+
print(C(1) == .instances()[0]);
334+
}
335+
''');
336+
337+
var identifier = findNode.singleDotShorthandInvocation;
338+
assertResolvedNodeText(identifier, r'''
339+
DotShorthandInvocation
340+
period: .
341+
memberName: SimpleIdentifier
342+
token: instances
343+
element: <testLibraryFragment>::@class::C::@method::instances#element
344+
staticType: List<C> Function()
345+
argumentList: ArgumentList
346+
leftParenthesis: (
347+
rightParenthesis: )
348+
staticInvokeType: List<C> Function()
349+
staticType: List<C>
350+
''');
351+
}
352+
324353
test_extensionType() async {
325354
await assertNoErrorsInCode(r'''
326355
extension type C(int integer) {

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

+25
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,31 @@ DotShorthandPropertyAccess
270270
''');
271271
}
272272

273+
test_equality_indexExpression() async {
274+
await assertNoErrorsInCode(r'''
275+
class C {
276+
int x;
277+
C(this.x);
278+
static List<C> instances = [C(1)];
279+
}
280+
281+
void main() {
282+
print(C(1) == .instances[0]);
283+
}
284+
''');
285+
286+
var identifier = findNode.singleDotShorthandPropertyAccess;
287+
assertResolvedNodeText(identifier, r'''
288+
DotShorthandPropertyAccess
289+
period: .
290+
propertyName: SimpleIdentifier
291+
token: instances
292+
element: <testLibraryFragment>::@class::C::@getter::instances#element
293+
staticType: List<C>
294+
staticType: List<C>
295+
''');
296+
}
297+
273298
test_equality_pattern() async {
274299
await assertNoErrorsInCode('''
275300
enum Color { red, blue }

0 commit comments

Comments
 (0)