Skip to content

Unsafe evolution: avoid errors inside nameof#84325

Open
jjonescz wants to merge 2 commits into
dotnet:mainfrom
jjonescz:Unsafe-40-NameOfField
Open

Unsafe evolution: avoid errors inside nameof#84325
jjonescz wants to merge 2 commits into
dotnet:mainfrom
jjonescz:Unsafe-40-NameOfField

Conversation

@jjonescz

@jjonescz jjonescz commented Jun 29, 2026

Copy link
Copy Markdown
Member

Test plan: #81207

Unsafe errors were already not reported for most members inside nameof (because binding there doesn't get to the point of "use" where those diagnostics are reported) except for fields.

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 PR adjusts the C# binder so “unsafe member access” diagnostics are not produced while binding inside a nameof(...) argument, and adds regression tests to ensure unsafe declarations/members (including fields) don’t trigger updated memory-safety diagnostics in that context.

Changes:

  • Skip ReportDiagnosticsIfUnsafeMemberAccess when Binder.IsInsideNameof is true, preventing unsafe-member-operation diagnostics from being reported in nameof arguments.
  • Add new compiler tests covering nameof over unsafe alias/local function/type declarations and member names, validating behavior under both legacy and updated memory safety rules.

Reviewed changes

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

File Description
src/Compilers/CSharp/Portable/Binder/Binder_Unsafe.cs Early-exits unsafe member access diagnostic reporting when binding within nameof.
src/Compilers/CSharp/Test/CSharp15/UnsafeEvolutionTests.cs Adds regression coverage ensuring unsafe-related diagnostics aren’t reported inside nameof (including for fields), across rule modes.
@jjonescz jjonescz marked this pull request as ready for review June 30, 2026 08:16
@jjonescz jjonescz requested a review from a team as a code owner June 30, 2026 08:16
@jjonescz jjonescz requested review from 333fred and AlekseyTs June 30, 2026 08:17
Comment thread src/Compilers/CSharp/Test/CSharp15/UnsafeEvolutionTests.cs

private void ReportDiagnosticsIfUnsafeMemberAccess<T>(DiagnosticBag diagnostics, Symbol symbol, T arg, Func<T, Location?> location)
{
if (IsInsideNameof)

@AlekseyTs AlekseyTs Jun 30, 2026

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.

IsInsideNameof

It looks like we have BinderFlags.SuppressUnsafeDiagnostics. Also, it looks like in one of the recent changes we set BinderFlags.UnsafeRegion flag on a binder to achieve the same goal for params collections declarations. I think we should simply stick to setting proper BinderFlags for NameOfBinder. Also, we probably should standardize on BinderFlags.SuppressUnsafeDiagnostics for the purpose of suppressing diagnostics. #Closed

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's what I looked at first, but it doesn't seem to be as simple in NameofBinder. It computes the fact whether we are in nameof operator lazily, via some non-trivial logic, hence I don't think we can just statically set a flag when creating the binder.

@AlekseyTs

Copy link
Copy Markdown
Contributor

Done with review pass (commit 1)

@jjonescz jjonescz requested review from 333fred and AlekseyTs July 1, 2026 09:25

@AlekseyTs AlekseyTs 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.

LGTM (commit 2).

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