Skip to content

Commit ccfb7cf

Browse files
committed
[patterns] Non-exhaustive switches on enum types are an error.
1 parent 8bcb3a4 commit ccfb7cf

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,24 +1854,17 @@ All other types are not exhaustive. Then:
18541854
is to throw an error and most Dart users prefer to catch those kinds of
18551855
mistakes at compile time.*
18561856

1857-
* If the static type of the matched value in a switch statement is an
1858-
exhaustive type and the cases are not exhaustive then:
1859-
1860-
* It is a compile-time warning if the type is an enum type or nullable
1861-
enum type. *This is for backwards compatibility.*
1862-
1863-
*Since enum types are handled specially, why define them as exhaustive?
1864-
This is so that they don't prevent a record type containing them as
1865-
being considered exhaustive. We want it to be a compile-time error if a
1866-
switch on a record type with a sealed type field and an enum type field
1867-
is not exhaustive. We even treat it as an error for switching on a
1868-
record containing only enum types. Since there are no records in the
1869-
wild, this is not a breaking change.*
1870-
1871-
* Else it is a compile-time error.
1857+
* It is a compile-time error if the cases in a switch statement are not
1858+
exhaustive and the static type of the matched value is an exhaustive type.
18721859

18731860
[exhaustiveness]: https://github.com/dart-lang/language/blob/master/working/0546-patterns/exhaustiveness.md
18741861

1862+
**Breaking change:** Currently, a non-exhaustive switch on an enum type is only
1863+
a warning. This promotes it to an error. Also, switches on `bool` do not have to
1864+
be exhaustive. In practice, many users already treat warnings as errors, and
1865+
switches on `bool` are rare and unidiomatic. This breaking change would only
1866+
apply to code that has opted into the language version where this ships.
1867+
18751868
## Runtime semantics
18761869

18771870
### Execution
@@ -2538,6 +2531,11 @@ Here is one way it could be broken down into separate pieces:
25382531

25392532
## Changelog
25402533

2534+
### 2.8
2535+
2536+
- Upgrade non-exhaustive switch statements on enums from a warning to an
2537+
error (#2474).
2538+
25412539
### 2.7
25422540

25432541
- Clarify that relational and extractor patterns can call extension members

0 commit comments

Comments
 (0)