Skip to content

Commit fa5b04d

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: build decorated types for implicit constructors of class type aliases (partial).
This CL addresses implicit constructors without parameters. Implicit constructors with parameters will take a bit more work to accomplish, and will be handled in a follow-up CL. Change-Id: Id12725712198eeeff8e4080e8fa0ca2a1eb46177 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107187 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 2ac7959 commit fa5b04d

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

pkg/nnbd_migration/lib/src/node_builder.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,23 @@ class NodeBuilder extends GeneralizingAstVisitor<DecoratedType> {
103103
node.metadata.accept(this);
104104
node.name.accept(this);
105105
node.typeParameters?.accept(this);
106-
_handleSupertypeClauses(node.declaredElement, node.superclass,
107-
node.withClause, node.implementsClause, null);
106+
var classElement = node.declaredElement;
107+
_handleSupertypeClauses(classElement, node.superclass, node.withClause,
108+
node.implementsClause, null);
109+
for (var constructorElement in classElement.constructors) {
110+
assert(constructorElement.isSynthetic);
111+
var decoratedReturnType =
112+
_createDecoratedTypeForClass(classElement, node);
113+
if (constructorElement.parameters.isNotEmpty) {
114+
_unimplemented(node,
115+
'Implicit constructor of a mixin application, with parameters');
116+
}
117+
var functionType = DecoratedType(constructorElement.type, _graph.never,
118+
returnType: decoratedReturnType,
119+
positionalParameters: [],
120+
namedParameters: {});
121+
_variables.recordDecoratedElementType(constructorElement, functionType);
122+
}
108123
return null;
109124
}
110125

pkg/nnbd_migration/test/node_builder_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ class NodeBuilderTest extends MigrationVisitorTestBase {
3131
DecoratedType decoratedTypeParameterBound(String search) => variables
3232
.decoratedElementType(findNode.typeParameter(search).declaredElement);
3333

34+
test_class_alias_synthetic_constructors_no_parameters() async {
35+
await analyze('''
36+
class C {
37+
C.a();
38+
C.b();
39+
}
40+
mixin M {}
41+
class D = C with M;
42+
''');
43+
var constructors = findElement.class_('D').constructors;
44+
expect(constructors, hasLength(2));
45+
var a = findElement.constructor('a', of: 'D');
46+
var aType = variables.decoratedElementType(a);
47+
expect(aType.type.toString(), 'D Function()');
48+
expect(aType.node, same(never));
49+
expect(aType.typeArguments, isEmpty);
50+
expect(aType.returnType.type.toString(), 'D');
51+
expect(aType.returnType.node, same(never));
52+
var b = findElement.constructor('b', of: 'D');
53+
var bType = variables.decoratedElementType(b);
54+
expect(bType.type.toString(), 'D Function()');
55+
expect(bType.node, same(never));
56+
expect(bType.typeArguments, isEmpty);
57+
expect(bType.returnType.type.toString(), 'D');
58+
expect(bType.returnType.node, same(never));
59+
}
60+
3461
test_class_with_default_constructor() async {
3562
await analyze('''
3663
class C {}

0 commit comments

Comments
 (0)