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

Allow accessing ref fields through rvalues #75316

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,38 @@ private bool CheckFieldValueKind(SyntaxNode node, BoundFieldAccess fieldAccess,
}
}

if (RequiresRefOrOut(valueKind))
{
switch (fieldSymbol.RefKind)
{
case RefKind.None:
break;
case RefKind.Ref:
// ref/out access to a ref field is fine regardless of the receiver
return true;
case RefKind.RefReadOnly:
ReportReadOnlyError(fieldSymbol, node, valueKind, checkingReceiver, diagnostics);
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 1, 2024

Choose a reason for hiding this comment

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

ReportReadOnlyError(fieldSymbol, node, valueKind, checkingReceiver, diagnostics);

Are we testing this code path? #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, this is unreachable code because there is an if (RequiresAssignableVariable(valueKind)) above which subsumes this one.

return false;
default:
throw ExceptionUtilities.UnexpectedValue(fieldSymbol.RefKind);
}
}

if (RequiresReferenceToLocation(valueKind))
{
switch (fieldSymbol.RefKind)
{
case RefKind.None:
break;
case RefKind.Ref:
case RefKind.RefReadOnly:
// ref readonly access to a ref (readonly) field is fine regardless of the receiver
return true;
default:
throw ExceptionUtilities.UnexpectedValue(fieldSymbol.RefKind);
}
}

// r/w fields that are static or belong to reference types are writeable and returnable
if (fieldSymbol.IsStatic || fieldSymbol.ContainingType.IsReferenceType)
{
Expand Down
Loading