Skip to content

Commit ec0ac06

Browse files
Fix function literal ambiguity resolution. (#2974)
The implementation only prohibits function literals inside the guards of switch expressions. This change adjusts the spec to match. Fixes dart-lang/sdk#51905.
1 parent 6710976 commit ec0ac06

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

accepted/future-releases/0546-patterns/feature-specification.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Author: Bob Nystrom
44

55
Status: Accepted
66

7-
Version 2.30 (see [CHANGELOG](#CHANGELOG) at end)
7+
Version 2.31 (see [CHANGELOG](#CHANGELOG) at end)
88

99
Note: This proposal is broken into a couple of separate documents. See also
1010
[records][] and [exhaustiveness][].
@@ -1344,23 +1344,19 @@ var x = switch (obj) {
13441344
A similar ambiguity exists with function expressions in initializer lists, if
13451345
the constructor happens to be a factory constructor with `=>` for its body. We
13461346
resolve the ambiguity similarly here: Inside the `expression` part of a
1347-
`guardedPattern`, a function literal is not allowed, unless it is enclosed in
1348-
grouping operators (parentheses, square brackets, or curly braces). *Therefore,
1349-
if `=>` is encountered after `when` in a guard, the `=>` is treated as the
1350-
separator between the guard and case body. In the above example, we take the
1351-
first interpretation.*
1352-
1353-
This rule applies in all contexts where a guard can appear: switch statements,
1354-
switch expressions, if-case statements, and if-case elements. It also applies
1355-
to all function expressions, whether their body is `=>` followed by an
1356-
expression, or a block delimited by curly braces. *We could restrict this rule
1357-
to guards only in switch expressions where the ambiguity arises, and to function
1358-
literals using `=>`. But that leads to a syntactic restriction that is
1359-
context-sensitive and harder to learn. Since the rule is unusual enough as it
1360-
is, we apply it as consistently as possible. Note that the related restriction
1361-
on constructor initializers applies regardless of whether the function literal
1362-
uses `=>` or a block, even though generative constructors can't use `=>` for
1363-
their body.*
1347+
`guardedPattern` that is part of a `switchExpression`, a function literal is not
1348+
allowed, unless it is enclosed in grouping operators (parentheses, square
1349+
brackets, or curly braces). *Therefore, if `=>` is encountered after `when` in
1350+
such a guard, the `=>` is treated as the separator between the guard and case
1351+
body. In the above example, we take the first interpretation.*
1352+
1353+
This rule applies to all function expressions, whether their body is `=>`
1354+
followed by an expression, or a block delimited by curly braces. *We could
1355+
restrict this rule to function literals using `=>`. But that leads to a
1356+
syntactic restriction that is harder to implement. Note that the related
1357+
restriction on constructor initializers applies regardless of whether the
1358+
function literal uses `=>` or a block, even though generative constructors can't
1359+
use `=>` for their body.*
13641360

13651361
*The rule is applied unconditionally even if the code after `=>` is not a valid
13661362
body expression, as in:*
@@ -3508,6 +3504,12 @@ Here is one way it could be broken down into separate pieces:
35083504
35093505
## Changelog
35103506
3507+
### 2.31
3508+
3509+
- Change the technique for resolving the "function expression in guard
3510+
ambiguity" to match what was implemented (function literals are only
3511+
prohibited in guards inside switch expressions).
3512+
35113513
### 2.30
35123514
35133515
- Re-add error for map patterns to have duplicate keys.

0 commit comments

Comments
 (0)