Skip to content

Allow non-public constructors with a single parameter in a union declaration#83788

Merged
AlekseyTs merged 1 commit into
dotnet:mainfrom
AlekseyTs:Unions_60
May 20, 2026
Merged

Allow non-public constructors with a single parameter in a union declaration#83788
AlekseyTs merged 1 commit into
dotnet:mainfrom
AlekseyTs:Unions_60

Conversation

@AlekseyTs

@AlekseyTs AlekseyTs commented May 19, 2026

Copy link
Copy Markdown
Contributor

Align behavior with the spec.

Microsoft Reviewers: Open in CodeFlow

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.

Pull request overview

This change updates C# compiler validation for union declarations so that only public explicitly-declared instance constructors with a single parameter are rejected, allowing non-public (for example, private/internal) single-parameter constructors as permitted by the language rules.

Changes:

  • Restrict CS9374 (ERR_InstanceCtorWithOneParameterInUnion) to public one-parameter instance constructors in union declarations.
  • Update and extend UnionsTests to:
    • Make the invalid constructors explicitly public (to keep testing CS9374).
    • Add coverage showing private/internal one-parameter constructors are allowed when properly chained.
    • Add new negative cases showing non-public one-parameter constructors still must use the required this initializer rule (CS9375).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs Changes the union-member validation to only report CS9374 for public one-parameter instance constructors (excluding synthesized union ctors).
src/Compilers/CSharp/Test/CSharp15/UnionsTests.cs Adjusts expected diagnostics and adds test cases covering allowed non-public one-parameter constructors and required initializer diagnostics.

@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@jjonescz, @dotnet/roslyn-compiler For a second review

@AlekseyTs AlekseyTs merged commit 5285c3f into dotnet:main May 20, 2026
26 checks passed
@dotnet-policy-service dotnet-policy-service Bot added this to the Next milestone May 20, 2026
@dibarbet dibarbet modified the milestones: Next, 18.8 May 26, 2026
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.

5 participants