Skip to content

Fix MemberNotNull attributes on substituted methods#47818

Merged
RikkiGibson merged 2 commits intodotnet:masterfrom
RikkiGibson:fix-47667
Sep 19, 2020
Merged

Fix MemberNotNull attributes on substituted methods#47818
RikkiGibson merged 2 commits intodotnet:masterfrom
RikkiGibson:fix-47667

Conversation

@RikkiGibson
Copy link
Member

Closes #47667

We forgot to override some methods in WrappedMethodSymbol, so substituted methods were showing as never having any NotNullMembers, NotNullWhenTrueMembers, or NotNullWhenFalseMembers.

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks (iteration 1)

@jcouv jcouv self-assigned this Sep 18, 2020
@RikkiGibson RikkiGibson requested a review from a team September 18, 2020 19:26
@jaredpar
Copy link
Member

There are a couple of other MethodSymbol derived types that don't override these members:

  • FunctionPointerMethodSymbol: probably not important because they cant' have attributes
  • ReducedExtensionMethodSymbol: not used for compilation but won't this impact binding information?
  • SynthesizedIntrinsicOperatorSymbol : unsure if this needs to be accounted for or not

Think the first two are fine but unsure about the third. Did you verify this didn't also need the override?

@RikkiGibson
Copy link
Member Author

RikkiGibson commented Sep 18, 2020

FunctionPointerMethodSymbol: probably not important because they cant' have attributes

Agreed, will probably refrain from overriding them due to no attributes.

SynthesizedIntrinsicOperatorSymbol : unsure if this needs to be accounted for or not

I do not think such methods would participate in NotNull/MemberNotNull

ReducedExtensionMethodSymbol: not used for compilation but won't this impact binding information?

I don't 100% understand the question. I do not think semantic model will be affected because we don't use these symbols to provide nullable public API information (e.g. nullable flow state for an expression).

I could see extension methods participating in NotNull, since it only concerns parameters), so let's add a test.

  • edit: This PR only affects [MemberNotNull(...)], [MemberNotNullWhen(true, ...)], and [MemberNotNullWhen(false, ...)] scenarios. Looking at the [NotNull] tests, I think the coverage is adequate, so will not do anything for it in this PR.

Extension methods in reduced form should not participate in MemberNotNull in the "intuitive" way, since LDM decided that such attributes on extension methods would only be referencing members of the containing type of the extension method. We could conceivably test something like:

class C
{
    public string? _field;
}

static class Ext
{
    public static string? _field;

    [MemberNotNull("field")]
    public static void AssertFieldNotNull(this C c) { if (_field == null) throw null!; }
}

class Program
{
    void M(C c)
    {
        c.AssertFieldNotNull();
        Ext._field.ToString(); // no warning?
        c._field.ToString(); // warning
    }
}

As you can see, it's a really weird scenario--but good to test.

@RikkiGibson RikkiGibson merged commit 51917b5 into dotnet:master Sep 19, 2020
@ghost ghost added this to the Next milestone Sep 19, 2020
@AlekseyTs
Copy link
Contributor

ReducedExtensionMethodSymbol: not used for compilation but won't this impact binding information?

I do not think semantic model will be affected because we don't use these symbols to provide nullable public API information (e.g. nullable flow state for an expression).

I am not sure what exactly does is mean to use a symbol to provide nullable public API information. In general, Retargeting symbols and PE symbols are analogous and should be treated uniformly. Similar to PE symbols, we are not performing binding for them as for symbols that are coming from source, but from the usage point of view, they are no different than source and PE symbols. I.e. any analysis can run into them, and they should be able to provide necessary information.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MemberNotNullWhen null tracking not working with generics

5 participants