Skip to content

NullabilityInfoContext returns incorrect results for an indexer parameter if the class has a property with a value type return type. #101071

Description

@RobSiklos

Description

If you have a class with:
a) a property which returns a value tuple; and
b) an indexer with a non-nullable reference type parameter

then when looking at the NullabilityInfoContext for the indexer parameter, it appears as Unknown instead of NotNull.

Reproduction Steps

Create a console app with the following code:

internal class Program
{
    public class Foo
    {
        public string this[string name] => throw new NotImplementedException();

        // *** If you comment out this property, the problem goes away ***
        public (int A, int B) ValueTupleProp { get; }
    }

    static void Main(string[] args)
    {
        // Get the PropertyInfo for the indexer.
        PropertyInfo propInfo = typeof(Foo).GetProperty("Item")!;

        // Get the ParameterInfo for the indexer parameter (non-nullable string).
        ParameterInfo paramInfo = propInfo.GetIndexParameters()[0];

        var nullabilityInfo = new NullabilityInfoContext().Create(paramInfo);

        Console.WriteLine($"{nullabilityInfo.ReadState}, {nullabilityInfo.WriteState}");
        Console.ReadKey();
    }
}

Expected behavior

The NullabilityInfoContext properties report NotNull.

Actual behavior

The NullabilityInfoContext properties report Unknown. However, if you comment out the ValueTupleProp property, then it magically works as expected.

Regression?

No response

Known Workarounds

No response

Configuration

Tried running in .NET 6.0 and .NET 8.0 on Windows 11.

Other information

No response

Activity

  1. added
    untriaged
    on Apr 15, 2024
  2. dotnet-policy-service commented on Apr 15, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-reflection
    See info in area-owners.md if you want to be subscribed.

  3. added this to the 9.0.0 milestone on Apr 17, 2024
  4. added
    help wanted
    and removed
    untriaged
    on Apr 17, 2024
  5. karakasa commented on May 25, 2024

    @karakasa
    Contributor

    The reason is because NullabilityInfoContext.Create retrieves NullableContextAttribute from ParameterInfo.Member, which is the indexer property for indexed parameters rather than the getter method, where NullableContext is annotated in IL.

    As for the ValueTuple padding, when there's only one property in a type, the nullability context is annotated on the type itself, therefore the member's annotation isn't involved.

  6. added a commit that references this issue on May 25, 2024
    680be21
  7. modified the milestones: 9.0.0, Future on Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Reflectionbughelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions