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

if you have: readonly ref int x = ref y does this count as a read/write ref now? or just a read-ref? It should be the latter. Can you check, fix if necessary, and add test? #58466

Closed
sharwell opened this issue Dec 22, 2021 · 2 comments
Assignees
Labels
Area-IDE Resolution-Answered The question has been answered untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@sharwell
Copy link
Member

sharwell commented Dec 22, 2021

if you have: readonly ref int x = ref y does this count as a read/write ref now? or just a read-ref? It should be the latter. Can you check, fix if necessary, and add test?

Originally posted by @CyrusNajmabadi in #58459 (comment)

Follow-up to #58459

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 22, 2021
@mavasani
Copy link
Contributor

@CyrusNajmabadi @sharwell This case seems to be already addressed in the existing code and we also have a test validating the same:

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeFieldReadonly)]
public async Task FieldAssignedToLocalReadOnlyRef()
{
await TestInRegularAndScript1Async(
@"
class Program
{
[|int i;|]
void M()
{
ref readonly var value = ref i;
}
}",
@"
class Program
{
[|readonly int i;|]
void M()
{
ref readonly var value = ref i;
}
}");

The product code handling this case is below:

else if (operation.Parent is IVariableInitializerOperation variableInitializerOperation)
{
if (variableInitializerOperation.Parent is IVariableDeclaratorOperation variableDeclaratorOperation)
{
switch (variableDeclaratorOperation.Symbol.RefKind)
{
case RefKind.Ref:
return ValueUsageInfo.ReadableWritableReference;
case RefKind.RefReadOnly:
return ValueUsageInfo.ReadableReference;
}
}
}

Note that readonly ref is only valid for a variable declaration, not a simple assignment, my recent PR #58459 handled the missing simple assignment with ref case.

@mavasani
Copy link
Contributor

Synced with Cyrus, closing issue.

@sharwell sharwell added Resolution-Not Applicable The issue is not relevant to code in this repo and is not an external issue. Resolution-Answered The question has been answered and removed Resolution-Not Applicable The issue is not relevant to code in this repo and is not an external issue. labels Dec 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Resolution-Answered The question has been answered untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants