Skip to content

Commit 6089a94

Browse files
Dan Rubelcommit-bot@chromium.org
authored andcommitted
add migration engine support for imported top level var references
In addition to adding imported top level var references support, this revert GraphBuilder._checkAssignment changes so that the method conforms to team expectations and addresses comments in https://dart-review.googlesource.com/c/sdk/+/106562 Change-Id: I3e2b021ec51a1363706a7f6811807d0e6a680df5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106920 Reviewed-by: Paul Berry <paulberry@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent 9c62632 commit 6089a94

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

pkg/nnbd_migration/lib/src/decorated_type.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class DecoratedType {
8787
DecoratedType decoratedType;
8888
if (element is ExecutableElement) {
8989
decoratedType = decorate(element.type);
90+
} else if (element is TopLevelVariableElement) {
91+
decoratedType = decorate(element.type);
9092
} else {
9193
// TODO(paulberry)
9294
throw UnimplementedError('Decorating ${element.runtimeType}');

pkg/nnbd_migration/lib/src/graph_builder.dart

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,12 @@ $stackTrace''');
638638
staticElement is LocalVariableElement) {
639639
return getOrComputeElementType(staticElement);
640640
} else if (staticElement is PropertyAccessorElement) {
641-
// TODO(danrubel): assuming getter context... need to handle setter
642-
return getOrComputeElementType(staticElement).returnType;
641+
if (staticElement.isGetter) {
642+
return getOrComputeElementType(staticElement).returnType;
643+
} else {
644+
// TODO(danrubel) handle setter
645+
_unimplemented(node, 'Setter');
646+
}
643647
} else if (staticElement is ClassElement) {
644648
return _nonNullableTypeType;
645649
} else {
@@ -738,14 +742,10 @@ $stackTrace''');
738742
guards: _guards, hard: hard);
739743
expressionChecks?.edges?.add(edge);
740744
// TODO(paulberry): generalize this.
741-
742745
if ((_isSimple(sourceType) || destinationType.type.isObject) &&
743746
_isSimple(destinationType)) {
744747
// Ok; nothing further to do.
745-
return;
746-
}
747-
748-
if (sourceType.type is InterfaceType &&
748+
} else if (sourceType.type is InterfaceType &&
749749
destinationType.type is InterfaceType &&
750750
sourceType.type.element == destinationType.type.element) {
751751
assert(sourceType.typeArguments.length ==
@@ -755,16 +755,11 @@ $stackTrace''');
755755
sourceType.typeArguments[i], expressionChecks,
756756
hard: false);
757757
}
758-
return;
759-
}
760-
761-
if (destinationType.type.isDynamic || sourceType.type.isDynamic) {
758+
} else if (destinationType.type.isDynamic || sourceType.type.isDynamic) {
762759
// ok; nothing further to do.
763-
return;
760+
} else {
761+
throw '$destinationType <= $sourceType'; // TODO(paulberry)
764762
}
765-
766-
// TODO(paulberry)
767-
throw '$destinationType <= $sourceType';
768763
}
769764

770765
/// Double checks that [name] is not the name of a method or getter declared

pkg/nnbd_migration/test/graph_builder_test.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,15 +1667,23 @@ int f() {
16671667
double pi = 3.1415;
16681668
double get myPi => pi;
16691669
''');
1670-
var pi = findNode.topLevelVariableDeclaration('double pi');
1671-
var piType =
1672-
variables.decoratedTypeAnnotation(testSource, pi.variables.type);
1673-
var myPi = findNode.any('myPi').parent as FunctionDeclaration;
1674-
var myPiType =
1675-
variables.decoratedTypeAnnotation(testSource, myPi.returnType);
1670+
var piType = decoratedTypeAnnotation('double pi');
1671+
var myPiType = decoratedTypeAnnotation('double get');
16761672
assertEdge(piType.node, myPiType.node, hard: false);
16771673
}
16781674

1675+
test_topLevelVar_reference_differentPackage() async {
1676+
addPackageFile('pkgPi', 'piConst.dart', '''
1677+
double pi = 3.1415;
1678+
''');
1679+
await analyze('''
1680+
import "package:pkgPi/piConst.dart";
1681+
double get myPi => pi;
1682+
''');
1683+
var myPiType = decoratedTypeAnnotation('double get');
1684+
assertEdge(never, myPiType.node, hard: false);
1685+
}
1686+
16791687
test_type_argument_explicit_bound() async {
16801688
await analyze('''
16811689
class C<T extends Object> {}

0 commit comments

Comments
 (0)