Skip to content

NullabilityInfoContext returns incorrect result for some unannotated private members #63847

Closed

Description

@madelson

Description

NullabilityInfoContext contains code which aborts analysis when the module indicates that nullability annotations have been omitted for private/internal members.

While it makes sense to consider this information, it ignores a few edge-cases that are otherwise respected by the class:

  • Value type nullability can still be determined without nullable annotations
  • Code analysis attributes (e. g. DisallowNull) may still be present that could be used to determine nullability without compiler-generated nullable annotations.

Reproduction Steps

public void Main()
{
        var context = new NullabilityInfoContext();
        var directoryInfo = context.Create(typeof(FileSystemEntry).GetProperty("Directory")!);
        Console.WriteLine(directoryInfo.ReadState); // NotNull as expected (property is ReadOnlySpan<char>)
        Console.WriteLine(directoryInfo.WriteState); // Unknown (should be NotNull because property is a non-nullable value type)
}

Expected behavior

The write state should be NotNull because the property is a non-nullable value type.

Actual behavior

The write state is unknown because the setter is private and private annotations are not available.

Regression?

No response

Known Workarounds

No response

Configuration

VS 17.0.2, .NET 6, Windows 10 x64.

I don't have any reason to believe this is configuration specific.

Other information

CC @Shane32

Activity

  1. ghost added
    untriaged
    on Jan 16, 2022
  2. removed
    untriaged
    on Jan 19, 2022
  3. added this to the 7.0.0 milestone on Jan 19, 2022
  4. added a commit that references this issue on Jan 22, 2022
    93cb0f6
  5. ghost added
    in-pr
    on Jan 22, 2022
  6. ghost removed
    in-pr
    on Jan 26, 2022
  7. ghost locked as resolved and limited conversation to collaborators on Feb 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions