Skip to content

[release/stable] Emit correct IL for exhaustive switch statements that use a separate Dag for lowering - #86251

Merged
AlekseyTs merged 1 commit into
release/stablefrom
backport/pr-85873-to-release/stable
Oct 30, 2026
Merged

AlekseyTs merged 1 commit into
release/stablefrom
backport/pr-85873-to-release/stable

Conversation

@github-actions

@github-actions github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #85873 to release/stable

/cc @AlekseyTs

Customer Impact

Reported by internal and external users of Unions feature.

Regression

  • Yes
  • No

Testing

18 unit tests were added for various scenarios.
The issue was missed previously because we were missing a test for an exhaustive switch statement utilizing separate decision Dag for lowering, which was used at the end of a method. In previous version, a separate lowering Dag was used only for some scenarios involving list patterns. When unions feature is used, usage of a lowering Dag is fairly common.

Risk

Low. We now have a pretty comprehensive set of unit-tests covering the scenario. Two senior compiler developers reviewed the change. Copilot review didn't find any issues.

Microsoft Reviewers: Open in CodeFlow

@github-actions
github-actions Bot requested a review from a team as a code owner October 29, 2026 18:53
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI 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.

🔵 Needs a closer look

Compiler-owner validation is needed because this release-branch change affects control-flow lowering and emitted IL, but its Debug/Release execution and IL-verification tests could not be run in the review environment.

No open issues 🎉

What changed in this PR

Backports #85873 to release/stable, correcting C# IL emission when an exhaustive switch statement uses a separate decision graph for lowering. The fix adds a terminating throw for unmatched paths introduced by lowering while preserving explicit goto default behavior.

Changes:

  • Detects fallback labels that become reachable only during lowering and emits an exception throw.
  • Shares exception-selection and throw-construction logic with switch-expression lowering.
  • Adds 18 regression tests for unions and list patterns, plus an assertion that an existing case emits no unnecessary throw.
File Description
src/​Compilers/​Test/​Utilities/​CSharp/​CSharpTestBase.cs Updates the decision-graph helper call for the new output parameter.
src/​Compilers/​CSharp/​Test/​Emit3/​Semantics/​PatternMatchingTests_ListPatterns.cs Adds exhaustive list-pattern tests and IL assertions.
src/​Compilers/​CSharp/​Test/​CSharp15/​UnionsTests.cs Adds 16 union-switch regression tests covering control flow and exception fallbacks.
src/​Compilers/​CSharp/​Portable/​Symbols/​Synthesized/​SynthesizedThrowSwitchExpressionExceptionMethod.cs Extracts reusable throw construction with the unmatched value.
src/​Compilers/​CSharp/​Portable/​Symbols/​Synthesized/​SynthesizedParameterlessThrowMethod.cs Extracts reusable parameterless exception throw construction.
src/​Compilers/​CSharp/​Portable/​Lowering/​LocalRewriter/​LocalRewriter_SwitchExpression.cs Uses the shared exception-selection strategy.
src/​Compilers/​CSharp/​Portable/​Lowering/​LocalRewriter/​LocalRewriter_PatternSwitchStatement.cs Emits throws at fallback labels introduced by lowering.
src/​Compilers/​CSharp/​Portable/​Lowering/​LocalRewriter/​LocalRewriter_BasePatternSwitchLocalRewriter.cs Centralizes exception selection and input-conversion checks.
src/​Compilers/​CSharp/​Portable/​BoundTree/​BoundSwitchStatement.cs Identifies lowering-only fallback paths without redirecting explicit default-label jumps.

Review effort: Balanced

@AlekseyTs
AlekseyTs merged commit a8dd230 into release/stable Oct 30, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants