Skip to content

Rewrite method infos earlier in ref safety analysis#79447

Merged
333fred merged 16 commits intodotnet:mainfrom
333fred:refsafety-rewriteearlier
Aug 8, 2025
Merged

Rewrite method infos earlier in ref safety analysis#79447
333fred merged 16 commits intodotnet:mainfrom
333fred:refsafety-rewriteearlier

Conversation

@333fred
Copy link
Member

@333fred 333fred commented Jul 17, 2025

Ref safety analysis did the same thing that nullable analysis did with respect rewriting new extensions into calling form, but did not do it immediately on seeing a call. We now do this as soon as the call is done, giving a more uniform handling of the analysis. Fixes #79654.

Relates to test plan #76130

Ref safety analysis did the same thing that nullable analysis did with respect rewriting new extensions into calling form, but did not do it immediately on seeing a call. We now do this as soon as the call is done, giving a more uniform handling of the analysis.
@333fred 333fred force-pushed the refsafety-rewriteearlier branch from 2dfaf21 to b6ecfc8 Compare July 18, 2025 18:52
@333fred 333fred marked this pull request as ready for review July 22, 2025 18:46
@333fred 333fred requested a review from a team as a code owner July 22, 2025 18:46
@333fred
Copy link
Member Author

333fred commented Jul 22, 2025

@dotnet/roslyn-compiler @AlekseyTs @jcouv for reviews

333fred added 8 commits July 24, 2025 17:54
…rlier

* upstream/main: (217 commits)
  Fix tests
  Fix tests
  Fix tests
  Fix tests
  Fix tests
  Fix tests
  Reduce allocations during CommonCompletionItem.Create (dotnet#79591)
  Fix tests
  Fix tests
  Fix tests
  Fix tests
  Add test
  Fix tests
  Add work item
  Fix eol handling on the last token in a file when formatting code actions
  remove unchecked values from tests
  [main] Source code updates from dotnet/dotnet (dotnet#79599)
  Nullable extensions: Add assertion to AsMemberOfType and handle failures (dotnet#79428)
  Avoid adding dependency on System.Threading.Channels to InteractiveHost (dotnet#79594)
  Update debugger contracts to 18.0.0-beta.25353.1 (dotnet#79277)
  ...
@RikkiGibson RikkiGibson self-assigned this Jul 25, 2025
@RikkiGibson
Copy link
Member

We should add a test for the scenario in #79654

@333fred
Copy link
Member Author

333fred commented Aug 5, 2025

@jjonescz @RikkiGibson @jaredpar please take another look

@jaredpar jaredpar self-assigned this Aug 5, 2025
@RikkiGibson
Copy link
Member

We should add a test for the scenario in #79654

It looks like this test was not added but this still seems worthwhile to me.

@333fred
Copy link
Member Author

333fred commented Aug 6, 2025

@RikkiGibson @jjonescz for another look please.

@333fred
Copy link
Member Author

333fred commented Aug 6, 2025

@RikkiGibson after taking another look at that bug, I do think it falls in the scope of this PR, as the fix is "analyze extension setters like they're method calls". I've pushed a change to do just that.

@333fred
Copy link
Member Author

333fred commented Aug 7, 2025

@jjonescz @RikkiGibson please review

@333fred
Copy link
Member Author

333fred commented Aug 8, 2025

@RikkiGibson please review

E.set_Property(s, x);

s.RefReceiverProperty = x;
E.set_RefReceiverProperty(ref s, x);
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add the following test.

s = s.Property; // success 
s = s.RefReceiverProperty; // fail

Copy link
Member

Choose a reason for hiding this comment

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

Ah. s.RefReceiverProperty is allowed to return an S that wraps ref s (the extension parameter), because ref-safe-context of s is return-only.

This seems to illustrate that extension authors should prefer scoped ref S s when S is ref struct, to make things resemble the defaults used for ref struct instance methods.

@333fred 333fred enabled auto-merge (squash) August 8, 2025 21:09
@333fred 333fred merged commit b4860b0 into dotnet:main Aug 8, 2025
23 of 24 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Aug 8, 2025
@333fred 333fred deleted the refsafety-rewriteearlier branch August 8, 2025 22:54
@RikkiGibson RikkiGibson modified the milestones: Next, 18.0 P1 Aug 19, 2025
@jcouv jcouv added the Feature - Extension Everything The extension everything feature label Dec 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Compilers Feature - Extension Everything The extension everything feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ref safety analysis for extension properties produces different results based on syntax used

5 participants