Skip to content

ITypeSymbol.BaseType docs appear to be incorrect wrt Type Parameters. #41733

@CyrusNajmabadi

Description

@CyrusNajmabadi

The docs for ITypeSymbol.BaseType explicitly state:

The base type of a type parameter is its effective base class.

However, in practice, this does not appear to be true. For example, when given the following code:

abstract class Base
{
    public abstract void Method();
}

class Derived<T> : Base where T : Base
{
    T inner;
}

The ITypeParameterSymbol for T returns null for BaseType.

Debugging through, this appears to be very explicit:

ITypeSymbol.BaseType is:

        INamedTypeSymbol ITypeSymbol.BaseType
        {
            get
            {
                return UnderlyingTypeSymbol.BaseTypeNoUseSiteDiagnostics.GetPublicSymbol();
            }
        }

And BaseTypeNoUseSiteDiagnostics is defined for all TypeParameters with this sealed override:

        internal sealed override NamedTypeSymbol BaseTypeNoUseSiteDiagnostics => null;

Now, TypeParameterSymbol also has:

        /// <summary>
        /// The effective base class of the type parameter (spec 10.1.5). If the deduced
        /// base type is a reference type, the effective base type will be the same as
        /// the deduced base type. Otherwise if the deduced base type is a value type,
        /// the effective base type will be the most derived reference type from which
        /// deduced base type is derived.
        /// </summary>
        internal NamedTypeSymbol EffectiveBaseClassNoUseSiteDiagnostics

One of two things seems to be going on here:

  1. we actually want ITypeParameterSymbol.BaseType to return null (in which case we need to update the docs).
  2. The docs are correct, but the impl is incorrect. In which case we could consider fixing (though it's unclear what the implications of that might be).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions