提交 89de9256 编写于 作者: C CyrusNajmabadi

Add type inference support for tuples.

上级 540f00d0
......@@ -7262,7 +7262,7 @@ private object Method()
parseOptions: TestOptions.Regular);
}
[Fact/*(Skip = "https://github.com/dotnet/roslyn/issues/15508")*/, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
[WorkItem(14136, "https://github.com/dotnet/roslyn/issues/14136")]
public async Task TestDeconstruction4()
{
......@@ -7379,5 +7379,67 @@ public void Foo()
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
[WorkItem(18969, "https://github.com/dotnet/roslyn/issues/18969")]
public async Task TestTupleElement1()
{
await TestAsync(
@"using System;
class C
{
public void M1()
{
(int x, string y) t = ([|Method|](), null);
}
}",
@"using System;
class C
{
public void M1()
{
(int x, string y) t = (Method(), null);
}
private int Method()
{
throw new NotImplementedException();
}
}",
parseOptions: TestOptions.Regular);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
[WorkItem(18969, "https://github.com/dotnet/roslyn/issues/18969")]
public async Task TestTupleElement2()
{
await TestAsync(
@"using System;
class C
{
public void M1()
{
(int x, string y) t = (0, [|Method|]());
}
}",
@"using System;
class C
{
public void M1()
{
(int x, string y) t = (0, Method());
}
private string Method()
{
throw new NotImplementedException();
}
}",
parseOptions: TestOptions.Regular);
}
}
}
\ No newline at end of file
......@@ -310,6 +310,11 @@ private IEnumerable<TypeInferenceInfo> InferTypeInAnonymousObjectCreation(Anonym
return InferTypeInElementAccessExpression(elementAccess, index, argument);
}
if (argument.IsParentKind(SyntaxKind.TupleExpression))
{
return InferTypeInTupleExpression((TupleExpressionSyntax)argument.Parent, argument);
}
}
if (argument.Parent.IsParentKind(SyntaxKind.ImplicitElementAccess) &&
......@@ -331,6 +336,18 @@ private IEnumerable<TypeInferenceInfo> InferTypeInAnonymousObjectCreation(Anonym
return SpecializedCollections.EmptyEnumerable<TypeInferenceInfo>();
}
private IEnumerable<TypeInferenceInfo> InferTypeInTupleExpression(
TupleExpressionSyntax parent, ArgumentSyntax argument)
{
var index = parent.Arguments.IndexOf(argument);
var parentTypes = InferTypes(parent);
return parentTypes.Select(typeInfo => typeInfo.InferredType)
.OfType<INamedTypeSymbol>()
.Where(namedType => namedType.IsTupleType && index < namedType.TupleElements.Length)
.Select(tupleType => new TypeInferenceInfo(tupleType.TupleElements[index].Type));
}
private IEnumerable<TypeInferenceInfo> InferTypeInAttributeArgument(AttributeArgumentSyntax argument, SyntaxToken? previousToken = null, ArgumentSyntax argumentOpt = null)
{
if (previousToken.HasValue)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册