Skip to content

Add RequireNamedArgumentsAttribute - #138159

Open
semirhamid wants to merge 5 commits into
dotnet:mainfrom
semirhamid:issue-132924
Open

semirhamid wants to merge 5 commits into
dotnet:mainfrom
semirhamid:issue-132924

Conversation

@semirhamid

@semirhamid semirhamid commented Nov 23, 2026 •

Copy link
Copy Markdown

Adds System.Diagnostics.CodeAnalysis.RequireNamedArgumentsAttribute, matching the API shape approved in the API review comment on the issue.

  • Implementation in System.Private.CoreLib, following SetsRequiredMembersAttribute.
  • Entry in the System.Runtime reference assembly. It matches GenAPI's output for this type; GenAPI's unrelated changes were left out.
  • Tests for construction and for the AttributeUsage targets, AllowMultiple and Inherited.

This PR covers the attribute only. The C# analyzer is out of scope and will follow separately.

Validation (macOS arm64, Xcode 27.3 beta)

  • ./build.sh -subset clr+libs -rc Release: succeeded, 0 warnings, 0 errors.
  • ./build.sh clr.corelib+clr.nativecorelib+libs.pretest -rc Release: succeeded.
  • RequireNamedArgumentsAttributeTests: 2 run, 2 passed. The rest of the System.Runtime tests were not run locally.

Contributes to #132924

@azure-pipelines

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

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 23, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@semirhamid

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@jkotas
jkotas requested review from T-Gro and a balanced review from Copilot November 24, 2026 01:33

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

Unavailable label-event history prevents verifying who applied api-approved and when, leaving the repository’s required API-approval check incomplete.

No open issues 🎉

What changed in this PR

Adds System.Diagnostics.CodeAnalysis.RequireNamedArgumentsAttribute to shared CoreLib and the System.Runtime public contract, contributing to #132924. This PR provides the attribute metadata; analyzer enforcement is deferred to separate work.

Changes:

  • Defines the sealed attribute, its supported targets, and XML documentation.
  • Includes it in shared CoreLib builds and the System.Runtime reference assembly.
  • Adds tests for construction, allowed targets, multiplicity, and inheritance settings.
File Description
src/​libraries/​System.Runtime/​tests/​System.Runtime.Tests/​System/​Diagnostics/​CodeAnalysis/​RequireNamedArgumentsAttributeTests.cs Tests construction and attribute usage metadata.
src/​libraries/​System.Runtime/​tests/​System.Runtime.Tests/​System.Runtime.Tests.csproj Includes the new tests in the test project.
src/​libraries/​System.Runtime/​ref/​System.Runtime.cs Adds the public reference declaration.
src/​libraries/​System.Private.CoreLib/​src/​System/​Diagnostics/​CodeAnalysis/​RequireNamedArgumentsAttribute.cs Implements and documents the attribute.
src/​libraries/​System.Private.CoreLib/​src/​System.Private.CoreLib.Shared.projitems Includes the implementation in shared CoreLib builds.

Review effort: Balanced

@T-Gro T-Gro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Was this tested with the compiler please? If so, how?
(I don't insist for the test to land in the repo as well, but would be nice to see it in action at least as a PR comment)

@semirhamid

Copy link
Copy Markdown
Author

Was this tested with the compiler please? If so, how? (I don't insist for the test to land in the repo as well, but would be nice to see it in action at least as a PR comment)

Yes, I tested it with both the F# and C# compilers. I built this branch locally and compiled a few small sample projects against the reference assemblies from that build, using the SDK in the repo's .dotnet folder (12.0.100-alpha.1.26557.102, F# 11.2 by default) and the samples don't define the attribute themselves, so it's the one from System.Runtime.

and on the F# side calling an attributed method or constructor with positional arguments gives FS3923 and the same calls with named arguments compile fine.
It behaves the same when the method or constructor is defined in C# and called from F#, even thoug I didn't get any diagnostic for indexers, properties, parameters, delegates or curried methods.

I also compiled the same code against the SDK's own reference pack, and there it fails because the attribute doesn't exist.

On the other hand for the C# side, the attribute can be applied to a method, constructor, property, indexer, delegate and parameter, and putting it on a class gives CS0592.
Nothing is enforced at C# call sites yet since the analyzer will come in a separate PR.

I only compiled the samples, I didn't run them. The project files and the full compiler output are here if you want to look or rerun it:
https://gist.github.com/semirhamid/3a05b79b9354658550ed37861b5ffffc

@jeffhandley

Copy link
Copy Markdown
Member

/ba-g Known test errors, unrelated DeadLetter, and unrelated networking timeout

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants