-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Conversation
} | ||
static void F(ref S s) | ||
{ | ||
_ = s; |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
?
@dotnet/roslyn-compiler, please review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (commit 3)
Fixes #60905