Skip to content

Commit 2f7a1ce

Browse files
[cfe] Handle synthesized super initializers in super-parameters
Part of #47525 Change-Id: I768dce70465a77a94609af03b21c64d4bf4a1386 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/223800 Reviewed-by: Johnni Winther <[email protected]>
1 parent 9ca2bcd commit 2f7a1ce

File tree

6 files changed

+88
-126
lines changed

6 files changed

+88
-126
lines changed

pkg/front_end/lib/src/fasta/kernel/body_builder.dart

Lines changed: 83 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,78 +1049,6 @@ class BodyBuilder extends ScopeListener<JumpTarget>
10491049
];
10501050
}
10511051

1052-
if (libraryBuilder.enableSuperParametersInLibrary) {
1053-
if (initializers.isNotEmpty && initializers.last is SuperInitializer) {
1054-
SuperInitializer superInitializer =
1055-
initializers.last as SuperInitializer;
1056-
Arguments arguments = superInitializer.arguments;
1057-
1058-
List<VariableDeclaration>? positionalSuperParameters;
1059-
List<VariableDeclaration>? namedSuperParameters;
1060-
1061-
List<FormalParameterBuilder>? formals =
1062-
(member as ConstructorBuilder).formals;
1063-
if (formals != null) {
1064-
for (FormalParameterBuilder formal in formals) {
1065-
if (formal.isSuperInitializingFormal) {
1066-
if (formal.isNamed) {
1067-
(namedSuperParameters ??= <VariableDeclaration>[])
1068-
.add(formal.variable!);
1069-
} else {
1070-
(positionalSuperParameters ??= <VariableDeclaration>[])
1071-
.add(formal.variable!);
1072-
}
1073-
}
1074-
}
1075-
1076-
if (positionalSuperParameters != null) {
1077-
if (arguments.positional.isNotEmpty) {
1078-
addProblem(fasta.messagePositionalSuperParametersAndArguments,
1079-
arguments.fileOffset, noLength,
1080-
context: <LocatedMessage>[
1081-
fasta.messageSuperInitializerParameter.withLocation(uri,
1082-
positionalSuperParameters.first.fileOffset, noLength)
1083-
]);
1084-
} else {
1085-
for (VariableDeclaration positional
1086-
in positionalSuperParameters) {
1087-
arguments.positional.add(
1088-
new VariableGetImpl(positional, forNullGuardedAccess: false)
1089-
..fileOffset = positional.fileOffset);
1090-
}
1091-
}
1092-
}
1093-
if (namedSuperParameters != null) {
1094-
// TODO(cstefantsova): Report name conflicts.
1095-
for (VariableDeclaration named in namedSuperParameters) {
1096-
arguments.named.add(new NamedExpression(
1097-
named.name!,
1098-
new VariableGetImpl(named, forNullGuardedAccess: false)
1099-
..fileOffset = named.fileOffset)
1100-
..fileOffset = named.fileOffset);
1101-
}
1102-
}
1103-
}
1104-
1105-
LocatedMessage? message = checkArgumentsForFunction(
1106-
superInitializer.target.function,
1107-
arguments,
1108-
arguments.fileOffset, <TypeParameter>[]);
1109-
if (message != null) {
1110-
initializers[initializers.length - 1] = buildInvalidInitializer(
1111-
buildUnresolvedError(
1112-
forest.createNullLiteral(superInitializer.fileOffset),
1113-
constructorNameForDiagnostics(
1114-
superInitializer.target.name.text),
1115-
arguments,
1116-
superInitializer.fileOffset,
1117-
isSuper: true,
1118-
message: message,
1119-
kind: UnresolvedKind.Constructor));
1120-
}
1121-
}
1122-
}
1123-
11241052
_initializers ??= <Initializer>[];
11251053
_initializers!.addAll(initializers);
11261054
}
@@ -1748,15 +1676,87 @@ class BodyBuilder extends ScopeListener<JumpTarget>
17481676
typeInferrer.flowAnalysis.declare(parameter.variable!, true);
17491677
}
17501678
}
1751-
if (_initializers != null) {
1679+
1680+
List<Expression>? positionalSuperParametersAsArguments;
1681+
List<NamedExpression>? namedSuperParametersAsArguments;
1682+
if (formals != null) {
1683+
for (FormalParameterBuilder formal in formals) {
1684+
if (formal.isSuperInitializingFormal) {
1685+
if (formal.isNamed) {
1686+
(namedSuperParametersAsArguments ??= <NamedExpression>[]).add(
1687+
new NamedExpression(
1688+
formal.name,
1689+
new VariableGetImpl(formal.variable!,
1690+
forNullGuardedAccess: false)
1691+
..fileOffset = formal.charOffset)
1692+
..fileOffset = formal.charOffset);
1693+
} else {
1694+
(positionalSuperParametersAsArguments ??= <Expression>[]).add(
1695+
new VariableGetImpl(formal.variable!,
1696+
forNullGuardedAccess: false)
1697+
..fileOffset = formal.charOffset);
1698+
}
1699+
}
1700+
}
1701+
}
1702+
1703+
List<Initializer>? initializers = _initializers;
1704+
if (initializers != null) {
1705+
if (libraryBuilder.enableSuperParametersInLibrary) {
1706+
if (initializers.isNotEmpty && initializers.last is SuperInitializer) {
1707+
SuperInitializer superInitializer =
1708+
initializers.last as SuperInitializer;
1709+
Arguments arguments = superInitializer.arguments;
1710+
1711+
if (positionalSuperParametersAsArguments != null) {
1712+
if (arguments.positional.isNotEmpty) {
1713+
addProblem(fasta.messagePositionalSuperParametersAndArguments,
1714+
arguments.fileOffset, noLength,
1715+
context: <LocatedMessage>[
1716+
fasta.messageSuperInitializerParameter.withLocation(
1717+
uri,
1718+
(positionalSuperParametersAsArguments.first
1719+
as VariableGet)
1720+
.variable
1721+
.fileOffset,
1722+
noLength)
1723+
]);
1724+
} else {
1725+
arguments.positional.addAll(positionalSuperParametersAsArguments);
1726+
}
1727+
}
1728+
if (namedSuperParametersAsArguments != null) {
1729+
// TODO(cstefantsova): Report name conflicts.
1730+
arguments.named.addAll(namedSuperParametersAsArguments);
1731+
}
1732+
1733+
LocatedMessage? message = checkArgumentsForFunction(
1734+
superInitializer.target.function,
1735+
arguments,
1736+
arguments.fileOffset, <TypeParameter>[]);
1737+
if (message != null) {
1738+
initializers[initializers.length - 1] = buildInvalidInitializer(
1739+
buildUnresolvedError(
1740+
forest.createNullLiteral(superInitializer.fileOffset),
1741+
constructorNameForDiagnostics(
1742+
superInitializer.target.name.text),
1743+
arguments,
1744+
superInitializer.fileOffset,
1745+
isSuper: true,
1746+
message: message,
1747+
kind: UnresolvedKind.Constructor));
1748+
}
1749+
}
1750+
}
1751+
17521752
Map<Initializer, InitializerInferenceResult> inferenceResults =
17531753
<Initializer, InitializerInferenceResult>{};
1754-
for (Initializer initializer in _initializers!) {
1754+
for (Initializer initializer in initializers) {
17551755
inferenceResults[initializer] =
17561756
typeInferrer.inferInitializer(this, initializer);
17571757
}
17581758
if (!builder.isExternal) {
1759-
for (Initializer initializer in _initializers!) {
1759+
for (Initializer initializer in initializers) {
17601760
builder.addInitializer(initializer, this,
17611761
inferenceResult: inferenceResults[initializer]!);
17621762
}
@@ -1772,7 +1772,14 @@ class BodyBuilder extends ScopeListener<JumpTarget>
17721772
/// >unless the enclosing class is class Object.
17731773
Constructor? superTarget = lookupConstructor(emptyName, isSuper: true);
17741774
Initializer initializer;
1775-
Arguments arguments = forest.createArgumentsEmpty(noLocation);
1775+
Arguments arguments;
1776+
if (libraryBuilder.enableSuperParametersInLibrary) {
1777+
arguments = forest.createArguments(
1778+
noLocation, positionalSuperParametersAsArguments ?? <Expression>[],
1779+
named: namedSuperParametersAsArguments);
1780+
} else {
1781+
arguments = forest.createArgumentsEmpty(noLocation);
1782+
}
17761783
if (superTarget == null ||
17771784
checkArgumentsForFunction(superTarget.function, arguments,
17781785
builder.charOffset, const <TypeParameter>[]) !=
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
library /*isNonNullableByDefault*/;
2-
//
3-
// Problems in library:
4-
//
5-
// pkg/front_end/testcases/super_parameters/simple.dart:11:3: Error: The superclass, 'A', has no unnamed constructor that takes no arguments.
6-
// B(super.foo);
7-
// ^
8-
//
92
import self as self;
103
import "dart:core" as core;
114

@@ -17,9 +10,7 @@ class A extends core::Object {
1710
}
1811
class B extends self::A {
1912
constructor •(dynamic foo) → self::B
20-
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/super_parameters/simple.dart:11:3: Error: The superclass, 'A', has no unnamed constructor that takes no arguments.
21-
B(super.foo);
22-
^"
13+
: super self::A::•(foo)
2314
;
2415
}
2516
static method main() → dynamic {}
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
library /*isNonNullableByDefault*/;
2-
//
3-
// Problems in library:
4-
//
5-
// pkg/front_end/testcases/super_parameters/simple.dart:11:3: Error: The superclass, 'A', has no unnamed constructor that takes no arguments.
6-
// B(super.foo);
7-
// ^
8-
//
92
import self as self;
103
import "dart:core" as core;
114

@@ -17,9 +10,7 @@ class A extends core::Object {
1710
}
1811
class B extends self::A {
1912
constructor •(dynamic foo) → self::B
20-
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/super_parameters/simple.dart:11:3: Error: The superclass, 'A', has no unnamed constructor that takes no arguments.
21-
B(super.foo);
22-
^"
13+
: super self::A::•(foo)
2314
;
2415
}
2516
static method main() → dynamic {}
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
library /*isNonNullableByDefault*/;
2-
//
3-
// Problems in library:
4-
//
5-
// pkg/front_end/testcases/super_parameters/simple.dart:11:3: Error: The superclass, 'A', has no unnamed constructor that takes no arguments.
6-
// B(super.foo);
7-
// ^
8-
//
92
import self as self;
103
import "dart:core" as core;
114

@@ -17,9 +10,7 @@ class A extends core::Object {
1710
}
1811
class B extends self::A {
1912
constructor •(dynamic foo) → self::B
20-
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/super_parameters/simple.dart:11:3: Error: The superclass, 'A', has no unnamed constructor that takes no arguments.
21-
B(super.foo);
22-
^"
13+
: super self::A::•(foo)
2314
;
2415
}
2516
static method main() → dynamic {}
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
library /*isNonNullableByDefault*/;
2-
//
3-
// Problems in library:
4-
//
5-
// pkg/front_end/testcases/super_parameters/simple.dart:11:3: Error: The superclass, 'A', has no unnamed constructor that takes no arguments.
6-
// B(super.foo);
7-
// ^
8-
//
92
import self as self;
103
import "dart:core" as core;
114

@@ -17,9 +10,7 @@ class A extends core::Object {
1710
}
1811
class B extends self::A {
1912
constructor •(dynamic foo) → self::B
20-
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/super_parameters/simple.dart:11:3: Error: The superclass, 'A', has no unnamed constructor that takes no arguments.
21-
B(super.foo);
22-
^"
13+
: super self::A::•(foo)
2314
;
2415
}
2516
static method main() → dynamic {}
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
library /*isNonNullableByDefault*/;
2-
//
3-
// Problems in library:
4-
//
5-
// pkg/front_end/testcases/super_parameters/simple.dart:11:3: Error: The superclass, 'A', has no unnamed constructor that takes no arguments.
6-
// B(super.foo);
7-
// ^
8-
//
92
import self as self;
103
import "dart:core" as core;
114

@@ -17,9 +10,7 @@ class A extends core::Object {
1710
}
1811
class B extends self::A {
1912
constructor •(dynamic foo) → self::B
20-
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/super_parameters/simple.dart:11:3: Error: The superclass, 'A', has no unnamed constructor that takes no arguments.
21-
B(super.foo);
22-
^"
13+
: super self::A::•(foo)
2314
;
2415
}
2516
static method main() → dynamic {}

0 commit comments

Comments
 (0)