Skip to content

Commit 6980a1b

Browse files
johnniwintherCommit Bot
authored andcommitted
[kernel] Add VariableDeclaration.hasDeclaredInitializer
The computation of default values on super parameters requires the notion of a declared initializer. By adding this as a flag to VariableDeclarations the implementation can now be normalized across parameter from source and dill. TEST=existing Change-Id: Ic980e68b569e3bdab38d2c7c7e4374e0c931a87b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/240403 Reviewed-by: Aske Simon Christensen <askesc@google.com> Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> Commit-Queue: Johnni Winther <johnniwinther@google.com>
1 parent d162c63 commit 6980a1b

File tree

332 files changed

+2048
-1318
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

332 files changed

+2048
-1318
lines changed

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,14 +1589,6 @@ class VariableDeclarationImpl extends VariableDeclaration {
15891589
/// the kernel.
15901590
final bool isImplicitlyTyped;
15911591

1592-
/// True if the initializer was specified by the programmer.
1593-
///
1594-
/// Note that the variable might have a synthesized initializer expression,
1595-
/// so `hasDeclaredInitializer == false` doesn't imply `initializer == null`.
1596-
/// For instance, for duplicate variable names, an invalid expression is set
1597-
/// as the initializer of the second variable.
1598-
final bool hasDeclaredInitializer;
1599-
16001592
// TODO(ahe): Remove this field. We can get rid of it by recording closure
16011593
// mutation in [BodyBuilder].
16021594
final int functionNestingLevel;
@@ -1625,7 +1617,7 @@ class VariableDeclarationImpl extends VariableDeclaration {
16251617

16261618
VariableDeclarationImpl(String? name, this.functionNestingLevel,
16271619
{this.forSyntheticToken: false,
1628-
this.hasDeclaredInitializer: false,
1620+
bool hasDeclaredInitializer: false,
16291621
Expression? initializer,
16301622
DartType? type,
16311623
bool isFinal: false,
@@ -1648,15 +1640,15 @@ class VariableDeclarationImpl extends VariableDeclaration {
16481640
isCovariantByDeclaration: isCovariantByDeclaration,
16491641
isLate: isLate,
16501642
isRequired: isRequired,
1651-
isLowered: isLowered);
1643+
isLowered: isLowered,
1644+
hasDeclaredInitializer: hasDeclaredInitializer);
16521645

16531646
VariableDeclarationImpl.forEffect(Expression initializer)
16541647
: forSyntheticToken = false,
16551648
functionNestingLevel = 0,
16561649
isImplicitlyTyped = false,
16571650
isLocalFunction = false,
16581651
isStaticLate = false,
1659-
hasDeclaredInitializer = true,
16601652
super.forValue(initializer);
16611653

16621654
VariableDeclarationImpl.forValue(Expression initializer)
@@ -1665,7 +1657,6 @@ class VariableDeclarationImpl extends VariableDeclaration {
16651657
isImplicitlyTyped = true,
16661658
isLocalFunction = false,
16671659
isStaticLate = false,
1668-
hasDeclaredInitializer = true,
16691660
super.forValue(initializer);
16701661

16711662
// The synthesized local getter function for a lowered late variable.

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class DelayedDefaultValueCloner {
225225
synthesizedParameterType, SubtypeCheckMode.withNullabilities)) {
226226
_cloneInitializer(originalParameter, synthesizedParameter);
227227
} else {
228+
synthesizedParameter.hasDeclaredInitializer = false;
228229
if (synthesizedParameterType.isPotentiallyNonNullable) {
229230
_libraryBuilder.addProblem(
230231
templateOptionalSuperParameterWithoutInitializer.withArguments(
@@ -260,10 +261,14 @@ class TypeDependency {
260261
for (int i = 0; i < original.function!.positionalParameters.length; i++) {
261262
VariableDeclaration synthesizedParameter =
262263
synthesized.function!.positionalParameters[i];
263-
VariableDeclaration constructorParameter =
264+
VariableDeclaration originalParameter =
264265
original.function!.positionalParameters[i];
265266
synthesizedParameter.type =
266-
substitution.substituteType(constructorParameter.type);
267+
substitution.substituteType(originalParameter.type);
268+
if (!synthesizedParameter.hasDeclaredInitializer) {
269+
synthesizedParameter.hasDeclaredInitializer =
270+
originalParameter.hasDeclaredInitializer;
271+
}
267272
}
268273
for (int i = 0; i < original.function!.namedParameters.length; i++) {
269274
VariableDeclaration synthesizedParameter =
@@ -272,6 +277,10 @@ class TypeDependency {
272277
original.function!.namedParameters[i];
273278
synthesizedParameter.type =
274279
substitution.substituteType(originalParameter.type);
280+
if (!synthesizedParameter.hasDeclaredInitializer) {
281+
synthesizedParameter.hasDeclaredInitializer =
282+
originalParameter.hasDeclaredInitializer;
283+
}
275284
}
276285
if (copyReturnType) {
277286
synthesized.function!.returnType =

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@ class KernelTarget extends TargetImplementation {
10011001
VariableDeclaration copy = new VariableDeclaration(formal.name,
10021002
isFinal: formal.isFinal,
10031003
isConst: formal.isConst,
1004+
hasDeclaredInitializer: formal.hasDeclaredInitializer,
10041005
type: const UnknownType());
10051006
if (!hasTypeDependency && formal.type is! UnknownType) {
10061007
copy.type = substitution.substituteType(formal.type);

pkg/front_end/lib/src/fasta/source/source_constructor_builder.dart

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,10 @@ class DeclaredSourceConstructorBuilder extends SourceFunctionBuilderImpl
330330
}
331331

332332
Constructor superTarget;
333-
List<FormalParameterBuilder>? superFormals;
334333
FunctionNode? superConstructorFunction;
335-
if (superTargetBuilder is DeclaredSourceConstructorBuilder) {
336-
superTarget = superTargetBuilder.constructor;
337-
superFormals = superTargetBuilder.formals!;
338-
} else if (superTargetBuilder is DillConstructorBuilder) {
334+
if (superTargetBuilder != null) {
339335
superTarget = superTargetBuilder.constructor;
340336
superConstructorFunction = superTargetBuilder.function;
341-
if (superTargetBuilder is SyntheticSourceConstructorBuilder) {
342-
superFormals = superTargetBuilder.formals;
343-
}
344337
} else {
345338
// The error in this case should be reported elsewhere. Here we perform a
346339
// simple recovery.
@@ -351,53 +344,17 @@ class DeclaredSourceConstructorBuilder extends SourceFunctionBuilderImpl
351344
List<bool> positionalSuperFormalHasInitializer = [];
352345
Map<String, DartType?> namedSuperFormalType = {};
353346
Map<String, bool> namedSuperFormalHasInitializer = {};
354-
// TODO(johnniwinther): Clean this up when [VariableDeclaration] has a
355-
// `hasDeclaredInitializer` flag.
356-
if (superFormals != null && superConstructorFunction != null) {
357-
for (VariableDeclaration formal
358-
in superConstructorFunction.positionalParameters) {
359-
positionalSuperFormalType.add(formal.type);
360-
}
361-
for (VariableDeclaration formal
362-
in superConstructorFunction.namedParameters) {
363-
namedSuperFormalType[formal.name!] = formal.type;
364-
}
365-
for (FormalParameterBuilder formal in superFormals) {
366-
if (formal.isPositional) {
367-
positionalSuperFormalHasInitializer
368-
.add(formal.hasDeclaredInitializer);
369-
} else {
370-
namedSuperFormalHasInitializer[formal.name] =
371-
formal.hasDeclaredInitializer;
372-
}
373-
}
374-
} else if (superFormals != null) {
375-
for (FormalParameterBuilder formal in superFormals) {
376-
if (formal.isPositional) {
377-
positionalSuperFormalType.add(formal.variable?.type);
378-
positionalSuperFormalHasInitializer
379-
.add(formal.hasDeclaredInitializer);
380-
} else {
381-
namedSuperFormalType[formal.name] = formal.variable?.type;
382-
namedSuperFormalHasInitializer[formal.name] =
383-
formal.hasDeclaredInitializer;
384-
}
385-
}
386-
} else if (superConstructorFunction != null) {
387-
for (VariableDeclaration formal
388-
in superConstructorFunction.positionalParameters) {
389-
positionalSuperFormalType.add(formal.type);
390-
positionalSuperFormalHasInitializer.add(formal.initializer != null);
391-
}
392-
for (VariableDeclaration formal
393-
in superConstructorFunction.namedParameters) {
394-
namedSuperFormalType[formal.name!] = formal.type;
395-
namedSuperFormalHasInitializer[formal.name!] =
396-
formal.initializer != null;
397-
}
398-
} else {
399-
// The error is reported elsewhere.
400-
return performRecoveryForErroneousCase();
347+
348+
for (VariableDeclaration formal
349+
in superConstructorFunction.positionalParameters) {
350+
positionalSuperFormalType.add(formal.type);
351+
positionalSuperFormalHasInitializer.add(formal.hasDeclaredInitializer);
352+
}
353+
for (VariableDeclaration formal
354+
in superConstructorFunction.namedParameters) {
355+
namedSuperFormalType[formal.name!] = formal.type;
356+
namedSuperFormalHasInitializer[formal.name!] =
357+
formal.hasDeclaredInitializer;
401358
}
402359

403360
int superInitializingFormalIndex = -1;
@@ -423,9 +380,12 @@ class DeclaredSourceConstructorBuilder extends SourceFunctionBuilderImpl
423380
positionalSuperFormalType.length);
424381
if (superInitializingFormalIndex <
425382
positionalSuperFormalHasInitializer.length) {
426-
formal.hasDeclaredInitializer = hasImmediatelyDeclaredInitializer ||
427-
positionalSuperFormalHasInitializer[
428-
superInitializingFormalIndex];
383+
if (formal.isOptional) {
384+
formal.hasDeclaredInitializer =
385+
hasImmediatelyDeclaredInitializer ||
386+
positionalSuperFormalHasInitializer[
387+
superInitializingFormalIndex];
388+
}
429389
correspondingSuperFormalType =
430390
positionalSuperFormalType[superInitializingFormalIndex];
431391
if (!hasImmediatelyDeclaredInitializer &&
@@ -439,8 +399,11 @@ class DeclaredSourceConstructorBuilder extends SourceFunctionBuilderImpl
439399
}
440400
} else {
441401
if (namedSuperFormalHasInitializer[formal.name] != null) {
442-
formal.hasDeclaredInitializer = hasImmediatelyDeclaredInitializer ||
443-
namedSuperFormalHasInitializer[formal.name]!;
402+
if (formal.isOptional) {
403+
formal.hasDeclaredInitializer =
404+
hasImmediatelyDeclaredInitializer ||
405+
namedSuperFormalHasInitializer[formal.name]!;
406+
}
444407
correspondingSuperFormalType = namedSuperFormalType[formal.name];
445408
if (!hasImmediatelyDeclaredInitializer && !formal.isRequiredNamed) {
446409
(namedSuperParameters ??= <String>[]).add(formal.name);
@@ -457,6 +420,7 @@ class DeclaredSourceConstructorBuilder extends SourceFunctionBuilderImpl
457420
}
458421
formal.variable!.type = type ?? const DynamicType();
459422
}
423+
formal.variable!.hasDeclaredInitializer = formal.hasDeclaredInitializer;
460424
}
461425
}
462426

pkg/front_end/testcases/const_functions/const_functions_simple_invocations.dart.weak.outline.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ static const field core::int multi = self::multiFn(1);
1919
static const field core::int multi2 = self::multiFn(2);
2020
static method binaryFn(core::int a, core::int b) → core::int
2121
;
22-
static method optionalFn(core::int c, [core::int d]) → core::int
22+
static method optionalFn(core::int c, [has-declared-initializer core::int d]) → core::int
2323
;
24-
static method namedFn(core::int e, {core::int f}) → core::int
24+
static method namedFn(core::int e, {has-declared-initializer core::int f}) → core::int
2525
;
2626
static method typeFn<T extends core::Object? = dynamic>(self::typeFn::T% x) → self::typeFn::T%
2727
;

pkg/front_end/testcases/constructor_tearoffs/bound_checks_in_type_literals.dart.weak.outline.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class C<T extends core::num> extends core::Object {
2121
static field core::Type test3;
2222
static method test() → dynamic
2323
;
24-
static method test2([core::Type t]) → dynamic
24+
static method test2([has-declared-initializer core::Type t]) → dynamic
2525
;
2626
static method main() → dynamic
2727
;

pkg/front_end/testcases/constructor_tearoffs/inferred_constructor_tear_off.dart.weak.outline.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ static method testInferred() → dynamic
2626
;
2727
static method expect(dynamic expected, dynamic actual) → dynamic
2828
;
29-
static method throws(() → dynamic f, {core::bool inSoundModeOnly}) → dynamic
29+
static method throws(() → dynamic f, {has-declared-initializer core::bool inSoundModeOnly}) → dynamic
3030
;

pkg/front_end/testcases/constructor_tearoffs/lowering/const_redirect.dart.weak.outline.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class Class extends core::Object /*hasConstConstructor*/ {
3030
const constructor named({core::List<self::Const> constants = const <self::Const>[const self::_ConstImpl::•<dynamic>(), const self::_ConstImpl::•<dynamic>(), const self::_ConstImpl::•<core::String>()]}) → self::Class
3131
: self::Class::constants = constants, super core::Object::•()
3232
;
33-
static method _#new#tearOff({core::List<self::Const> constants}) → self::Class
33+
static method _#new#tearOff({has-declared-initializer core::List<self::Const> constants}) → self::Class
3434
return new self::Class::•(constants: constants);
35-
static method _#named#tearOff({core::List<self::Const> constants}) → self::Class
35+
static method _#named#tearOff({has-declared-initializer core::List<self::Const> constants}) → self::Class
3636
return new self::Class::named(constants: constants);
3737
}
3838
abstract class Const extends core::Object {

pkg/front_end/testcases/constructor_tearoffs/lowering/constructor_tear_off.dart.weak.outline.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ static method testArgs() → dynamic
5454
;
5555
static method expect(dynamic expected, dynamic actual) → dynamic
5656
;
57-
static method throws(() → dynamic f, {core::bool inSoundModeOnly}) → dynamic
57+
static method throws(() → dynamic f, {has-declared-initializer core::bool inSoundModeOnly}) → dynamic
5858
;

pkg/front_end/testcases/constructor_tearoffs/lowering/constructor_tear_off_default_values.dart.weak.outline.expect

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ class Class1 extends core::Object {
66
final field core::int field;
77
constructor •([core::int field = 42]) → self::Class1
88
;
9-
static method _#new#tearOff([core::int field]) → self::Class1
9+
static method _#new#tearOff([has-declared-initializer core::int field]) → self::Class1
1010
return new self::Class1::•(field);
1111
}
1212
class Class2 extends core::Object {
1313
final field core::int field;
1414
constructor •({core::int field = 42}) → self::Class2
1515
;
16-
static method _#new#tearOff({core::int field}) → self::Class2
16+
static method _#new#tearOff({has-declared-initializer core::int field}) → self::Class2
1717
return new self::Class2::•(field: field);
1818
}
1919
static final field core::bool inSoundMode;
@@ -23,5 +23,5 @@ static method testDefaultValues() → void
2323
;
2424
static method expect(dynamic expected, dynamic actual) → dynamic
2525
;
26-
static method throws(() → dynamic f, {core::bool inSoundModeOnly}) → dynamic
26+
static method throws(() → dynamic f, {has-declared-initializer core::bool inSoundModeOnly}) → dynamic
2727
;

0 commit comments

Comments
 (0)