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

Emit a more specific error when using the null suppression operator on a Nullable<T> #41799

Closed
JoeRobich opened this issue Feb 20, 2020 · 1 comment
Labels
Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. Resolution-By Design The behavior reported in the issue matches the current design

Comments

@JoeRobich
Copy link
Member

The current error isn't useful in leading developers to using name.Value instead.

#nullable enable

class Program
{
    static void Main(string[] args)
    {
        if (TryGetName(out var name))
        {
            SayHello(name!);
            //          ^--- CS1503: cannot convert from 'Name?' to 'Name'
        }
    }

    static bool TryGetName([NotNullWhen(returnValue: true)]out Name? name)
    {
        name = Name.Jon;
        return true;
    }

    static void SayHello(Name name)
    {
        Console.WriteLine(name);
    }
}

enum Name
{
    Jon,
    Bob
}
@jcouv jcouv added Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. and removed New Language Feature - Nullable Reference Types Nullable Reference Types labels Feb 9, 2021
@jcouv
Copy link
Member

jcouv commented Feb 9, 2021

The error message is accurate.
The ! suppression operator doesn't do any conversion, and so doesn't affect the error that we produce when assigning Nullable<Name> to Name.

@jcouv jcouv closed this as completed Feb 9, 2021
@jcouv jcouv added the Resolution-By Design The behavior reported in the issue matches the current design label Feb 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

3 participants