Skip to content

Should nullable reference types be disallowed in typeof? #29894

@AlekseyTs

Description

@AlekseyTs
        [Fact]
        public void TypeOf_02()
        {
            CSharpCompilation c = CreateCompilation(new[] { @"
class List<T> { }
class C<T, TClass, TStruct>
    where TClass : class
    where TStruct : struct
{
    void M()
    {
        _ = typeof(C<int, object, int>?);
        _ = typeof(T?);
        _ = typeof(TClass?);
        _ = typeof(TStruct?);
        _ = typeof(List<T?>);
        _ = typeof(List<TClass?>);
        _ = typeof(List<TStruct?>);
    }
}
", NonNullTypesTrue, NonNullTypesAttributesDefinition });

            // PROTOTYPE(NullableReferenceTypes): should nullable reference types be disallowed in `typeof`?
            c.VerifyDiagnostics(
                // (10,20): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint.
                //         _ = typeof(T?);
                Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "T?").WithLocation(10, 20),
                // (13,25): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint.
                //         _ = typeof(List<T?>);
                Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "T?").WithLocation(13, 25)
                );
        }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions