dotnet/roslyn

CS compiler accepts contradicting signature and fails to accept exact signature when overriding a generic abstract method

Open

#76,914 创建于 2025年1月24日

在 GitHub 查看
 (7 评论) (0 反应) (0 负责人)C# (4,257 fork)batch import
Area-CompilersConcept-Diagnostic ClarityFeature - Nullable Reference Typeshelp wanted

仓库指标

Star
 (20,414 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 256 个 PR)

描述

This compiles:

#nullable enable

using System;
					
namespace Demo
{
    public interface ITest
    {
        public bool TryGet<T>(out T? result) where T : notnull;
    }

    public abstract class MyAbstractClass : ITest
    {
        public abstract bool TryGet<T>(out T? result) where T : notnull;
    }

    public class MyDerivedClass : MyAbstractClass
    {
        public override bool TryGet<T>(out T result)
        {
            throw new Exception("");
        }
    }
}

public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
	}
}

This fails:

#nullable enable

using System;
					
namespace Demo
{
    public interface ITest
    {
        public bool TryGet<T>(out T? result) where T : notnull;
    }

    public abstract class MyAbstractClass : ITest
    {
        public abstract bool TryGet<T>(out T? result) where T : notnull;
    }

    public class MyDerivedClass : MyAbstractClass
    {
        public override bool TryGet<T>(out T? result)
        {
            throw new Exception("");
        }
    }
}

public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
	}
}

public override bool TryGet<T>(out T result) signature, the one the compiler is happy to accept, contradicts both inherited definitions while it rejects public override bool TryGet<T>(out T? result) which is exactly the same as in parents.

贡献者指南