Clarify rules for redundant arms#1693
Conversation
- Make sure that redundancy is defined in terms of all preceding switch arms, not an arm. - Define "unguarded" in statements. - Add example to explain the rule that only unguarded switch arms apply to subsumption detection.
jskeet
left a comment
There was a problem hiding this comment.
Looks good! I may be missing something, of course - but I'd personally be fine to ship it.
Nigel-Ecma
left a comment
There was a problem hiding this comment.
A suggestion that an “Informally…” statement become a Note, a comment on another Note, otherwise looks good.
| - `P` is a var pattern and the set of patterns `Q` is *exhaustive* ([§11.4](patterns.md#114-pattern-exhaustiveness)) for the type of the pattern input value ([§11.1](patterns.md#111-general)), and either the pattern input value is not of a nullable type or some pattern in `Q` would match `null`. | ||
| - `P` is a declaration pattern with type `T` and the set of patterns `Q` is *exhaustive* for the type `T` ([§11.4](patterns.md#114-pattern-exhaustiveness)). | ||
|
|
||
| > *Note*: Only *unguarded* cases or arms ([§13.8.3](statements.md#1383-the-switch-statement)) contribute to subsumption; a *case_guard* whose expression is not a constant expression with the value `true` cannot in general be evaluated at compile time, so the corresponding case or arm is conservatively assumed not to match every input value to which its pattern is applicable. *end note* |
There was a problem hiding this comment.
I’m a little wary about explaining the restriction. Consider the example below:
bis a simple variable and so evaluating it has no side-effect, the uses on lines 436 & 437 will return the same value forb;- now consider the situation of changing the guard on line 437 to
!b, the arm becomes unreachable (as line 436 always takes precedence) and yet is not “subsumed”…
An implementation could catch that, indeed there is already a requirement that if the guard is a constant expression evaluating to true then the arm must be treated as unguarded and included in subsumption analysis. The step from there to the guard having no-side effect and repeated on more than one arm is a fairly small step.
Something to feed into the discussion on #1691 maybe?
Co-authored-by: Nigel-Ecma <6654683+Nigel-Ecma@users.noreply.github.com> Co-authored-by: Bill Wagner <wiwagn@microsoft.com>
Fixes #1656