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

Should nullability attributes affect method bodies and OHI? #36039

Closed
3 tasks done
jcouv opened this issue May 29, 2019 · 6 comments
Closed
3 tasks done

Should nullability attributes affect method bodies and OHI? #36039

jcouv opened this issue May 29, 2019 · 6 comments
Assignees
Milestone

Comments

@jcouv
Copy link
Member

jcouv commented May 29, 2019

Status:


Also, should they affect OHI (override/hiding/implementation)?

Relates to #35816
Relates to https://github.com/dotnet/csharplang/blob/master/meetings/2019/LDM-2019-05-15.md#discussion

    // should this warn?
    static void F1<T>(T t, [NotNull]out T t2) { t2 = t; }
    // should this warn?
    static void F1<T>([AllowNull]T t, out T t2) where T : class { t2 = t; }
    virtual void F1<T>(T t) { } // on base
    override void F1<T>([DisallowNull]T t) { } // on derived. Should this warn?

Stephen pointed out a scenario they hit:

internal class Foo<T> where T : class
{
    public bool TryGetValue2([MaybeNullWhen(false)] out T value) => 
        TryGetValue1(out value); // currently warns, but shouldn't

    public bool TryGetValue1([MaybeNullWhen(false)] out T value)
    {
        value = default!;
        return false;
    }
}
@jcouv jcouv changed the title Should nullability attributes affect method bodies? Should nullability attributes affect method bodies and OHI? May 30, 2019
@jcouv
Copy link
Member Author

jcouv commented Jun 18, 2019

Relates to #30953 ([MaybeNull]: Cannot implement a generic method with a "default" return value with nullable references)

@cartermp
Copy link
Contributor

Tracking my answer for posterity: Yes, they should warn.

@jnm2
Copy link
Contributor

jnm2 commented Aug 28, 2019

@RikkiGibson suggested that I add my concern about the lack of equivalence of [NotNullWhen(true)]/[MaybeNullWhen(false)] on reference types in delegate conversions.

Let's say I write a nongeneric public API with [NotNullWhen(true)] out string?. Should I preemptively make it [MaybeNullWhen(false)] out string instead, just in case someone wants to pass it to some generic API from another library with an out T parameter?

What's driving the signature is no longer anything intrinsic to the method. Now it's constrained by how it might be used.

Real-world examples and flaws with each alternative are shown in depth at #38191.

@sharwell
Copy link
Member

Tracking my answer for posterity: Yes, they should warn.

Agreed, although it wouldn't be terrible if the warning was produced by an analyzer.

@RikkiGibson
Copy link
Contributor

Late response, but I think this needs to happen in the compiler's nullability analysis, as the analysis of the attributes may result in removing warnings as well as adding them.

@jcouv
Copy link
Member Author

jcouv commented Feb 14, 2020

All three components of this issue are done (two were in 16.5 and one is in 16.6p1).
This was validated against the runtime repo to confirm the impact is expected and useful (PR dotnet/runtime#32090)
I'll go ahead and close this issue now.

@jcouv jcouv closed this as completed Feb 14, 2020
@jcouv jcouv moved this to Active/Investigating in Compiler: Julien's umbrellas Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

5 participants