Skip to content

Commit 40632d7

Browse files
stereotype441Commit Bot
authored and
Commit Bot
committed
Ensure that parenthesized function literals are deferred too.
When a function literal is wrapped in parentheses, it shouldn't affect how it interacts with type inference. This change ensures that parenthesized function literals are treated the same as unparenthesized ones by the logic that supports dart-lang/language#731 (improved inference for fold etc.) Change-Id: I672787a31addbfe3f3282b6e638e00b693eea46f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/243000 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent c6a061f commit 40632d7

10 files changed

+215
-3
lines changed

pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,8 +2462,12 @@ class TypeInferrerImpl implements TypeInferrer {
24622462
"invocation.");
24632463
continue;
24642464
}
2465+
Expression unparenthesizedExpression = argumentExpression;
2466+
while (unparenthesizedExpression is ParenthesizedExpression) {
2467+
unparenthesizedExpression = unparenthesizedExpression.expression;
2468+
}
24652469
if (isInferenceUpdate1Enabled &&
2466-
argumentExpression is FunctionExpression) {
2470+
unparenthesizedExpression is FunctionExpression) {
24672471
(deferredFunctionLiterals ??= []).add(new _DeferredParamInfo(
24682472
formalType: formalType,
24692473
argumentExpression: argumentExpression,
@@ -5942,8 +5946,9 @@ class _DeferredParamInfo {
59425946
/// argument.
59435947
final DartType formalType;
59445948

5945-
/// The function literal expression.
5946-
final FunctionExpression argumentExpression;
5949+
/// The argument expression (possibly wrapped in an arbitrary number of
5950+
/// ParenthesizedExpressions).
5951+
final Expression argumentExpression;
59475952

59485953
/// Indicates whether this is a named argument.
59495954
final bool isNamed;

pkg/front_end/test/spell_checking_list_code.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,7 @@ unmark
14301430
unneeded
14311431
unordered
14321432
unpaired
1433+
unparenthesized
14331434
unparsed
14341435
unpleasant
14351436
unqualified

pkg/front_end/testcases/inference_update_1/write_capture_deferral.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ withUnnamedArguments(int? i, void Function(void Function(), Object?) f) {
1717
}
1818
}
1919

20+
withUnnamedArgumentsParenthesized(int? i, void Function(void Function(), Object?) f) {
21+
if (i != null) {
22+
f((() {
23+
i = null;
24+
}), i);
25+
i;
26+
}
27+
}
28+
29+
withUnnamedArgumentsParenthesizedTwice(int? i, void Function(void Function(), Object?) f) {
30+
if (i != null) {
31+
f(((() {
32+
i = null;
33+
})), i);
34+
i;
35+
}
36+
}
37+
2038
withNamedArguments(
2139
int? i, void Function({required void Function() g, Object? x}) f) {
2240
if (i != null) {
@@ -29,6 +47,30 @@ withNamedArguments(
2947
}
3048
}
3149

50+
withNamedArgumentsParenthesized(
51+
int? i, void Function({required void Function() g, Object? x}) f) {
52+
if (i != null) {
53+
f(
54+
g: (() {
55+
i = null;
56+
}),
57+
x: i);
58+
i;
59+
}
60+
}
61+
62+
withNamedArgumentsParenthesizedTwice(
63+
int? i, void Function({required void Function() g, Object? x}) f) {
64+
if (i != null) {
65+
f(
66+
g: ((() {
67+
i = null;
68+
})),
69+
x: i);
70+
i;
71+
}
72+
}
73+
3274
withIdentical_lhs(int? i) {
3375
if (i != null) {
3476
i;

pkg/front_end/testcases/inference_update_1/write_capture_deferral.dart.textual_outline.expect

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
withUnnamedArguments(int? i, void Function(void Function(), Object?) f) {}
2+
withUnnamedArgumentsParenthesized(
3+
int? i, void Function(void Function(), Object?) f) {}
4+
withUnnamedArgumentsParenthesizedTwice(
5+
int? i, void Function(void Function(), Object?) f) {}
26
withNamedArguments(
37
int? i, void Function({required void Function() g, Object? x}) f) {}
8+
withNamedArgumentsParenthesized(
9+
int? i, void Function({required void Function() g, Object? x}) f) {}
10+
withNamedArgumentsParenthesizedTwice(
11+
int? i, void Function({required void Function() g, Object? x}) f) {}
412
withIdentical_lhs(int? i) {}
513
withIdentical_rhs(int? i) {}
614

pkg/front_end/testcases/inference_update_1/write_capture_deferral.dart.textual_outline_modelled.expect

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ withIdentical_lhs(int? i) {}
1818
withIdentical_rhs(int? i) {}
1919
withNamedArguments(
2020
int? i, void Function({required void Function() g, Object? x}) f) {}
21+
withNamedArgumentsParenthesized(
22+
int? i, void Function({required void Function() g, Object? x}) f) {}
23+
withNamedArgumentsParenthesizedTwice(
24+
int? i, void Function({required void Function() g, Object? x}) f) {}
2125
withUnnamedArguments(int? i, void Function(void Function(), Object?) f) {}
26+
withUnnamedArgumentsParenthesized(
27+
int? i, void Function(void Function(), Object?) f) {}
28+
withUnnamedArgumentsParenthesizedTwice(
29+
int? i, void Function(void Function(), Object?) f) {}

pkg/front_end/testcases/inference_update_1/write_capture_deferral.dart.weak.expect

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ static method withUnnamedArguments(core::int? i, (() → void, core::Object?)
2727
i;
2828
}
2929
}
30+
static method withUnnamedArgumentsParenthesized(core::int? i, (() → void, core::Object?) → void f) → dynamic {
31+
if(!(i == null)) {
32+
f(() → void {
33+
i = null;
34+
}, i{core::int}){(() → void, core::Object?) → void};
35+
i;
36+
}
37+
}
38+
static method withUnnamedArgumentsParenthesizedTwice(core::int? i, (() → void, core::Object?) → void f) → dynamic {
39+
if(!(i == null)) {
40+
f(() → void {
41+
i = null;
42+
}, i{core::int}){(() → void, core::Object?) → void};
43+
i;
44+
}
45+
}
3046
static method withNamedArguments(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic {
3147
if(!(i == null)) {
3248
f(g: () → void {
@@ -35,6 +51,22 @@ static method withNamedArguments(core::int? i, ({required g: () → void, x: cor
3551
i;
3652
}
3753
}
54+
static method withNamedArgumentsParenthesized(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic {
55+
if(!(i == null)) {
56+
f(g: () → void {
57+
i = null;
58+
}, x: i{core::int}){({required g: () → void, x: core::Object?}) → void};
59+
i;
60+
}
61+
}
62+
static method withNamedArgumentsParenthesizedTwice(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic {
63+
if(!(i == null)) {
64+
f(g: () → void {
65+
i = null;
66+
}, x: i{core::int}){({required g: () → void, x: core::Object?}) → void};
67+
i;
68+
}
69+
}
3870
static method withIdentical_lhs(core::int? i) → dynamic {
3971
if(!(i == null)) {
4072
i{core::int};

pkg/front_end/testcases/inference_update_1/write_capture_deferral.dart.weak.modular.expect

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ static method withUnnamedArguments(core::int? i, (() → void, core::Object?)
2727
i;
2828
}
2929
}
30+
static method withUnnamedArgumentsParenthesized(core::int? i, (() → void, core::Object?) → void f) → dynamic {
31+
if(!(i == null)) {
32+
f(() → void {
33+
i = null;
34+
}, i{core::int}){(() → void, core::Object?) → void};
35+
i;
36+
}
37+
}
38+
static method withUnnamedArgumentsParenthesizedTwice(core::int? i, (() → void, core::Object?) → void f) → dynamic {
39+
if(!(i == null)) {
40+
f(() → void {
41+
i = null;
42+
}, i{core::int}){(() → void, core::Object?) → void};
43+
i;
44+
}
45+
}
3046
static method withNamedArguments(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic {
3147
if(!(i == null)) {
3248
f(g: () → void {
@@ -35,6 +51,22 @@ static method withNamedArguments(core::int? i, ({required g: () → void, x: cor
3551
i;
3652
}
3753
}
54+
static method withNamedArgumentsParenthesized(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic {
55+
if(!(i == null)) {
56+
f(g: () → void {
57+
i = null;
58+
}, x: i{core::int}){({required g: () → void, x: core::Object?}) → void};
59+
i;
60+
}
61+
}
62+
static method withNamedArgumentsParenthesizedTwice(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic {
63+
if(!(i == null)) {
64+
f(g: () → void {
65+
i = null;
66+
}, x: i{core::int}){({required g: () → void, x: core::Object?}) → void};
67+
i;
68+
}
69+
}
3870
static method withIdentical_lhs(core::int? i) → dynamic {
3971
if(!(i == null)) {
4072
i{core::int};

pkg/front_end/testcases/inference_update_1/write_capture_deferral.dart.weak.outline.expect

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ class C extends self::B {
1414
}
1515
static method withUnnamedArguments(core::int? i, (() → void, core::Object?) → void f) → dynamic
1616
;
17+
static method withUnnamedArgumentsParenthesized(core::int? i, (() → void, core::Object?) → void f) → dynamic
18+
;
19+
static method withUnnamedArgumentsParenthesizedTwice(core::int? i, (() → void, core::Object?) → void f) → dynamic
20+
;
1721
static method withNamedArguments(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic
1822
;
23+
static method withNamedArgumentsParenthesized(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic
24+
;
25+
static method withNamedArgumentsParenthesizedTwice(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic
26+
;
1927
static method withIdentical_lhs(core::int? i) → dynamic
2028
;
2129
static method withIdentical_rhs(core::int? i) → dynamic

pkg/front_end/testcases/inference_update_1/write_capture_deferral.dart.weak.transformed.expect

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ static method withUnnamedArguments(core::int? i, (() → void, core::Object?)
2727
i;
2828
}
2929
}
30+
static method withUnnamedArgumentsParenthesized(core::int? i, (() → void, core::Object?) → void f) → dynamic {
31+
if(!(i == null)) {
32+
f(() → void {
33+
i = null;
34+
}, i{core::int}){(() → void, core::Object?) → void};
35+
i;
36+
}
37+
}
38+
static method withUnnamedArgumentsParenthesizedTwice(core::int? i, (() → void, core::Object?) → void f) → dynamic {
39+
if(!(i == null)) {
40+
f(() → void {
41+
i = null;
42+
}, i{core::int}){(() → void, core::Object?) → void};
43+
i;
44+
}
45+
}
3046
static method withNamedArguments(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic {
3147
if(!(i == null)) {
3248
f(g: () → void {
@@ -35,6 +51,22 @@ static method withNamedArguments(core::int? i, ({required g: () → void, x: cor
3551
i;
3652
}
3753
}
54+
static method withNamedArgumentsParenthesized(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic {
55+
if(!(i == null)) {
56+
f(g: () → void {
57+
i = null;
58+
}, x: i{core::int}){({required g: () → void, x: core::Object?}) → void};
59+
i;
60+
}
61+
}
62+
static method withNamedArgumentsParenthesizedTwice(core::int? i, ({required g: () → void, x: core::Object?}) → void f) → dynamic {
63+
if(!(i == null)) {
64+
f(g: () → void {
65+
i = null;
66+
}, x: i{core::int}){({required g: () → void, x: core::Object?}) → void};
67+
i;
68+
}
69+
}
3870
static method withIdentical_lhs(core::int? i) → dynamic {
3971
if(!(i == null)) {
4072
i{core::int};

tests/language/inference_update_1/write_capture_deferral_enabled_test.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ withUnnamedArguments(int? i, void Function(void Function(), Object?) f) {
2121
}
2222
}
2323

24+
withUnnamedArgumentsParenthesized(
25+
int? i, void Function(void Function(), Object?) f) {
26+
if (i != null) {
27+
f((() {
28+
i = null;
29+
}), i..expectStaticType<Exactly<int>>());
30+
i..expectStaticType<Exactly<int?>>();
31+
}
32+
}
33+
34+
withUnnamedArgumentsParenthesizedTwice(
35+
int? i, void Function(void Function(), Object?) f) {
36+
if (i != null) {
37+
f(((() {
38+
i = null;
39+
})), i..expectStaticType<Exactly<int>>());
40+
i..expectStaticType<Exactly<int?>>();
41+
}
42+
}
43+
2444
withNamedArguments(
2545
int? i, void Function({required void Function() g, Object? x}) f) {
2646
if (i != null) {
@@ -33,6 +53,30 @@ withNamedArguments(
3353
}
3454
}
3555

56+
withNamedArgumentsParenthesized(
57+
int? i, void Function({required void Function() g, Object? x}) f) {
58+
if (i != null) {
59+
f(
60+
g: (() {
61+
i = null;
62+
}),
63+
x: i..expectStaticType<Exactly<int>>());
64+
i..expectStaticType<Exactly<int?>>();
65+
}
66+
}
67+
68+
withNamedArgumentsParenthesizedTwice(
69+
int? i, void Function({required void Function() g, Object? x}) f) {
70+
if (i != null) {
71+
f(
72+
g: ((() {
73+
i = null;
74+
})),
75+
x: i..expectStaticType<Exactly<int>>());
76+
i..expectStaticType<Exactly<int?>>();
77+
}
78+
}
79+
3680
withIdentical_lhs(int? i) {
3781
if (i != null) {
3882
i..expectStaticType<Exactly<int>>();

0 commit comments

Comments
 (0)