Skip to content

Commit 25409af

Browse files
committed
[Patterns] A few bug fixes and clarifications.
- Specify runtime behavior when no cases match. Fix #2123. - Allow empty list, map, and record patterns. Fix #2441. - Clarify ambiguity between grouping and record patterns.
1 parent 989784d commit 25409af

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

working/0546-patterns/patterns-feature-specification.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ is expected.
607607
### List pattern
608608

609609
```
610-
listPattern ::= typeArguments? '[' patterns ']'
610+
listPattern ::= typeArguments? '[' patterns? ']'
611611
```
612612

613613
A list pattern matches an object that implements `List` and extracts elements by
@@ -622,7 +622,7 @@ elements. Allow capturing the rest in a variable.**
622622
### Map pattern
623623

624624
```
625-
mapPattern ::= typeArguments? '{' mapPatternEntries '}'
625+
mapPattern ::= typeArguments? '{' mapPatternEntries? '}'
626626
mapPatternEntries ::= mapPatternEntry ( ',' mapPatternEntry )* ','?
627627
mapPatternEntry ::= expression ':' pattern
628628
```
@@ -648,7 +648,7 @@ It is a compile-time error if:
648648
### Record pattern
649649

650650
```
651-
recordPattern ::= '(' patternFields ')'
651+
recordPattern ::= '(' patternFields? ')'
652652
patternFields ::= patternField ( ',' patternField )* ','?
653653
patternField ::= ( identifier? ':' )? pattern
654654
```
@@ -690,6 +690,11 @@ Field subpatterns can be in one of three forms:
690690
(:field as int)
691691
```
692692
693+
A record pattern with a single unnamed field and no trailing comma is ambiguous
694+
with a grouping pattern. In that case, it is treated as a grouping pattern. To
695+
write a record pattern that matches a single unnamed field, add a trailing
696+
comma, as you would with the corresponding record expression.
697+
693698
### Extractor pattern
694699
695700
```
@@ -1687,6 +1692,14 @@ behavior.
16871692
3. If no case pattern matched and there is a default clause, execute the
16881693
statements after it.
16891694

1695+
4. If no case matches and there is no default clause, throw a runtime
1696+
exception. *This can only occur when `null` or a legacy typed value flows
1697+
into this switch statement from another library that hasn't migrated to
1698+
[null safety][]. In fully migrated programs, exhaustiveness checking is
1699+
sound and it isn't possible to reach this runtime error.*
1700+
1701+
[null safety]: https://dart.dev/null-safety
1702+
16901703
#### Switch expression
16911704

16921705
1. Evaluate the switch value producing `v`.
@@ -1707,6 +1720,12 @@ behavior.
17071720
expression after it and yield that as the result of the entire switch
17081721
expression.
17091722

1723+
4. If no case matches and there is no default clause, throw a runtime
1724+
exception. *This can only occur when `null` or a legacy typed value flows
1725+
into this switch expression from another library that hasn't migrated to
1726+
[null safety][]. In fully migrated programs, exhaustiveness checking is
1727+
sound and it isn't possible to reach this runtime error.*
1728+
17101729
#### Pattern-if statement
17111730

17121731
1. Evaluate the `expression` producing `v`.
@@ -1958,6 +1977,15 @@ Here is one way it could be broken down into separate pieces:
19581977

19591978
## Changelog
19601979

1980+
### 2.3
1981+
1982+
- Specify that switches throw a runtime error if values from legacy libraries
1983+
flow in and break exhaustiveness checking (#2123).
1984+
1985+
- Allow empty list, map, and record patterns (#2441).
1986+
1987+
- Clarify ambiguity between grouping and record patterns.
1988+
19611989
### 2.2
19621990

19631991
- Make map patterns check length like list patterns do (#2415).

0 commit comments

Comments
 (0)