Skip to content

Commit 991e55f

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
NNBD Migrator: Fix generic super initializers
Change-Id: Icc78a248791881a97f5221ee2b5edf6314266225 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127542 Reviewed-by: Mike Fairhurst <mfairhurst@google.com> Commit-Queue: Samuel Rawlins <srawlins@google.com>
1 parent 9605cca commit 991e55f

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

pkg/nnbd_migration/lib/src/edge_builder.dart

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,11 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
799799
InstanceCreationExpression node) {
800800
var callee = node.staticElement;
801801
var typeParameters = callee.enclosingElement.typeParameters;
802-
List<DartType> typeArgumentTypes;
802+
Iterable<DartType> typeArgumentTypes;
803803
List<DecoratedType> decoratedTypeArguments;
804804
var typeArguments = node.constructorName.type.typeArguments;
805805
if (typeArguments != null) {
806-
typeArgumentTypes = typeArguments.arguments.map((t) => t.type).toList();
806+
typeArgumentTypes = typeArguments.arguments.map((t) => t.type);
807807
decoratedTypeArguments = typeArguments.arguments
808808
.map((t) => _variables.decoratedTypeAnnotation(source, t))
809809
.toList();
@@ -1267,15 +1267,28 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
12671267
SuperConstructorInvocation node) {
12681268
var callee = node.staticElement;
12691269
var nullabilityNode = NullabilityNode.forInferredType();
1270-
var createdType = DecoratedType(callee.returnType, nullabilityNode);
1270+
var class_ = node.thisOrAncestorOfType<ClassDeclaration>();
1271+
var decoratedSupertype = _decoratedClassHierarchy.getDecoratedSupertype(
1272+
class_.declaredElement, callee.enclosingElement);
1273+
var typeArguments = decoratedSupertype.typeArguments;
1274+
Iterable<DartType> typeArgumentTypes;
1275+
if (typeArguments != null) {
1276+
typeArgumentTypes = typeArguments.map((t) => t.type);
1277+
} else {
1278+
typeArgumentTypes = [];
1279+
}
1280+
var createdType = DecoratedType(callee.returnType, nullabilityNode,
1281+
typeArguments: typeArguments);
12711282
var calleeType = getOrComputeElementType(callee, targetType: createdType);
1283+
var constructorTypeParameters = callee.enclosingElement.typeParameters;
1284+
12721285
_handleInvocationArguments(
12731286
node,
12741287
node.argumentList.arguments,
1275-
null /* typeArguments */,
1276-
[] /* typeArgumentTypes */,
1288+
null /*typeArguments*/,
1289+
typeArgumentTypes,
12771290
calleeType,
1278-
[] /* constructorTypeParameters */);
1291+
constructorTypeParameters);
12791292
return null;
12801293
}
12811294

@@ -1954,15 +1967,18 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
19541967
/// Creates the necessary constraint(s) for an [ArgumentList] when invoking an
19551968
/// executable element whose type is [calleeType].
19561969
///
1970+
/// Only pass [typeArguments] or [typeArgumentTypes] depending on the use
1971+
/// case; only one will be used.
1972+
///
19571973
/// Returns the decorated return type of the invocation, after any necessary
19581974
/// substitutions.
19591975
DecoratedType _handleInvocationArguments(
19601976
AstNode node,
19611977
Iterable<AstNode> arguments,
19621978
TypeArgumentList typeArguments,
1963-
List<DartType> typeArgumentTypes,
1979+
Iterable<DartType> typeArgumentTypes,
19641980
DecoratedType calleeType,
1965-
List<TypeParameterElement> constructorTypeParameters,
1981+
Iterable<TypeParameterElement> constructorTypeParameters,
19661982
{DartType invokeType}) {
19671983
var typeFormals = constructorTypeParameters ?? calleeType.typeFormals;
19681984
if (typeFormals.isNotEmpty) {

pkg/nnbd_migration/test/edge_builder_test.dart

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

1668-
test_constructor_withRedirectingSuperInitializer() async {
1668+
test_constructor_superInitializer() async {
16691669
await analyze('''
16701670
class C {
16711671
C.named(int i);
@@ -1683,45 +1683,37 @@ class D extends C {
16831683
hard: true);
16841684
}
16851685

1686-
@FailingTest(
1687-
reason: 'Need to pass type arguments along in '
1688-
'EdgeBuilder.visitSuperConstructorInvocation')
1689-
test_constructor_withRedirectingSuperInitializer_withTypeArgument() async {
1686+
test_constructor_superInitializer_withTypeArgument() async {
16901687
await analyze('''
16911688
class C<T> {
1692-
C.named(T i);
1689+
C.named(T/*1*/ i);
16931690
}
1694-
class D extends C<int> {
1695-
D(int j) : super.named(j);
1691+
class D extends C<int/*2*/> {
1692+
D(int/*3*/ j) : super.named(j);
16961693
}
16971694
''');
16981695

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,
1696+
var nullable_t1 = decoratedTypeAnnotation('T/*1*/').node;
1697+
var nullable_int2 = decoratedTypeAnnotation('int/*2*/').node;
1698+
var nullable_int3 = decoratedTypeAnnotation('int/*3*/').node;
1699+
assertEdge(nullable_int3, substitutionNode(nullable_int2, nullable_t1),
17041700
hard: true);
17051701
}
17061702

1707-
@FailingTest(
1708-
reason: 'Need to pass type arguments along in '
1709-
'EdgeBuilder.visitSuperConstructorInvocation')
1710-
test_constructor_withRedirectingSuperInitializer_withTypeVariable() async {
1703+
test_constructor_superInitializer_withTypeVariable() async {
17111704
await analyze('''
17121705
class C<T> {
1713-
C.named(T i);
1706+
C.named(T/*1*/ i);
17141707
}
1715-
class D<T> extends C<T> {
1716-
D(T j) : super.named(j);
1708+
class D<U> extends C<U/*2*/> {
1709+
D(U/*3*/ j) : super.named(j);
17171710
}
17181711
''');
17191712

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,
1713+
var nullable_t1 = decoratedTypeAnnotation('T/*1*/').node;
1714+
var nullable_u2 = decoratedTypeAnnotation('U/*2*/').node;
1715+
var nullable_u3 = decoratedTypeAnnotation('U/*3*/').node;
1716+
assertEdge(nullable_u3, substitutionNode(nullable_u2, nullable_t1),
17251717
hard: true);
17261718
}
17271719

0 commit comments

Comments
 (0)