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

CS8602 false positive after TryGetValue #73274

Closed
AArnott opened this issue Apr 29, 2024 · 4 comments
Closed

CS8602 false positive after TryGetValue #73274

AArnott opened this issue Apr 29, 2024 · 4 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@AArnott
Copy link
Contributor

AArnott commented Apr 29, 2024

Version Used: VS 17.11 Preview 1 (34828.26.main)

Steps to Reproduce:

Sharplab

Diagnostic Id: CS8602

Expected Behavior:

No warning

Actual Behavior:

A warning that list is dereferenced when it may be null. When in fact it cannot be null because of the simple check and assignment I do.

The really interesting thing is this only shows up when targeting .NET 6 and 8 for me. .NET Standard 2.0 and 2.1 targets don't mis-fire here.
It looks like .NET 6 added [MaybeNullWhen(false)] to the TryGetValue output parameter. I bet that's what is triggering this, though it shouldn't, since I check the false condition and assign a non-null value.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 29, 2024
@jjonescz
Copy link
Member

Dictionary<string, List<bool>> d = new();
bool e = d.TryGetValue("hi", out List<bool>? list);
if (!e)
{
    list = new();
}
Console.WriteLine(list.Count);

Nullability info isn't propagated through boolean variables like that. See #27011.

If you "inline" the check, it works:

Dictionary<string, List<bool>> d = new();
if (!d.TryGetValue("hi", out List<bool>? list))
{
    list = new();
}
Console.WriteLine(list.Count);

@AArnott
Copy link
Contributor Author

AArnott commented Apr 30, 2024

Thanks for looking.
In the case I got this from, I need the bool local because it is used in later expressions. So it isn't as simple as inlining it, which is what I normally do.

@jjonescz
Copy link
Member

I agree it would be useful to support this, but I think it needs a language proposal - #27011 (comment).

@jaredpar
Copy link
Member

jaredpar commented May 1, 2024

The current behavior is indeed "By Design". I agree it's unfortunate and it would be nice if this worked. Getting this to work though is not a trivial undertaking, it's a significant amount of design and implementation work. Rikki wrote up a summary of the problems that this poses.

@jaredpar jaredpar closed this as completed May 1, 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

No branches or pull requests

3 participants