Do not expand closed subtypes in old language versions#83704
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the C# compiler’s pattern-matching exhaustiveness/type-union machinery so that closed class subtype expansion is only performed when the closed classes feature is enabled for the compilation’s language version. This prevents older language versions from treating metadata “closed” hierarchies as exhaustive sets of subtypes.
Changes:
- Add a regression test verifying that C# 14 does not treat closed classes as special for exhaustiveness, including metadata reference scenarios.
- Thread
CSharpCompilationthroughValueSetFactorytype-union factories and call sites, and gateExpandClosedSubtypesoncompilation.IsFeatureEnabled(MessageID.IDS_FeatureClosedClasses). - Update pattern-matching DAG construction and counterexample generation to use the new
TypeUnionValueSetFactoryForInput(CSharpCompilation, ...)signature.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/Compilers/CSharp/Test/CSharp15/ClosedClassesTests.cs |
Adds C# 14 exhaustiveness coverage to ensure closed subtype expansion does not happen in older language versions. |
src/Compilers/CSharp/Portable/Utilities/ValueSetFactory.cs |
Updates the type-union factory entry point to accept a CSharpCompilation for feature gating. |
src/Compilers/CSharp/Portable/Utilities/ValueSetFactory.ClosedClassTypeUnionValueSetFactory.cs |
Gates closed subtype expansion on feature availability and carries CSharpCompilation through the factory. |
src/Compilers/CSharp/Portable/Utilities/ValueSetFactory.UnionTypeTypeUnionValueSetFactory.cs |
Threads CSharpCompilation so union case handling can reuse the gated closed-subtype expansion logic. |
src/Compilers/CSharp/Portable/Binder/PatternExplainer.cs / src/Compilers/CSharp/Portable/Binder/DecisionDagBuilder.cs |
Updates call sites to pass compilation into type-union value-set queries and adjusts selected-test plumbing accordingly. |
AlekseyTs
reviewed
May 14, 2026
AlekseyTs
reviewed
May 14, 2026
AlekseyTs
reviewed
May 14, 2026
| """; | ||
|
|
||
| var comp = CreateCompilation([source1, source2, ClosedAttributeDefinition], parseOptions: TestOptions.Regular14, targetFramework: TargetFramework.Net100); | ||
| comp.VerifyDiagnostics( |
| comp0.VerifyDiagnostics(); | ||
|
|
||
| var comp1 = CreateCompilation([source2, ClosedAttributeDefinition], references: [comp0.ToMetadataReference()], parseOptions: TestOptions.Regular14, targetFramework: TargetFramework.Net100); | ||
| comp1.VerifyDiagnostics( |
AlekseyTs
reviewed
May 14, 2026
Contributor
|
Done with review pass (commit 3) #Closed |
jjonescz
approved these changes
May 15, 2026
| // (100,18): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '_' is not covered. | ||
| // return c switch | ||
| Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithArguments("_").WithLocation(100, 18)); | ||
| } |
Member
There was a problem hiding this comment.
nit: I think it would be better to test langversion next and preview too, for completness
This was referenced May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test plan: #81039
Microsoft Reviewers: Open in CodeFlow