Skip to content

Commit 76c38fb

Browse files
chloestefantsovaCommit Bot
authored and
Commit Bot
committed
[cfe] Disentangle type inference and default value copying in super-parameters
Part of #47525 Change-Id: Ia8f14aa5995f50719bc95d8b26c3bd5a40d6f967 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/229148 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent 2a34453 commit 76c38fb

10 files changed

+195
-33
lines changed

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

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -357,45 +357,42 @@ class DeclaredSourceConstructorBuilder extends SourceFunctionBuilderImpl
357357
if (formal.isSuperInitializingFormal) {
358358
superInitializingFormalIndex++;
359359
bool hasImmediatelyDeclaredInitializer = formal.hasDeclaredInitializer;
360-
if (formal.type == null) {
361-
DartType? type;
362-
363-
if (formal.isPositional) {
364-
if (superInitializingFormalIndex < superFormals.length) {
365-
FormalParameterBuilder correspondingSuperFormal =
366-
superFormals[superInitializingFormalIndex];
367-
formal.hasDeclaredInitializer =
368-
hasImmediatelyDeclaredInitializer ||
369-
correspondingSuperFormal.hasDeclaredInitializer;
370-
if (!hasImmediatelyDeclaredInitializer) {
371-
(positionalSuperParameters ??= <int>[]).add(formalIndex);
372-
}
373-
type = correspondingSuperFormal.variable!.type;
374-
} else {
375-
// TODO(cstefantsova): Report an error.
360+
361+
FormalParameterBuilder? correspondingSuperFormal;
362+
363+
if (formal.isPositional) {
364+
if (superInitializingFormalIndex < superFormals.length) {
365+
correspondingSuperFormal =
366+
superFormals[superInitializingFormalIndex];
367+
formal.hasDeclaredInitializer = hasImmediatelyDeclaredInitializer ||
368+
correspondingSuperFormal.hasDeclaredInitializer;
369+
if (!hasImmediatelyDeclaredInitializer) {
370+
(positionalSuperParameters ??= <int>[]).add(formalIndex);
376371
}
377372
} else {
378-
FormalParameterBuilder? correspondingSuperFormal;
379-
for (FormalParameterBuilder superFormal in superFormals) {
380-
if (superFormal.isNamed && superFormal.name == formal.name) {
381-
correspondingSuperFormal = superFormal;
382-
break;
383-
}
373+
// TODO(cstefantsova): Report an error.
374+
}
375+
} else {
376+
for (FormalParameterBuilder superFormal in superFormals) {
377+
if (superFormal.isNamed && superFormal.name == formal.name) {
378+
correspondingSuperFormal = superFormal;
379+
break;
384380
}
381+
}
385382

386-
if (correspondingSuperFormal != null) {
387-
formal.hasDeclaredInitializer =
388-
hasImmediatelyDeclaredInitializer ||
389-
correspondingSuperFormal.hasDeclaredInitializer;
390-
if (!hasImmediatelyDeclaredInitializer) {
391-
(namedSuperParameters ??= <String>[]).add(formal.name);
392-
}
393-
type = correspondingSuperFormal.variable!.type;
394-
} else {
395-
// TODO(cstefantsova): Report an error.
383+
if (correspondingSuperFormal != null) {
384+
formal.hasDeclaredInitializer = hasImmediatelyDeclaredInitializer ||
385+
correspondingSuperFormal.hasDeclaredInitializer;
386+
if (!hasImmediatelyDeclaredInitializer) {
387+
(namedSuperParameters ??= <String>[]).add(formal.name);
396388
}
389+
} else {
390+
// TODO(cstefantsova): Report an error.
397391
}
392+
}
398393

394+
if (formal.type == null) {
395+
DartType? type = correspondingSuperFormal?.variable?.type;
399396
if (substitution.isNotEmpty && type != null) {
400397
type = substitute(type, substitution);
401398
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
class S {
6+
int s1;
7+
int s2;
8+
S([this.s1 = 1, this.s2 = 2]);
9+
}
10+
11+
class C extends S {
12+
int c1;
13+
C(this.c1, [int super.s1, int x = 0, int super.s2]);
14+
}
15+
16+
main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class S extends core::Object {
6+
field core::int s1;
7+
field core::int s2;
8+
constructor •([core::int s1 = #C1, core::int s2 = #C2]) → self::S
9+
: self::S::s1 = s1, self::S::s2 = s2, super core::Object::•()
10+
;
11+
}
12+
class C extends self::S {
13+
field core::int c1;
14+
constructor •(core::int c1, [dynamic s1 = #C1, core::int x = #C3, dynamic s2 = #C2]) → self::C
15+
: self::C::c1 = c1, super self::S::•(s1, s2)
16+
;
17+
}
18+
static method main() → dynamic {}
19+
20+
constants {
21+
#C1 = 1
22+
#C2 = 2
23+
#C3 = 0
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class S extends core::Object {
6+
field core::int s1;
7+
field core::int s2;
8+
constructor •([core::int s1 = #C1, core::int s2 = #C2]) → self::S
9+
: self::S::s1 = s1, self::S::s2 = s2, super core::Object::•()
10+
;
11+
}
12+
class C extends self::S {
13+
field core::int c1;
14+
constructor •(core::int c1, [dynamic s1 = #C1, core::int x = #C3, dynamic s2 = #C2]) → self::C
15+
: self::C::c1 = c1, super self::S::•(s1, s2)
16+
;
17+
}
18+
static method main() → dynamic {}
19+
20+
constants {
21+
#C1 = 1
22+
#C2 = 2
23+
#C3 = 0
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class S {
2+
int s1;
3+
int s2;
4+
S([this.s1 = 1, this.s2 = 2]);
5+
}
6+
class C extends S {
7+
int c1;
8+
C(this.c1, [int super.s1, int x = 0, int super.s2]);
9+
}
10+
main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class S extends core::Object {
6+
field core::int s1;
7+
field core::int s2;
8+
constructor •([core::int s1 = #C1, core::int s2 = #C2]) → self::S
9+
: self::S::s1 = s1, self::S::s2 = s2, super core::Object::•()
10+
;
11+
}
12+
class C extends self::S {
13+
field core::int c1;
14+
constructor •(core::int c1, [dynamic s1 = #C1, core::int x = #C3, dynamic s2 = #C2]) → self::C
15+
: self::C::c1 = c1, super self::S::•(s1, s2)
16+
;
17+
}
18+
static method main() → dynamic {}
19+
20+
constants {
21+
#C1 = 1
22+
#C2 = 2
23+
#C3 = 0
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class S extends core::Object {
6+
field core::int s1;
7+
field core::int s2;
8+
constructor •([core::int s1 = #C1, core::int s2 = #C2]) → self::S
9+
: self::S::s1 = s1, self::S::s2 = s2, super core::Object::•()
10+
;
11+
}
12+
class C extends self::S {
13+
field core::int c1;
14+
constructor •(core::int c1, [dynamic s1 = #C1, core::int x = #C3, dynamic s2 = #C2]) → self::C
15+
: self::C::c1 = c1, super self::S::•(s1, s2)
16+
;
17+
}
18+
static method main() → dynamic {}
19+
20+
constants {
21+
#C1 = 1
22+
#C2 = 2
23+
#C3 = 0
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class S extends core::Object {
6+
field core::int s1;
7+
field core::int s2;
8+
constructor •([core::int s1 = 1, core::int s2 = 2]) → self::S
9+
;
10+
}
11+
class C extends self::S {
12+
field core::int c1;
13+
constructor •(core::int c1, [dynamic s1 = 1, core::int x = 0, dynamic s2 = 2]) → self::C
14+
: self::C::c1 = c1
15+
;
16+
}
17+
static method main() → dynamic
18+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class S extends core::Object {
6+
field core::int s1;
7+
field core::int s2;
8+
constructor •([core::int s1 = #C1, core::int s2 = #C2]) → self::S
9+
: self::S::s1 = s1, self::S::s2 = s2, super core::Object::•()
10+
;
11+
}
12+
class C extends self::S {
13+
field core::int c1;
14+
constructor •(core::int c1, [dynamic s1 = #C1, core::int x = #C3, dynamic s2 = #C2]) → self::C
15+
: self::C::c1 = c1, super self::S::•(s1, s2)
16+
;
17+
}
18+
static method main() → dynamic {}
19+
20+
constants {
21+
#C1 = 1
22+
#C2 = 2
23+
#C3 = 0
24+
}

pkg/front_end/testcases/textual_outline.status

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ general/issue42997: FormatterCrash
100100
general/issue43363: FormatterCrash
101101
general/issue45490: FormatterCrash
102102
general/issue45700.crash: FormatterCrash
103-
general/issue_46886: FormatterCrash
104103
general/issue47495: FormatterCrash
105104
general/issue47728_2: FormatterCrash
106105
general/issue47728_3: FormatterCrash
106+
general/issue_46886: FormatterCrash
107107
general/macro_class: FormatterCrash
108108
general/many_errors: FormatterCrash
109109
general/missing_prefix_name: FormatterCrash
@@ -219,6 +219,7 @@ super_parameters/simple: FormatterCrash
219219
super_parameters/simple_inference: FormatterCrash
220220
super_parameters/simple_named_super_parameters: FormatterCrash
221221
super_parameters/simple_positional_super_parameters: FormatterCrash
222+
super_parameters/super_parameters_with_types_and_default_values: FormatterCrash
222223
super_parameters/synthesized_super_constructor_with_parameters: FormatterCrash
223224
super_parameters/type_alias_in_supertype: FormatterCrash
224225
super_parameters/typed_super_parameter: FormatterCrash

0 commit comments

Comments
 (0)