Skip to content

Commit c696942

Browse files
authored
Move warnings to a separate section and suggest some more. (#2813)
1 parent 92fa381 commit c696942

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

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

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

55
Status: Accepted
66

7-
Version 2.22 (see [CHANGELOG](#CHANGELOG) at end)
7+
Version 2.23 (see [CHANGELOG](#CHANGELOG) at end)
88

99
Note: This proposal is broken into a couple of separate documents. See also
1010
[records][] and [exhaustiveness][].
@@ -2460,10 +2460,39 @@ All other types are not always-exhaustive. Then:
24602460
other option is to throw an error and most Dart users prefer to catch those
24612461
kinds of mistakes at compile time.*
24622462

2463-
* The language doesn't specify or mandate this, but implementations are
2464-
encouraged to report a warning if a case in a switch statement, collection
2465-
element, or expression is unreachable because all values it can match are
2466-
also matched by preceding cases.
2463+
**Breaking change:** Currently, a non-exhaustive switch on an enum type is only
2464+
a warning. This promotes it to an error. Also, switches on `bool` do not
2465+
currently have to be exhaustive. In practice, many users already treat warnings
2466+
as errors, and switches on `bool` are rare and unidiomatic. This breaking change
2467+
would only apply to code that has opted into the language version where this
2468+
ships.
2469+
2470+
### Warnings
2471+
2472+
We don't want to mandate warnings in the language specification, but the user
2473+
experience of a language feature is holistic and we try to design it thinking
2474+
about the entire tooling experience. With that in mind, implementations are
2475+
encouraged to report a static warning when:
2476+
2477+
* The left branch of an `||` pattern will always match, since it means the
2478+
right branch will never be used. A pattern will always match if it's an
2479+
untyped wildcard, a variable whose type is a supertype of the matched value
2480+
type, etc.
2481+
2482+
* Either branch of an `&&` is an untyped wildcard, since it has no effect and
2483+
can be removed.
2484+
2485+
* A cast pattern casts to a supertype of the matched value type.
2486+
2487+
* A null-check or null-assert pattern has a non-nullable matched value type.
2488+
2489+
* A constant pattern's constant has primitive equality and is matched against
2490+
a type that it can never be equal to, like matching a String against the
2491+
constant pattern `3`.
2492+
2493+
* A case in a switch statement, collection element, or expression is
2494+
unreachable because all values it can match are also matched by preceding
2495+
cases. (The exhaustiveness algorithm can be used to determine this.)
24672496

24682497
*We make this a warning and not an error because it's harmless dead code.
24692498
Also, in some cases the exhaustiveness analysis may not be very precise and
@@ -2472,12 +2501,8 @@ All other types are not always-exhaustive. Then:
24722501
that default case may become unreachable. If that happens, we don't want
24732502
this to be a breaking change.*
24742503

2475-
**Breaking change:** Currently, a non-exhaustive switch on an enum type is only
2476-
a warning. This promotes it to an error. Also, switches on `bool` do not
2477-
currently have to be exhaustive. In practice, many users already treat warnings
2478-
as errors, and switches on `bool` are rare and unidiomatic. This breaking change
2479-
would only apply to code that has opted into the language version where this
2480-
ships.
2504+
In general, these all have the property that they describe dead code that
2505+
provably can be removed without changing the behavior of the program.
24812506

24822507
## Runtime semantics
24832508

@@ -3436,6 +3461,10 @@ Here is one way it could be broken down into separate pieces:
34363461
34373462
## Changelog
34383463
3464+
### 2.23
3465+
3466+
- Suggest warnings that implementations may want to report.
3467+
34393468
### 2.22
34403469
34413470
- In list patterns, don't call `v[e]` if the corresponding subpattern is a

0 commit comments

Comments
 (0)