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

"Field is never assigned" warning inconsistent for ref fields #75315

Closed
jjonescz opened this issue Oct 1, 2024 · 1 comment · Fixed by #75340
Closed

"Field is never assigned" warning inconsistent for ref fields #75315

jjonescz opened this issue Oct 1, 2024 · 1 comment · Fixed by #75340

Comments

@jjonescz
Copy link
Member

jjonescz commented Oct 1, 2024

Version Used: 4.13.0-1.24480.10 (ba7fe35)

Steps to Reproduce:

ref struct R
{
    public ref int F; // warning CS0649: Field 'R.F' is never assigned to, and will always have its default value 0

    static void M(in int x) { }

    static void Test(R r)
    {
        M(r.F);
    }
}

compare with:

ref struct R
{
    public ref int F; // no warning

    static void M(in int x) { }

    static void Test(R r)
    {
        M(in r.F); // `in` added here
    }
}

Expected Behavior: The two code snippets are equivalent, so I would expect the warning to be either reported for both or for none of them.

Actual Behavior: The warning is reported inconsistently.

Additional Notes:

  • The warning is also not reported for other callsite modifiers like ref (but one cannot reproduce the "no callsite modifier" scenario there). Perhaps the warning is not supposed to be reported for ref fields at all.
  • If the warning should be reported for ref fields, it shouldn't say that R.F will always have its default value 0 - because it's a ref field, it will have its default value null.
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Oct 1, 2024
@jaredpar
Copy link
Member

jaredpar commented Oct 1, 2024

I wouldn't expect in to remove the warning here because it's not reassigning the ref. I would expect only a ref reassignment to remove the warning.

@jaredpar jaredpar added Bug Feature - Ref Fields and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Oct 1, 2024
@jaredpar jaredpar added this to the 17.13 milestone Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants