Skip to content

Commit 1bbfe85

Browse files
kallentuCommit Queue
authored andcommitted
[cfe] Extract the lowered wildcard name for initializing formals when inferring field type.
When we introduced lowering wildcard variable names in https://dart-review.googlesource.com/c/sdk/+/373621, we missed the fact that we also need to extract the name in `finalizeInitializingFormal` so we can infer the correct type. This caused all formals to be initialized with the `dynamic` type instead. This fix makes sure that we're inferring the correct type for these wildcard variable initializing formals. Bug: #57089 Change-Id: I380bf960082dc395a55b8cbed4753b2f009af2dd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395343 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent a4ff25f commit 1bbfe85

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

pkg/front_end/lib/src/builder/formal_parameter_builder.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import '../base/scope.dart' show LookupScope;
1515
import '../kernel/body_builder.dart' show BodyBuilder;
1616
import '../kernel/body_builder_context.dart';
1717
import '../kernel/internal_ast.dart' show VariableDeclarationImpl;
18+
import '../kernel/wildcard_lowering.dart';
1819
import '../source/builder_factory.dart';
1920
import '../source/constructor_declaration.dart';
2021
import '../source/source_factory_builder.dart';
@@ -237,7 +238,8 @@ class FormalParameterBuilder extends BuilderImpl
237238
DeclarationBuilder declarationBuilder,
238239
ConstructorDeclaration constructorDeclaration,
239240
ClassHierarchyBase hierarchy) {
240-
Builder? fieldBuilder = declarationBuilder.lookupLocalMember(name);
241+
String fieldName = isWildcardLoweredFormalParameter(name) ? '_' : name;
242+
Builder? fieldBuilder = declarationBuilder.lookupLocalMember(fieldName);
241243
if (fieldBuilder is SourceFieldBuilder) {
242244
DartType fieldType = fieldBuilder.inferType(hierarchy);
243245
fieldType = constructorDeclaration.substituteFieldType(fieldType);

pkg/front_end/testcases/wildcard_variables/initializing_formals.dart.strong.expect

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ import "dart:core" as core;
1919

2020
class A extends core::Object {
2121
final field core::int _;
22-
constructor •(wildcard dynamic _#wc0#formal) → self::A
22+
constructor •(wildcard core::int _#wc0#formal) → self::A
2323
: self::A::_ = _#wc0#formal, super core::Object::•() {
2424
core::print(this.{self::A::_}{core::int});
2525
}
2626
}
2727
class InitializerListError extends core::Object {
2828
final field core::int _;
2929
final field core::int x;
30-
constructor •(wildcard dynamic _#wc1#formal) → self::InitializerListError
30+
constructor •(wildcard core::int _#wc1#formal) → self::InitializerListError
3131
: self::InitializerListError::_ = _#wc1#formal, self::InitializerListError::x = invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:15:38: Error: Can't access 'this' in a field initializer to read '_'.
3232
InitializerListError(this._) : x = _; // Error. `_` in initializer list.
3333
^", super core::Object::•()
3434
;
3535
}
3636
class MultipleThisError extends core::Object {
3737
final field core::int _;
38-
constructor •(wildcard dynamic _#wc2#formal, wildcard dynamic _#wc3#formal) → self::MultipleThisError
38+
constructor •(wildcard core::int _#wc2#formal, wildcard core::int _#wc3#formal) → self::MultipleThisError
3939
: self::MultipleThisError::_ = _#wc2#formal, final dynamic #t1 = invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:20:34: Error: '_' was already initialized by this constructor.
4040
MultipleThisError(this._, this._); // Error. Multiple `this._`.
4141
^", super core::Object::•()
@@ -45,14 +45,14 @@ class B extends core::Object {
4545
final field core::int _;
4646
final field core::int v;
4747
final field core::int w;
48-
constructor •(wildcard dynamic _#wc4#formal, core::int v, core::int w) → self::B
48+
constructor •(wildcard core::int _#wc4#formal, core::int v, core::int w) → self::B
4949
: self::B::_ = _#wc4#formal, self::B::v = v, self::B::w = w, super core::Object::•()
5050
;
5151
}
5252
class C extends self::B {
5353
final field core::int z;
54-
constructor •(dynamic x, wildcard core::int _#wc5#formal, wildcard core::int _#wc6#formal, core::int z) → self::C
55-
: self::C::z = z, assert(x{dynamic}.>(0) as{TypeError,ForDynamic} core::bool), assert(invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:32:16: Error: Undefined name '_'.
54+
constructor •(core::int x, wildcard core::int _#wc5#formal, wildcard core::int _#wc6#formal, core::int z) → self::C
55+
: self::C::z = z, assert(x.{core::num::>}(0){(core::num) → core::bool}), assert(invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:32:16: Error: Undefined name '_'.
5656
assert(_ >= 0) // Error: no `_` in scope.
5757
^"{<invalid>}.>=(0)), super self::B::•(x, _#wc5#formal, _#wc6#formal) {
5858
core::print(this.{self::B::_}{core::int});

pkg/front_end/testcases/wildcard_variables/initializing_formals.dart.strong.modular.expect

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ import "dart:core" as core;
1919

2020
class A extends core::Object {
2121
final field core::int _;
22-
constructor •(wildcard dynamic _#wc0#formal) → self::A
22+
constructor •(wildcard core::int _#wc0#formal) → self::A
2323
: self::A::_ = _#wc0#formal, super core::Object::•() {
2424
core::print(this.{self::A::_}{core::int});
2525
}
2626
}
2727
class InitializerListError extends core::Object {
2828
final field core::int _;
2929
final field core::int x;
30-
constructor •(wildcard dynamic _#wc1#formal) → self::InitializerListError
30+
constructor •(wildcard core::int _#wc1#formal) → self::InitializerListError
3131
: self::InitializerListError::_ = _#wc1#formal, self::InitializerListError::x = invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:15:38: Error: Can't access 'this' in a field initializer to read '_'.
3232
InitializerListError(this._) : x = _; // Error. `_` in initializer list.
3333
^", super core::Object::•()
3434
;
3535
}
3636
class MultipleThisError extends core::Object {
3737
final field core::int _;
38-
constructor •(wildcard dynamic _#wc2#formal, wildcard dynamic _#wc3#formal) → self::MultipleThisError
38+
constructor •(wildcard core::int _#wc2#formal, wildcard core::int _#wc3#formal) → self::MultipleThisError
3939
: self::MultipleThisError::_ = _#wc2#formal, final dynamic #t1 = invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:20:34: Error: '_' was already initialized by this constructor.
4040
MultipleThisError(this._, this._); // Error. Multiple `this._`.
4141
^", super core::Object::•()
@@ -45,14 +45,14 @@ class B extends core::Object {
4545
final field core::int _;
4646
final field core::int v;
4747
final field core::int w;
48-
constructor •(wildcard dynamic _#wc4#formal, core::int v, core::int w) → self::B
48+
constructor •(wildcard core::int _#wc4#formal, core::int v, core::int w) → self::B
4949
: self::B::_ = _#wc4#formal, self::B::v = v, self::B::w = w, super core::Object::•()
5050
;
5151
}
5252
class C extends self::B {
5353
final field core::int z;
54-
constructor •(dynamic x, wildcard core::int _#wc5#formal, wildcard core::int _#wc6#formal, core::int z) → self::C
55-
: self::C::z = z, assert(x{dynamic}.>(0) as{TypeError,ForDynamic} core::bool), assert(invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:32:16: Error: Undefined name '_'.
54+
constructor •(core::int x, wildcard core::int _#wc5#formal, wildcard core::int _#wc6#formal, core::int z) → self::C
55+
: self::C::z = z, assert(x.{core::num::>}(0){(core::num) → core::bool}), assert(invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:32:16: Error: Undefined name '_'.
5656
assert(_ >= 0) // Error: no `_` in scope.
5757
^"{<invalid>}.>=(0)), super self::B::•(x, _#wc5#formal, _#wc6#formal) {
5858
core::print(this.{self::B::_}{core::int});

pkg/front_end/testcases/wildcard_variables/initializing_formals.dart.strong.outline.expect

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,30 @@ import "dart:core" as core;
1111

1212
class A extends core::Object {
1313
final field core::int _;
14-
constructor •(wildcard dynamic _#wc0#formal) → self::A
14+
constructor •(wildcard core::int _#wc0#formal) → self::A
1515
;
1616
}
1717
class InitializerListError extends core::Object {
1818
final field core::int _;
1919
final field core::int x;
20-
constructor •(wildcard dynamic _#wc1#formal) → self::InitializerListError
20+
constructor •(wildcard core::int _#wc1#formal) → self::InitializerListError
2121
;
2222
}
2323
class MultipleThisError extends core::Object {
2424
final field core::int _;
25-
constructor •(wildcard dynamic _#wc2#formal, wildcard dynamic _#wc3#formal) → self::MultipleThisError
25+
constructor •(wildcard core::int _#wc2#formal, wildcard core::int _#wc3#formal) → self::MultipleThisError
2626
;
2727
}
2828
class B extends core::Object {
2929
final field core::int _;
3030
final field core::int v;
3131
final field core::int w;
32-
constructor •(wildcard dynamic _#wc4#formal, core::int v, core::int w) → self::B
32+
constructor •(wildcard core::int _#wc4#formal, core::int v, core::int w) → self::B
3333
;
3434
}
3535
class C extends self::B {
3636
final field core::int z;
37-
constructor •(dynamic x, wildcard core::int _#wc5#formal, wildcard core::int _#wc6#formal, core::int z) → self::C
37+
constructor •(core::int x, wildcard core::int _#wc5#formal, wildcard core::int _#wc6#formal, core::int z) → self::C
3838
: self::C::z = z
3939
;
4040
}

pkg/front_end/testcases/wildcard_variables/initializing_formals.dart.strong.transformed.expect

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ import "dart:core" as core;
1919

2020
class A extends core::Object {
2121
final field core::int _;
22-
constructor •(wildcard dynamic _#wc0#formal) → self::A
22+
constructor •(wildcard core::int _#wc0#formal) → self::A
2323
: self::A::_ = _#wc0#formal, super core::Object::•() {
2424
core::print(this.{self::A::_}{core::int});
2525
}
2626
}
2727
class InitializerListError extends core::Object {
2828
final field core::int _;
2929
final field core::int x;
30-
constructor •(wildcard dynamic _#wc1#formal) → self::InitializerListError
30+
constructor •(wildcard core::int _#wc1#formal) → self::InitializerListError
3131
: self::InitializerListError::_ = _#wc1#formal, self::InitializerListError::x = invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:15:38: Error: Can't access 'this' in a field initializer to read '_'.
3232
InitializerListError(this._) : x = _; // Error. `_` in initializer list.
3333
^", super core::Object::•()
3434
;
3535
}
3636
class MultipleThisError extends core::Object {
3737
final field core::int _;
38-
constructor •(wildcard dynamic _#wc2#formal, wildcard dynamic _#wc3#formal) → self::MultipleThisError
38+
constructor •(wildcard core::int _#wc2#formal, wildcard core::int _#wc3#formal) → self::MultipleThisError
3939
: self::MultipleThisError::_ = _#wc2#formal, final dynamic #t1 = invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:20:34: Error: '_' was already initialized by this constructor.
4040
MultipleThisError(this._, this._); // Error. Multiple `this._`.
4141
^", super core::Object::•()
@@ -45,14 +45,14 @@ class B extends core::Object {
4545
final field core::int _;
4646
final field core::int v;
4747
final field core::int w;
48-
constructor •(wildcard dynamic _#wc4#formal, core::int v, core::int w) → self::B
48+
constructor •(wildcard core::int _#wc4#formal, core::int v, core::int w) → self::B
4949
: self::B::_ = _#wc4#formal, self::B::v = v, self::B::w = w, super core::Object::•()
5050
;
5151
}
5252
class C extends self::B {
5353
final field core::int z;
54-
constructor •(dynamic x, wildcard core::int _#wc5#formal, wildcard core::int _#wc6#formal, core::int z) → self::C
55-
: self::C::z = z, assert(x{dynamic}.>(0) as{TypeError,ForDynamic} core::bool), assert(invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:32:16: Error: Undefined name '_'.
54+
constructor •(core::int x, wildcard core::int _#wc5#formal, wildcard core::int _#wc6#formal, core::int z) → self::C
55+
: self::C::z = z, assert(x.{core::num::>}(0){(core::num) → core::bool}), assert(invalid-expression "pkg/front_end/testcases/wildcard_variables/initializing_formals.dart:32:16: Error: Undefined name '_'.
5656
assert(_ >= 0) // Error: no `_` in scope.
5757
^"{<invalid>}.>=(0)), super self::B::•(x, _#wc5#formal, _#wc6#formal) {
5858
core::print(this.{self::B::_}{core::int});

0 commit comments

Comments
 (0)