Skip to content

Commit 35f45f0

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Do null-aware shorting for IndexExpression.
[email protected] Change-Id: I72e6aabb4fd2f79201ee1146e6ea83aa54ceb7a6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115763 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 90aca0b commit 35f45f0

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -616,14 +616,9 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<void> {
616616
}
617617

618618
type ??= _dynamicType;
619-
if (_nonNullableEnabled) {
620-
if (node.leftBracket.type ==
621-
TokenType.QUESTION_PERIOD_OPEN_SQUARE_BRACKET) {
622-
type = _typeSystem.makeNullable(type);
623-
}
624-
}
625619

626620
_recordStaticType(node, type);
621+
_nullShortingTermination(node);
627622
}
628623

629624
/**
@@ -2154,6 +2149,11 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<void> {
21542149
if (node is AssignmentExpression) {
21552150
return _hasNullShorting(node.leftHandSide);
21562151
}
2152+
if (node is IndexExpression) {
2153+
return node.leftBracket.type ==
2154+
TokenType.QUESTION_PERIOD_OPEN_SQUARE_BRACKET ||
2155+
_hasNullShorting(node.target);
2156+
}
21572157
if (node is PropertyAccess) {
21582158
return node.operator.type == TokenType.QUESTION_PERIOD ||
21592159
_hasNullShorting(node.target);

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ main(A? a) {
218218
assertType(indexExpression, 'bool?');
219219
}
220220

221-
@failingTest
222221
test_readWrite_nullable() async {
223222
await assertNoErrorsInCode(r'''
224223
class A {
@@ -242,7 +241,7 @@ main(A? a) {
242241
indexExpression.index,
243242
indexEqElement.parameters[0],
244243
);
245-
assertType(indexExpression, 'num?');
244+
assertType(indexExpression, 'num');
246245

247246
var assignment = indexExpression.parent as AssignmentExpression;
248247
assertElement(assignment, numPlusElement);
@@ -253,7 +252,6 @@ main(A? a) {
253252
);
254253
}
255254

256-
@failingTest
257255
test_write_nullable() async {
258256
await assertNoErrorsInCode(r'''
259257
class A {
@@ -274,7 +272,7 @@ main(A? a) {
274272
indexExpression.index,
275273
indexEqElement.parameters[0],
276274
);
277-
assertType(indexExpression, 'num?');
275+
assertType(indexExpression, 'num');
278276

279277
var assignment = indexExpression.parent as AssignmentExpression;
280278
assertElement(assignment, null);

tests/language_2/nnbd/resolution/question_dot_index_produces_nullable_type_test.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,26 @@ void main() {}
88

99
void f1(NotGeneric x) {
1010
x[0] + 1; //# 01: ok
11+
x[0] = 1; //# 02: ok
12+
useNonNullable(x[0] = 1); //# 03: ok
13+
x[0] += 1; //# 04: ok
14+
useNonNullable(x[0] += 1); //# 05: ok
1115
}
1216

1317
void f2(NotGeneric? x) {
14-
x?.[0] + 1; //# 02: compile-time error
18+
x?.[0] + 1; //# 06: compile-time error
19+
x?.[0] = 1; //# 07: ok
20+
useNonNullable(x?.[0] = 1); //# 08: compile-time error
21+
x?.[0] += 1; //# 09: ok
22+
useNonNullable(x?.[0] += 1); //# 10: compile-time error
1523
}
1624

1725
void f3<T extends num>(Generic<T>? x) {
18-
x?.[0] + 1; //# 03: compile-time error
26+
x?.[0] + 1; //# 11: compile-time error
1927
}
2028

2129
void f4<T extends num>(Generic<T?> x) {
22-
x[0] + 1; //# 04: compile-time error
30+
x[0] + 1; //# 12: compile-time error
2331
}
2432

2533
class NotGeneric {
@@ -31,3 +39,5 @@ class Generic<T> {
3139
T operator[](int index) => throw 'unreachable';
3240
void operator[]=(int index, T value) => throw 'unreachable';
3341
}
42+
43+
void useNonNullable(int a) {}

0 commit comments

Comments
 (0)