Skip to content

Commit

Permalink
Migration: support assignments to fields and setters.
Browse files Browse the repository at this point in the history
Change-Id: I4dbf43878f5cbf0a16a041e0975880c3d844fa13
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/105041
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
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 6, 2019
1 parent 4ccae23 commit c130384
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 5 deletions.
16 changes: 12 additions & 4 deletions pkg/analysis_server/lib/src/nullability/graph_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ class GraphBuilder extends GeneralizingAstVisitor<DecoratedType> {
baseElement.isSynthetic &&
!baseElement.variable.isSynthetic) {
var variable = baseElement.variable;
var decoratedElementType =
_variables.decoratedElementType(variable, create: true);
if (baseElement.isGetter) {
decoratedBaseType = DecoratedType(
baseElement.type, NullabilityNode.never,
returnType:
_variables.decoratedElementType(variable, create: true));
returnType: decoratedElementType);
} else {
throw UnimplementedError('TODO(paulberry)');
assert(baseElement.isSetter);
decoratedBaseType = DecoratedType(
baseElement.type, NullabilityNode.never,
positionalParameters: [decoratedElementType]);
}
} else {
decoratedBaseType =
Expand Down Expand Up @@ -575,7 +579,11 @@ class GraphBuilder extends GeneralizingAstVisitor<DecoratedType> {
}
var calleeType = getOrComputeElementType(callee, targetType: targetType);
// TODO(paulberry): substitute if necessary
return calleeType.returnType;
if (propertyName.inSetterContext()) {
return calleeType.positionalParameters[0];
} else {
return calleeType.returnType;
}
}

/// Double checks that [type] is sufficiently simple for this naive prototype
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,33 @@ void f(int i) {
assertEdge(decoratedTypeAnnotation('int i').node, never, hard: true);
}

test_assignmentExpression_field() async {
await analyze('''
class C {
int x = 0;
}
void f(C c, int i) {
c.x = i;
}
''');
assertEdge(decoratedTypeAnnotation('int i').node,
decoratedTypeAnnotation('int x').node,
hard: true);
}

test_assignmentExpression_field_target_check() async {
await analyze('''
class C {
int x = 0;
}
void f(C c, int i) {
c.x = i;
}
''');
assertNullCheck(
checkExpression('c.x'), decoratedTypeAnnotation('C c').node);
}

test_assignmentExpression_indexExpression_index() async {
await analyze('''
class C {
Expand Down Expand Up @@ -183,6 +210,33 @@ void g(int k) {}
hard: false);
}

test_assignmentExpression_setter() async {
await analyze('''
class C {
void set s(int value) {}
}
void f(C c, int i) {
c.s = i;
}
''');
assertEdge(decoratedTypeAnnotation('int i').node,
decoratedTypeAnnotation('int value').node,
hard: true);
}

test_assignmentExpression_setter_target_check() async {
await analyze('''
class C {
void set s(int value) {}
}
void f(C c, int i) {
c.s = i;
}
''');
assertNullCheck(
checkExpression('c.s'), decoratedTypeAnnotation('C c').node);
}

test_binaryExpression_add_left_check() async {
await analyze('''
int f(int i, int j) => i + j;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,27 @@ main() {
await _checkSingleFileChanges(content, expected);
}

test_data_flow_assignment() async {
test_data_flow_assignment_field() async {
var content = '''
class C {
int x = 0;
}
void f(C c) {
c.x = null;
}
''';
var expected = '''
class C {
int? x = 0;
}
void f(C c) {
c.x = null;
}
''';
await _checkSingleFileChanges(content, expected);
}

test_data_flow_assignment_local() async {
var content = '''
void main() {
int i = 0;
Expand All @@ -240,6 +260,26 @@ void main() {
await _checkSingleFileChanges(content, expected);
}

test_data_flow_assignment_setter() async {
var content = '''
class C {
void set s(int value) {}
}
void f(C c) {
c.s = null;
}
''';
var expected = '''
class C {
void set s(int? value) {}
}
void f(C c) {
c.s = null;
}
''';
await _checkSingleFileChanges(content, expected);
}

test_data_flow_field_read() async {
var content = '''
class C {
Expand Down

0 comments on commit c130384

Please sign in to comment.