Skip to content

NullabilityInfoContext reports incorrect result when mixing AllowNull and DisallowNull #63846

@madelson

Description

@madelson

Description

The result does not match how the compiler interprets the attributes. See the reproduction.

Reproduction Steps

public void Main()
{
            var context = new NullabilityInfoContext();
        
        var hasAllowAndDisallowInfo = context.Create(typeof(HasMultipleCodeAnalysisAttributes).GetField("HasAllowAndDisallow")!);
        Console.WriteLine(hasAllowAndDisallowInfo.ReadState); // NotNull as expected
        Console.WriteLine(hasAllowAndDisallowInfo.WriteState); // Nullable; should be NotNull
}

private class HasMultipleCodeAnalysisAttributes
{
    [AllowNull, DisallowNull]
    public string HasAllowAndDisallow = string.Empty;
}

Expected behavior

We should expect a NotNull write state because this is how the C# compiler interprets it:

new HasMultipleCodeAnalysisAttributes().HasAllowAndDisallow = null; // CS8625

Actual behavior

Returns Nullable

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions