Skip to content

Unsafe evolution: handle new() constraint#82647

Merged
jjonescz merged 9 commits into
dotnet:mainfrom
jjonescz:Unsafe-17-NewConstraint
Mar 12, 2026
Merged

Unsafe evolution: handle new() constraint#82647
jjonescz merged 9 commits into
dotnet:mainfrom
jjonescz:Unsafe-17-NewConstraint

Conversation

@jjonescz

@jjonescz jjonescz commented Mar 6, 2026

Copy link
Copy Markdown
Member

Test plan: #81207

This should resolve the following items from the test plan:

  • alias directives
  • using static directives

@jjonescz jjonescz force-pushed the Unsafe-17-NewConstraint branch from b6c8be3 to 9175dcc Compare March 6, 2026 19:40
@jjonescz jjonescz marked this pull request as ready for review March 10, 2026 15:59
@jjonescz jjonescz requested a review from a team as a code owner March 10, 2026 15:59
@jjonescz jjonescz requested review from 333fred and jcouv March 10, 2026 15:59
@jcouv jcouv mentioned this pull request Mar 10, 2026
4 tasks
@333fred 333fred self-assigned this Mar 10, 2026
Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs
{
awaitableType = originalBinder.Compilation.GetWellKnownType(WellKnownType.System_Threading_Tasks_ValueTask);
if (awaitableType is not null) AssertNotUnsafeMemberAccess(awaitableType);
if (awaitableType is not null) originalBinder.ReportDiagnosticsIfUnsafeMemberAccess(diagnostics, awaitableType, syntax);

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.

Is this change useful? I think we should remove it.

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.

nit: it's a bit odd to keep the assertion for just this one well-known type. I'd either remove the assertion of push it up in GetWellKnownType

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.

I will remove it, thanks.

diagnostics.DiagnosticBag is { } bag &&
alias.Alias.TryGetFirstLocation() is { } location)
{
compilation.GetBinder(alias.UsingDirective.NamespaceOrType).ReportDiagnosticsIfUnsafeMemberAccess(bag, immediateTarget, location);

@jcouv jcouv Mar 10, 2026

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.

We've been checking for unsafe member access at the same time that we check for Obsolete. Why is this different? Are we missing an obsolete check? #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.

Yes, we are missing an obsolete check, see #19430. This is not the only place where unsafe evolution might have more checks, see #82128.

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.

Thanks. nit: consider commenting/linking

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.

I have linked this PR from that second tracking issue

""",
caller: """
using static D1<C>;
using static unsafe D2<C>;

@jcouv jcouv Mar 10, 2026

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.

This test shows that unsafe on using is still meaningful. We may want to tweak the spec:

Since pointer types are now safe, an unsafe modifier on declarations without bodies does not have a meaning anymore. Hence unsafe on the following declarations will produce a warning:

using static,
using alias.
``` #Pending

@jjonescz jjonescz Mar 11, 2026

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.

Yes, that's already mentioned in this open question: https://github.com/dotnet/csharplang/blob/909fd31b4b51cc4df6dba8c616b8b7440b5a7a07/proposals/unsafe-evolution.md#new-constraint-and-usings

I will change the main body of the speclet once this PR is merged to match the implementation.

Comment thread src/Compilers/CSharp/Test/CSharp15/UnsafeEvolutionTests.cs

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

Done with review pass (commit 6)

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

LGTM Thanks (commit 8)

@333fred 333fred 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.

No additional comments beyond Julien's test suggestions.

Was reviewing an old commit.

@jjonescz jjonescz enabled auto-merge (squash) March 12, 2026 11:15
@jjonescz jjonescz merged commit 836ffa6 into dotnet:main Mar 12, 2026
23 of 24 checks passed
@jjonescz jjonescz deleted the Unsafe-17-NewConstraint branch March 12, 2026 14:01
@dotnet-policy-service dotnet-policy-service Bot added this to the Next milestone Mar 12, 2026
@jjonescz jjonescz modified the milestones: Next, 18.6 Mar 31, 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.

3 participants