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

Fix assignment analysis of ref fields #75340

Merged
merged 5 commits into from
Oct 14, 2024

Conversation

jjonescz
Copy link
Member

@jjonescz jjonescz commented Oct 2, 2024

Fixes #75315.

A field cannot be assigned-to via a readonly reference, but previously we considered any reference to be a write and hence didn't emit the "field is never assigned" warning.

Similarly we didn't distinguish between ref assignments and value assignments of ref fields - now we consider only ref assignments to be writes to ref fields.

This also improves the warning to say that any never-assigned ref field will have its default value set to "null reference".

@jjonescz jjonescz force-pushed the 75315-RefFieldNeverAssigned branch from 9727861 to 500e713 Compare October 3, 2024 09:55
@jjonescz jjonescz marked this pull request as ready for review October 3, 2024 11:25
@jjonescz jjonescz requested a review from a team as a code owner October 3, 2024 11:25
@RikkiGibson RikkiGibson self-assigned this Oct 3, 2024
@jjonescz jjonescz requested a review from RikkiGibson October 7, 2024 08:28
@jjonescz
Copy link
Member Author

jjonescz commented Oct 9, 2024

@dotnet/roslyn-compiler for a second review, thanks

@@ -846,15 +846,17 @@ private void NoteRead(BoundNode fieldOrEventAccess)
}
}

protected virtual void NoteWrite(Symbol variable, BoundExpression value, bool read)
protected virtual void NoteWrite(Symbol variable, BoundExpression value, bool read, bool isRef)
Copy link
Member

Choose a reason for hiding this comment

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

Does isRef mean literally ref or any kind like in / out. Consider adding a comment here to make the intent clear.

Copy link
Member Author

@jjonescz jjonescz Oct 10, 2024

Choose a reason for hiding this comment

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

It means "ref assignment", so there's no in/out variant. Added a comment.

@jjonescz jjonescz requested a review from jaredpar October 11, 2024 07:57
@jjonescz jjonescz merged commit b1567b7 into dotnet:main Oct 14, 2024
24 checks passed
@jjonescz jjonescz deleted the 75315-RefFieldNeverAssigned branch October 14, 2024 07:57
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Oct 14, 2024
@akhera99 akhera99 modified the milestones: Next, 17.13 P1 Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Field is never assigned" warning inconsistent for ref fields
4 participants