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

Nullable state isn't propagated from out parameters if the return value is stored in local #66807

Closed
tmat opened this issue Feb 10, 2023 · 1 comment
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design

Comments

@tmat
Copy link
Member

tmat commented Feb 10, 2023

Version Used:

Version 17.6.0 Preview 2.0 [33407.517.main]

Steps to Reproduce:

using System.Diagnostics.CodeAnalysis;

var success = TryGet(out var x);
if (success)
{
    Console.WriteLine(x.ToString());
}

bool TryGet([NotNullWhen(true)] out object? x)
{
    x = new object();
    return true;
}

Expected Behavior:

No warnings.

Actual Behavior:

Program.cs(6,23,6,24): warning CS8602: Dereference of a possibly null reference.

This works as expected:

if (TryGet(out var x))
{
    Console.WriteLine(x.ToString());
}
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 10, 2023
@RikkiGibson
Copy link
Contributor

RikkiGibson commented Feb 10, 2023

Root cause is same as #27011. See also #66693 (comment).

It's not clear whether a tractable design exists which handles cases like these. Some fairly deep investigation would be needed to determine that which is better done on the csharplang side.

(In other words, I don't know for sure that it's impossible, I just know that I don't know how it would work within the practical constraints of the compiler's static analysis.)

@RikkiGibson RikkiGibson closed this as not planned Won't fix, can't repro, duplicate, stale Feb 10, 2023
@RikkiGibson RikkiGibson added Resolution-By Design The behavior reported in the issue matches the current design Feature - Nullable Reference Types Nullable Reference Types and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

2 participants