Skip to content

Commit

Permalink
Migration: support assignment to index expressions.
Browse files Browse the repository at this point in the history
Change-Id: I7f295247d059f532cabd24518f5009f017a34fd7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/104946
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
  • Loading branch information
stereotype441 authored and commit-bot@chromium.org committed Jun 5, 2019
1 parent 50efa48 commit 9a06144
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pkg/analysis_server/lib/src/nullability/graph_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,11 @@ class GraphBuilder extends GeneralizingAstVisitor<DecoratedType> {
var calleeType = getOrComputeElementType(callee, targetType: targetType);
// TODO(paulberry): substitute if necessary
_handleAssignment(calleeType.positionalParameters[0], node.index);
return calleeType.returnType;
if (node.inSetterContext()) {
return calleeType.positionalParameters[1];
} else {
return calleeType.returnType;
}
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,58 @@ void f(int i) {
assertEdge(decoratedTypeAnnotation('int i').node, never, hard: true);
}

test_assignmentExpression_indexExpression_index() async {
await analyze('''
class C {
void operator[]=(int a, int b) {}
}
void f(C c, int i, int j) {
c[i] = j;
}
''');
assertEdge(decoratedTypeAnnotation('int i').node,
decoratedTypeAnnotation('int a').node,
hard: true);
}

test_assignmentExpression_indexExpression_return_value() async {
await analyze('''
class C {
void operator[]=(int a, int b) {}
}
int f(C c, int i, int j) => c[i] = j;
''');
assertEdge(decoratedTypeAnnotation('int j').node,
decoratedTypeAnnotation('int f').node,
hard: false);
}

test_assignmentExpression_indexExpression_target_check() async {
await analyze('''
class C {
void operator[]=(int a, int b) {}
}
void f(C c, int i, int j) {
c[i] = j;
}
''');
assertNullCheck(checkExpression('c['), decoratedTypeAnnotation('C c').node);
}

test_assignmentExpression_indexExpression_value() async {
await analyze('''
class C {
void operator[]=(int a, int b) {}
}
void f(C c, int i, int j) {
c[i] = j;
}
''');
assertEdge(decoratedTypeAnnotation('int j').node,
decoratedTypeAnnotation('int b').node,
hard: true);
}

test_assignmentExpression_operands() async {
await analyze('''
void f(int i, int j) {
Expand All @@ -120,7 +172,7 @@ void f(int i, int j) {
hard: true);
}

test_assignmentExpression_value() async {
test_assignmentExpression_return_value() async {
await analyze('''
void f(int i, int j) {
g(i = j);
Expand Down
40 changes: 40 additions & 0 deletions pkg/analysis_server/test/src/nullability/provisional_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,46 @@ int? f(C c) => c[0];
await _checkSingleFileChanges(content, expected);
}

test_data_flow_indexed_set_index_value() async {
var content = '''
class C {
void operator[]=(int i, int j) {}
}
void f(C c) {
c[null] = 0;
}
''';
var expected = '''
class C {
void operator[]=(int? i, int j) {}
}
void f(C c) {
c[null] = 0;
}
''';
await _checkSingleFileChanges(content, expected);
}

test_data_flow_indexed_set_value() async {
var content = '''
class C {
void operator[]=(int i, int j) {}
}
void f(C c) {
c[0] = null;
}
''';
var expected = '''
class C {
void operator[]=(int i, int? j) {}
}
void f(C c) {
c[0] = null;
}
''';
await _checkSingleFileChanges(content, expected);
}

test_data_flow_inward() async {
var content = '''
int f(int i) => 0;
Expand Down

0 comments on commit 9a06144

Please sign in to comment.