Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ref safety of implicit calls that might capture refs in the receiver #76657

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jjonescz
Copy link
Member

@jjonescz jjonescz commented Jan 7, 2025

Alternative to #76237 and #76263.
Fixes #75802.
Fixes #63306.

There can be implicit calls generated by the compiler like collection.Add or interpolatedStringHandler.AppendFormatted that previously weren't fully analyzed by the ref safety analysis.

For collection initializers, the ref safety analysis went over all the initializers (Add calls) and intersected their val escape scopes, but that essentially did nothing since those Add methods usually return void so GetValEscape doesn't look at them.
For interpolated strings, the ref safety analysis went over all the arguments and intersected their val escape scopes.
But neither of those approaches detected when a ref passed as an in argument could be captured into the receiver (collection / interpolated string handler). So this PR analyzes the implicit calls properly to detect that.

All the existing get / check val escape utilities work on expressions and so they analyze calls only if they can actually return a ref. (Arg mixing checks if a ref can escape into the receiver but since here we are essentially trying to determine what the escape scope of the receiver should be, we cannot easily use that - I've tried that in the alternative PRs linked above but it wasn't very nice.) So I added other utilities to determine/check the escape scope of the receiver of a call.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Jan 7, 2025
@jjonescz jjonescz marked this pull request as ready for review January 7, 2025 17:14
@jjonescz jjonescz requested a review from a team as a code owner January 7, 2025 17:14
@jjonescz jjonescz requested review from RikkiGibson and cston January 7, 2025 17:15
@AlekseyTs
Copy link
Contributor

@jjonescz Could you provide more details about the change? How exactly it fixes the issues, etc.

if (part is not BoundCall { Method.Name: BoundInterpolatedString.AppendFormattedMethod } call)
{
// Dynamic calls cannot have ref struct parameters, and AppendLiteral calls will always have literal
// string arguments and do not require us to be concerned with escape
Copy link
Contributor

Choose a reason for hiding this comment

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

and do not require us to be concerned with escape

Can AppendLiteral have an in parameter? I assume the ref-ness of the parameter is important, not just the argument value.

Copy link
Member Author

@jjonescz jjonescz Jan 9, 2025

Choose a reason for hiding this comment

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

Yes, AppendLiteral calls can have the same issue, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Ref Fields untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
2 participants