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

! on nullable argument should suppress subsequent CS8602 #37071

Closed
drewnoakes opened this issue Jul 9, 2019 · 3 comments
Closed

! on nullable argument should suppress subsequent CS8602 #37071

drewnoakes opened this issue Jul 9, 2019 · 3 comments
Labels
Area-Compilers New Language Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design
Milestone

Comments

@drewnoakes
Copy link
Member

drewnoakes commented Jul 9, 2019

Version Used: 3.3.0-beta1-19327-04

Steps to Reproduce:

bool _initialised;

void M()
{
    _initialised = true;

    object? service = GetService();

    EnsuresNotNull(service!); // ! should indicate 'service' is not null below

    service.ToString();
//  ^^^^^^^ CS8602
}

object? GetService() => _initialised ? "" : null;

void EnsuresNotNull<T>(T t) where T : class
{
    if (t is null)
        throw new ArgumentNullException();
}

It seems we don't have a way of annotating EnsuresNotNull to say that it throws if its argument is null (DoesNotReturnIf in #36810 gets close).

In the absence of that, I'd be happy to ! arguments to such guard methods. It feels consistent and local. It'll also be easy to regex out of code later if I'm able to annotate the guard function in future.

@jcouv
Copy link
Member

jcouv commented Jul 9, 2019

The recommended way of doing this is Debug.Assert(service != null);.
The current behavior is by-design: the suppression (service!) returns a value with a non-null state, but does not change the known state of the variable service.

@jcouv jcouv added the Resolution-By Design The behavior reported in the issue matches the current design label Jul 9, 2019
@jcouv jcouv added this to the Compiler.Next milestone Jul 9, 2019
@RikkiGibson
Copy link
Contributor

Ideally the method EnsuresNotNull could be annotated EnsuresNotNull([NotNull] object? param) to tell the compiler that "if the method returns, the argument is not-null".

@jcouv jcouv added this to Out Of Scope in Nullable Board Jul 11, 2019
@jcouv
Copy link
Member

jcouv commented Jul 15, 2019

I'll go ahead and close this issue as by-design. Here's the relevant section of the spec. It's probably captured in the LDM notes for nullability as well, but I couldn't find which one from a quick look.

@jcouv jcouv closed this as completed Jul 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers New Language Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design
Projects
Nullable Board
Out Of Scope
Development

No branches or pull requests

4 participants