提交 cd6a3e3c 编写于 作者: A AlekseyTs

Improve method type argument inference around nullable tuples.

Fixes #14152.
上级 db716fad
......@@ -8453,10 +8453,16 @@ static void Main()
{
// nested tuple literals set lower bounds
Test1((a: 1, b: (a: 1, b: 2)), (a: 1, b: (object)1));
Test1(Nullable((a: 1, b: (a: 1, b: 2))), (a: 1, b: (object)1));
Test1((a: 1, b: (a: 1, b: 2)), (a: 1, b: (c: 1, d: 2)));
Test1((a: 1, b: (a: 1, b: 2)), (a: 1, b: (1, 2)));
Test1((a: 1, b: (a: 1, b: 2)), (a: 1, b: (a: 1, b: 2)));
}
}
static T? Nullable<T>(T x) where T : struct
{
return x;
}
static void Test1<T, U>((T, U)? x, (T, U) y)
{
......@@ -8469,12 +8475,45 @@ static void Main()
additionalRefs: s_valueTupleRefs,
parseOptions: TestOptions.Regular, expectedOutput: @"
System.Object
System.Object
System.ValueTuple`2[System.Int32,System.Int32]
System.ValueTuple`2[System.Int32,System.Int32]
System.ValueTuple`2[System.Int32,System.Int32]
");
}
[Fact]
public void Inference13_Err()
{
var source = @"
class C
{
static void Main()
{
Test1(Nullable((a: 1, b: (a: 1, b: 2))), (a: 1, b: (object)1));
}
static T? Nullable<T>(T x) where T : struct
{
return x;
}
static void Test1<T, U>((T, U) x, (T, U) y)
{
System.Console.WriteLine(typeof(U));
}
}
";
var comp = CreateCompilationWithMscorlib(source,
references: s_valueTupleRefs);
comp.VerifyDiagnostics(
// (6,15): error CS1503: Argument 1: cannot convert from '(int a, (int a, int b) b)?' to '(int, object)'
// Test1(Nullable((a: 1, b: (a: 1, b: 2))), (a: 1, b: (object)1));
Diagnostic(ErrorCode.ERR_BadArgType, "Nullable((a: 1, b: (a: 1, b: 2)))").WithArguments("1", "(int a, (int a, int b) b)?", "(int, object)").WithLocation(6, 15)
);
}
[Fact]
public void Inference14()
{
......
......@@ -1408,11 +1408,13 @@ HandleAsAGeneralExpression:
Dim parameterElementTypes As ImmutableArray(Of TypeSymbol) = Nothing
If parameterType.TryGetElementTypesIfTupleOrCompatible(parameterElementTypes) Then
Dim argumentElementTypes As ImmutableArray(Of TypeSymbol) = Nothing
Dim argumentElementTypes As ImmutableArray(Of TypeSymbol) = Nothing
If Not argumentType.TryGetElementTypesIfTupleOrCompatible(argumentElementTypes) OrElse
parameterElementTypes.Length <> argumentElementTypes.Length Then
If parameterType.GetNullableUnderlyingTypeOrSelf().TryGetElementTypesIfTupleOrCompatible(parameterElementTypes) AndAlso
If(parameterType.IsNullableType(), argumentType.GetNullableUnderlyingTypeOrSelf(), argumentType).
TryGetElementTypesIfTupleOrCompatible(argumentElementTypes) Then
If parameterElementTypes.Length <> argumentElementTypes.Length Then
Return False
End If
......
......@@ -12966,7 +12966,8 @@ System.ValueTuple`2[System.Int32,System.Int32]
End Sub
<Fact(Skip:="https://github.com/dotnet/roslyn/issues/14152")>
<Fact>
<WorkItem(14152, "https://github.com/dotnet/roslyn/issues/14152")>
Public Sub Inference13()
Dim comp = CreateCompilationWithMscorlibAndVBRuntime(
......@@ -12976,14 +12977,24 @@ Imports System
Public Class C
Shared Sub Main()
Test1((a:=1, b:=(a:=1, b:=2)), (a:=1, b:=DirectCast(1, Object)))
Test1(Nullable((a:=1, b:=(a:=1, b:=2))), (a:=1, b:=DirectCast(1, Object)))
Test2(Nullable((a:=1, b:=(a:=1, b:=2))), (a:=1, b:=DirectCast(1, Object)))
Test1((a:=1, b:=(a:=1, b:=2)), (a:=1, b:=(c:=1, d:=2)))
Test1((a:=1, b:=(a:=1, b:=2)), (a:=1, b:=(1, 2)))
Test1((a:=1, b:=(a:=1, b:=2)), (a:=1, b:=(a:=1, b:=2)))
End Sub
Shared Function Nullable(Of T as structure)(x as T) as T?
return x
End Function
Shared Sub Test1(Of T, U)(x As (T, U)?, y As (T, U))
Console.WriteLine(GetType(U))
End Sub
Shared Sub Test2(Of T, U)(x As (T, U), y As (T, U))
Console.WriteLine(GetType(U))
End Sub
End Class
</file>
</compilation>,
......@@ -12991,6 +13002,8 @@ options:=TestOptions.ReleaseExe, additionalRefs:=s_valueTupleRefs)
CompileAndVerify(comp, expectedOutput:="
System.Object
System.Object
System.Object
System.ValueTuple`2[System.Int32,System.Int32]
System.ValueTuple`2[System.Int32,System.Int32]
System.ValueTuple`2[System.Int32,System.Int32]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册