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

Record read of ref local even if value is discarded #60910

Merged
merged 3 commits into from
May 10, 2022
Merged

Conversation

cston
Copy link
Member

@cston cston commented Apr 22, 2022

Fixes #60905

}
static void F(ref S s)
{
_ = s;
Copy link
Member Author

Choose a reason for hiding this comment

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

Should _ = s; result in a NullReferenceException at runtime if the reference is null?

Copy link
Member Author

Choose a reason for hiding this comment

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

From discussion with @jaredpar, we should emit the dereference of ref s here, even though the value is discarded.

Copy link
Member

Choose a reason for hiding this comment

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

My logic here was essentially that it's possible for a ref to be null hence this is no different than say _ = a.b where b is a field / prop and a is a reference type. It is an operation that has a potential visible side effect of throwing a NullReferenceException.

Before c# 11 there was no supported way in C# to create a ref that points to null which meant this was probably a lower priority issue. I would've felt a bit more ambivalent about it. Now that we can create such ref I feel a bit more confident that we should be treating it as a side effect here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's emit the dereference as part of the ref fields feature. I've added an item to the ref fields test plan.

}
static void F(ref S s)
{
_ = s.F;
Copy link
Member Author

@cston cston Apr 23, 2022

Choose a reason for hiding this comment

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

Should _ = s.F; result in a NullReferenceException at runtime if s is null?

@cston cston marked this pull request as ready for review April 24, 2022 21:31
@cston cston requested a review from a team as a code owner April 24, 2022 21:31
@cston cston marked this pull request as draft April 25, 2022 10:16
@cston cston marked this pull request as ready for review May 2, 2022 19:11
@cston cston mentioned this pull request May 3, 2022
@cston
Copy link
Member Author

cston commented May 4, 2022

@dotnet/roslyn-compiler, please review.

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 3)

@cston cston merged commit 4c4df4d into dotnet:main May 10, 2022
@cston cston deleted the 60905 branch May 10, 2022 16:25
@ghost ghost added this to the Next milestone May 10, 2022
@cston cston modified the milestones: Next, 17.3 May 10, 2022
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.

Invalid IL produced for an unused struct load from a ref local
4 participants