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 inference in conditional expressions #36096

Closed
RikkiGibson opened this issue May 31, 2019 · 3 comments
Closed

Nullable inference in conditional expressions #36096

RikkiGibson opened this issue May 31, 2019 · 3 comments

Comments

@RikkiGibson
Copy link
Contributor

RikkiGibson commented May 31, 2019

#nullable enable

public class Program
{
    static void M(bool flag, string? x)
    {
        if (flag ? x?.Length == 2 : x?.Length == 3)
        {
            x.ToString(); // warns today, but perhaps shouldn't warn
        }
    }
}

SharpLab

Note that if flag is changed to the constant true, the warning is not given.

The suggestion here is that if either conditional expression arm has a split state, we join the StateWhenTrue on each arm, and join the StateWhenFalse on each arm, and use the resulting split state as the resulting state of the conditional expression.

@RikkiGibson RikkiGibson added this to the 16.2.P3 milestone May 31, 2019
@RikkiGibson RikkiGibson self-assigned this May 31, 2019
@RikkiGibson RikkiGibson modified the milestones: 16.2.P3, Backlog May 31, 2019
@RikkiGibson RikkiGibson assigned 333fred and unassigned RikkiGibson May 31, 2019
@gafter
Copy link
Member

gafter commented May 31, 2019

This behavior is the same as our other flow analyses:

using System;
public class C {
    public void M(bool b1, bool b2) {
        int x;
        if (b1 ? b2 && M(out x) : b2 && M(out x))
        {
            Console.WriteLine(x); // error CS0165: Use of unassigned local variable 'x'
        }
    }
    private static bool M(out int x)
    {
        x = 1;
        return true;
    }
}

@RikkiGibson
Copy link
Contributor Author

I simplified the reproducer and added a description of what the implementation change would probably be.

@RikkiGibson
Copy link
Contributor Author

This will be fixed as part of the "improved definite assignment" feature. #51463

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants