提交 b31f8e18 编写于 作者: A AlekseyTs 提交者: GitHub

Merge pull request #16605 from AlekseyTs/Issue16478

Restore detection of an ambiguous method type argument inference.
......@@ -2501,6 +2501,11 @@ private bool Fix(int iParam, ref HashSet<DiagnosticInfo> useSiteDiagnostics)
best = MergeTupleNames(MergeDynamic(best, candidate, _conversions.CorLibrary), candidate);
}
else
{
// best candidate is not unique
return false;
}
OuterBreak:
;
......
......@@ -9068,5 +9068,100 @@ public enum Something
// should be NO errors.
CompileAndVerify(source, expectedOutput: @"False");
}
[Fact, WorkItem(16478, "https://github.com/dotnet/roslyn/issues/16478")]
public void AmbiguousInference_01()
{
string source =
@"
using System;
using System.Collections.Generic;
public class Test
{
public static void Assert<T>(T a, T b)
{
Console.WriteLine(""Non collection"");
}
public static void Assert<T>(IEnumerable<T> a, IEnumerable<T> b)
{
Console.WriteLine(""Collection"");
}
public static void Main()
{
string[] a = new[] { ""A"" };
StringValues b = new StringValues();
Assert(a, b);
Assert(b, a);
}
private class StringValues : List<string>
{
public static implicit operator StringValues(string[] values)
{
return new StringValues();
}
public static implicit operator string[] (StringValues value)
{
return new string[0];
}
}
}";
CompileAndVerify(source, expectedOutput:
@"Collection
Collection");
}
[Fact, WorkItem(16478, "https://github.com/dotnet/roslyn/issues/16478")]
public void AmbiguousInference_02()
{
string source =
@"
using System;
using System.Collections.Generic;
public class Test
{
public static void Assert<T>(T a, T b)
{
Console.WriteLine(""Non collection"");
}
public static void Main()
{
string[] a = new[] { ""A"" };
StringValues b = new StringValues();
Assert(a, b);
Assert(b, a);
}
private class StringValues : List<string>
{
public static implicit operator StringValues(string[] values)
{
return new StringValues();
}
public static implicit operator string[] (StringValues value)
{
return new string[0];
}
}
}";
var comp = CreateCompilationWithMscorlibAndSystemCore(source);
comp.VerifyDiagnostics(
// (17,9): error CS0411: The type arguments for method 'Test.Assert<T>(T, T)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
// Assert(a, b);
Diagnostic(ErrorCode.ERR_CantInferMethTypeArgs, "Assert").WithArguments("Test.Assert<T>(T, T)").WithLocation(17, 9),
// (18,9): error CS0411: The type arguments for method 'Test.Assert<T>(T, T)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
// Assert(b, a);
Diagnostic(ErrorCode.ERR_CantInferMethTypeArgs, "Assert").WithArguments("Test.Assert<T>(T, T)").WithLocation(18, 9)
);
}
}
}
......@@ -5775,5 +5775,103 @@ BC30057: Too many arguments to 'Public Sub M1()'.
</expected>)
End Sub
<Fact>
<WorkItem(16478, "https://github.com/dotnet/roslyn/issues/16478")>
Public Sub AmbiguousInference_01()
Dim compilationDef =
<compilation>
<file name="a.vb">
Imports System
Imports System.Collections.Generic
Public Class Test
Public Shared Sub Assert(Of T)(a As T, b As T)
Console.WriteLine("Non collection")
End Sub
Public Shared Sub Assert(Of T)(a As IEnumerable(Of T), b As IEnumerable(Of T))
Console.WriteLine("Collection")
End Sub
Public Shared Sub Main()
Dim a = {"A"}
Dim b = New StringValues()
Assert(a, b)
Assert(b, a)
End Sub
Private Class StringValues
Inherits List(Of String)
Public Shared Widening Operator CType(values As String()) As StringValues
Return New StringValues()
End Operator
Public Shared Widening Operator CType(value As StringValues) As String()
Return {}
End Operator
End Class
End Class
</file>
</compilation>
Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(compilationDef)
CompileAndVerify(compilationDef, expectedOutput:=
"Collection
Collection")
End Sub
<Fact>
<WorkItem(16478, "https://github.com/dotnet/roslyn/issues/16478")>
Public Sub AmbiguousInference_02()
Dim compilationDef =
<compilation>
<file name="a.vb">
Imports System
Imports System.Collections.Generic
Public Class Test
Public Shared Sub Assert(Of T)(a As T, b As T)
Console.WriteLine("Non collection")
End Sub
Public Shared Sub Main()
Dim a = {"A"}
Dim b = New StringValues()
Assert(a, b)
Assert(b, a)
End Sub
Private Class StringValues
Inherits List(Of String)
Public Shared Widening Operator CType(values As String()) As StringValues
Return New StringValues()
End Operator
Public Shared Widening Operator CType(value As StringValues) As String()
Return {}
End Operator
End Class
End Class
</file>
</compilation>
Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(compilationDef)
CompilationUtils.AssertTheseDiagnostics(compilation,
<expected>
BC36651: Data type(s) of the type parameter(s) in method 'Public Shared Sub Assert(Of T)(a As T, b As T)' cannot be inferred from these arguments because more than one type is possible. Specifying the data type(s) explicitly might correct this error.
Assert(a, b)
~~~~~~
BC36651: Data type(s) of the type parameter(s) in method 'Public Shared Sub Assert(Of T)(a As T, b As T)' cannot be inferred from these arguments because more than one type is possible. Specifying the data type(s) explicitly might correct this error.
Assert(b, a)
~~~~~~
</expected>)
End Sub
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册