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

Unclear error report when using uninitialized variable in a local function: "Use of possibly unassigned field '_dummyPrimitive'" #76586

Open
ForNeVeR opened this issue Dec 30, 2024 · 2 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@ForNeVeR
Copy link

ForNeVeR commented Dec 30, 2024

Version Used:

dotnet --info
.NET SDK:
 Version:           9.0.101
 Commit:            eedb237549
 Workload version:  9.0.100-manifests.3068a692
 MSBuild version:   17.12.12+1cce77968

Steps to Reproduce:

Write the following program (dotnet new classlib, <TargetFramework>net9.0</TargetFramework>).

namespace ClassLibrary2;

public class Class1
{
    public static void Foo()
    {
        var my = DoSmth();

        var guid = Guid.NewGuid();
        int DoSmth() => guid.GetHashCode();
    }
}

Diagnostic Id:

CS0170: Use of possibly unassigned field '_dummyPrimitive'

Expected Behavior:
Roslyn should report that it's impossible to use DoSmth() local function before initializing guid local variable. Without referring to any of private fields in the Guid type.

Actual Behavior:

Class1.cs(7,18): error CS0170: Use of possibly unassigned field '_dummyPrimitive'

So, not only it doesn't reference the actual variable that's not initialized, it also mentions _dummyPrimitive, a name of the synthetic field in the corresponding reference assembly and not a real field from System.Guid (I assume).

Imagine what will happen if you are working in a function with a dozen of variables, and Roslyn tells you that some of them are not initialized, using an unfamiliar field name when telling that. The error is quite confusing.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 30, 2024
@ForNeVeR ForNeVeR changed the title Bad error report when using uninitialized variable in a local function Bad error report when using uninitialized variable in a local function: "Use of possibly unassigned field '_dummyPrimitive'" Dec 30, 2024
@ForNeVeR ForNeVeR changed the title Bad error report when using uninitialized variable in a local function: "Use of possibly unassigned field '_dummyPrimitive'" Unclear error report when using uninitialized variable in a local function: "Use of possibly unassigned field '_dummyPrimitive'" Dec 31, 2024
@jhinder
Copy link
Contributor

jhinder commented Jan 2, 2025

Probably a duplicate of #67212.

@ForNeVeR
Copy link
Author

ForNeVeR commented Jan 3, 2025

I saw that issue but not sure honestly.

#67212 seems to describe a more complex situation involving pattern matching and async, while this one is much simpler.

There's an identical example in the last comment, though. But I am not sure if all the same arguments apply to this particular case.

So, whether you wish to close this as a duplicate is up to you, folks.

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

No branches or pull requests

2 participants