Skip to content

Conversation

@RikkiGibson
Copy link
Member

Closes #80244

The reason this only pops up when indirecting to a local is: the safe-contexts of a local are calc'd when its initializer is visited, using GetRefEscape()/GetValEscape(), and are stored. When a usage of the local is found, these safe-contexts are looked up and checked against the context of the usage.

Simple cases like return ref rs[1] don't end up actually using GetRefEscape() for ref rs[1]. Instead it uses CheckRefEscape() only, which uses a completely different code path to check the escape, which correctly understood that this does not escape by-reference.

The GetRefEscape() path needed to be changed to reflect that this doesn't escape by-reference in the old rules.

I saw that the Check() path also sometimes adjusts the MethodInvocationInfo to drop the receiver in order to visit it separately. This didn't seem necessary here.

@RikkiGibson RikkiGibson requested a review from a team as a code owner September 12, 2025 21:15
// - If `p` is an output parameter, its ref-safe-context is the caller-context.
// - Otherwise, if `p` is the `this` parameter of a struct type, its ref-safe-context is the function-member.
// - Otherwise, the parameter is a value parameter, and its ref-safe-context is the function-member.
if (parameter.RefKind != RefKind.None && !parameter.IsThis)
Copy link
Member Author

@RikkiGibson RikkiGibson Sep 12, 2025

Choose a reason for hiding this comment

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

A similar check occurs in line 3001, for the "new rules", where it delegates to helper GetParameterRefEscapeLevel that accounts for UnscopedRef, etc. We could consider also using that helper here. However, I feel the implementation here, is somewhat more clearly in line with the standard.

@RikkiGibson RikkiGibson requested a review from a team September 15, 2025 19:44
Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 2)

@RikkiGibson
Copy link
Member Author

Looks like CoreClr test jobs have all timed out. I checked one of the jobs and all the work items are still in Waiting state after 90 minutes. So there might be an infra issue.

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.

Unexpected error when using Span defined with old ref safety rules

4 participants