Fix ref safety of implicit calls that might capture refs in the receiver #76657
+971
−86
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Alternative to #76237 and #76263.
Fixes #75802.
Fixes #63306.
There can be implicit calls generated by the compiler like
collection.Add
orinterpolatedStringHandler.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 thoseAdd
methods usually returnvoid
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.