Skip to content

Clarify rules for redundant arms#1693

Merged
BillWagner merged 3 commits into
dotnet:draft-v8from
BillWagner:redundant-switch-arms
Jun 10, 2026
Merged

Clarify rules for redundant arms#1693
BillWagner merged 3 commits into
dotnet:draft-v8from
BillWagner:redundant-switch-arms

Conversation

@BillWagner

Copy link
Copy Markdown
Member

Fixes #1656

  • 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.

- 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 jskeet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I may be missing something, of course - but I'd personally be fine to ship it.

@RexJaeschke RexJaeschke removed their request for review May 17, 2026 15:30
@BillWagner BillWagner added the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Jun 3, 2026

@Nigel-Ecma Nigel-Ecma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A suggestion that an “Informally…” statement become a Note, a comment on another Note, otherwise looks good.

Comment thread standard/patterns.md Outdated
Comment thread standard/patterns.md Outdated
- `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*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m a little wary about explaining the restriction. Consider the example below:

  • b is a simple variable and so evaluating it has no side-effect, the uses on lines 436 & 437 will return the same value for b;
  • 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?

Comment thread standard/patterns.md Outdated
Co-authored-by: Nigel-Ecma <6654683+Nigel-Ecma@users.noreply.github.com>
Co-authored-by: Bill Wagner <wiwagn@microsoft.com>
@BillWagner BillWagner merged commit c95084d into dotnet:draft-v8 Jun 10, 2026
6 checks passed
@BillWagner BillWagner deleted the redundant-switch-arms branch June 10, 2026 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

meeting: discuss This issue should be discussed at the next TC49-TG2 meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix description around it being an error if a *switch_expression_arm* is redundant

3 participants