Skip to content

[NativeAOT] Method overrides with covariant return type don't work in all cases #96175

@AlexDoeringEnvision

Description

@AlexDoeringEnvision

Description

when NativeAOT finds an covariant override it will pick that type as new return type and ignore further overrides that don't return the same type
tested with NativeAOT net7/8

Reproduction Steps

var impl = new Impl();
var subImpl = new SubImpl();
Console.WriteLine(impl.FromBase());
Console.WriteLine(impl.FromType());

Console.WriteLine(subImpl.FromBase());
Console.WriteLine(subImpl.FromType());


class Base
{
    public Base FromBase()
    {
        return FromType();
    }

    public virtual Base FromType()
    {
        return new Base();
    }
}

class Impl : Base
{
    public override Impl FromType()
    {
        return new Impl();
    }
}

class SubImpl : Impl
{
    // not called from FromBase()
    public override SubImpl FromType()
    {
        return new SubImpl();
    }
}

Expected behavior

Impl
Impl
SubImpl
SubImpl

Actual behavior

Impl
Impl
Impl
SubImpl

Regression?

No response

Known Workarounds

don't use covariant return type

Configuration

.NET 8, Windows 10, x64, NativeAOT.

Other information

No response

Metadata

Metadata

Assignees

Labels

area-NativeAOT-coreclrin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

Status

No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions