Skip to content

Commit 41d8b9e

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
NNBD Migrator: Handle super redirecting initializers
Change-Id: I52043caaad81a89fe12cc554c32d1b0943d37b27 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127420 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Paul Berry <[email protected]>
1 parent baf1afa commit 41d8b9e

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

pkg/nnbd_migration/lib/src/edge_builder.dart

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,8 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
12041204
}
12051205
return type;
12061206
} else if (staticElement is FunctionElement ||
1207-
staticElement is MethodElement) {
1207+
staticElement is MethodElement ||
1208+
staticElement is ConstructorElement) {
12081209
return getOrComputeElementType(staticElement);
12091210
} else if (staticElement is PropertyAccessorElement) {
12101211
var elementType = getOrComputeElementType(staticElement);
@@ -1261,6 +1262,23 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
12611262
return _makeNonNullLiteralType(node);
12621263
}
12631264

1265+
@override
1266+
DecoratedType visitSuperConstructorInvocation(
1267+
SuperConstructorInvocation node) {
1268+
var callee = node.staticElement;
1269+
var nullabilityNode = NullabilityNode.forInferredType();
1270+
var createdType = DecoratedType(callee.returnType, nullabilityNode);
1271+
var calleeType = getOrComputeElementType(callee, targetType: createdType);
1272+
_handleInvocationArguments(
1273+
node,
1274+
node.argumentList.arguments,
1275+
null /* typeArguments */,
1276+
[] /* typeArgumentTypes */,
1277+
calleeType,
1278+
[] /* constructorTypeParameters */);
1279+
return null;
1280+
}
1281+
12641282
@override
12651283
DecoratedType visitSuperExpression(SuperExpression node) {
12661284
return _handleThisOrSuper(node);
@@ -1933,7 +1951,7 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
19331951
}
19341952
}
19351953

1936-
/// Creates the necessary constraint(s) for an [argumentList] when invoking an
1954+
/// Creates the necessary constraint(s) for an [ArgumentList] when invoking an
19371955
/// executable element whose type is [calleeType].
19381956
///
19391957
/// Returns the decorated return type of the invocation, after any necessary

pkg/nnbd_migration/lib/src/edge_origin.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class InstantiateToBoundsOrigin extends EdgeOrigin {
220220
/// check.
221221
///
222222
/// Before the migration, there was no way to say `is int?`, and therefore,
223-
// `is int` should migrate to non-null int.
223+
/// `is int` should migrate to non-null int.
224224
class IsCheckMainTypeOrigin extends EdgeOrigin {
225225
IsCheckMainTypeOrigin(Source source, TypeAnnotation node)
226226
: super(source, node);

pkg/nnbd_migration/test/edge_builder_test.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,66 @@ class C {
16651665
// exception to be thrown.
16661666
}
16671667

1668+
test_constructor_withRedirectingSuperInitializer() async {
1669+
await analyze('''
1670+
class C {
1671+
C.named(int i);
1672+
}
1673+
class D extends C {
1674+
D(int j) : super.named(j);
1675+
}
1676+
''');
1677+
1678+
var namedConstructor = findElement.constructor('named', of: 'C');
1679+
var constructorType = variables.decoratedElementType(namedConstructor);
1680+
var constructorParameterType = constructorType.positionalParameters[0];
1681+
assertEdge(
1682+
decoratedTypeAnnotation('int j').node, constructorParameterType.node,
1683+
hard: true);
1684+
}
1685+
1686+
@FailingTest(
1687+
reason: 'Need to pass type arguments along in '
1688+
'EdgeBuilder.visitSuperConstructorInvocation')
1689+
test_constructor_withRedirectingSuperInitializer_withTypeArgument() async {
1690+
await analyze('''
1691+
class C<T> {
1692+
C.named(T i);
1693+
}
1694+
class D extends C<int> {
1695+
D(int j) : super.named(j);
1696+
}
1697+
''');
1698+
1699+
var namedConstructor = findElement.constructor('named', of: 'C');
1700+
var constructorType = variables.decoratedElementType(namedConstructor);
1701+
var constructorParameterType = constructorType.positionalParameters[0];
1702+
assertEdge(
1703+
decoratedTypeAnnotation('int j').node, constructorParameterType.node,
1704+
hard: true);
1705+
}
1706+
1707+
@FailingTest(
1708+
reason: 'Need to pass type arguments along in '
1709+
'EdgeBuilder.visitSuperConstructorInvocation')
1710+
test_constructor_withRedirectingSuperInitializer_withTypeVariable() async {
1711+
await analyze('''
1712+
class C<T> {
1713+
C.named(T i);
1714+
}
1715+
class D<T> extends C<T> {
1716+
D(T j) : super.named(j);
1717+
}
1718+
''');
1719+
1720+
var namedConstructor = findElement.constructor('named', of: 'C');
1721+
var constructorType = variables.decoratedElementType(namedConstructor);
1722+
var constructorParameterType = constructorType.positionalParameters[0];
1723+
assertEdge(
1724+
decoratedTypeAnnotation('int j').node, constructorParameterType.node,
1725+
hard: true);
1726+
}
1727+
16681728
test_constructorDeclaration_returnType_generic() async {
16691729
await analyze('''
16701730
class C<T, U> {

0 commit comments

Comments
 (0)