Skip to content

NullabilityInfoContext returns incorrect result for parameter of private constructor in a public-only assembly #63853

Closed

Description

@madelson

Description

See reproduction.

Reproduction Steps

        var context = new NullabilityInfoContext();
        // private IndexOutOfRangeException(SerializationInfo info, StreamingContext context)
        var constructor = typeof(IndexOutOfRangeException)
            .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, new[] { typeof(SerializationInfo), typeof(StreamingContext) })!;
        var info = context.Create(constructor.GetParameters()[0]);
        Console.WriteLine(info.WriteState); // Nullable (should be Unknown)

        // printout includes System.Runtime.CompilerServices.NullablePublicOnlyAttribute
        Console.WriteLine(string.Join(", ", typeof(IndexOutOfRangeException).Module.GetCustomAttributes().Select(a => a.GetType())));

Expected behavior

Because of the NullablePublicOnlyAttribute, the parameter nullability for the reference type parameter on the private constructor should be Unknown.

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

I think the problem is this line in NullabilityInfoContext:

if (parameterInfo.Member is MethodInfo method && IsPrivateOrInternalMethodAndAnnotationDisabled(method))

Because ConstructorInfo is MethodBase but not MethodInfo, this check misses.

CC @Shane32

Activity

  1. ghost added
    untriaged
    on Jan 17, 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