提交 17940e76 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #16989 from CyrusNajmabadi/generateTypeValueTupleSingleArity

Properly generate type syntax for tuples with less than arity-2.
Fixes #16793
......@@ -141,6 +141,35 @@ class Class : IInterface
}");
}
[WorkItem(16793, "https://github.com/dotnet/roslyn/issues/16793")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)]
public async Task TestMethodWithValueTupleArity1()
{
await TestWithAllCodeStyleOptionsOffAsync(
@"
using System;
interface I
{
ValueTuple<object> F();
}
class C : [|I|]
{
}",
@"
using System;
interface I
{
ValueTuple<object> F();
}
class C : I
{
public ValueTuple<object> F()
{
throw new NotImplementedException();
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)]
public async Task TestExpressionBodiedMethod1()
{
......
......@@ -4514,5 +4514,40 @@ Public MustInherit Class C
End Class",
index:=1)
End Function
<WorkItem(16793, "https://github.com/dotnet/roslyn/issues/16793")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)>
Public Async Function TestMethodWithValueTupleArity1() As Task
Await TestAsync(
"
Imports System
interface I
Function F() As ValueTuple(Of Object)
end interface
class C
Implements [|I|]
end class
Namespace System
Structure ValueTuple(Of T1)
End Structure
End Namespace",
"
Imports System
interface I
Function F() As ValueTuple(Of Object)
end interface
class C
Implements I
Public Function F() As ValueTuple(Of Object) Implements I.F
Throw New NotImplementedException()
End Function
end class
Namespace System
Structure ValueTuple(Of T1)
End Structure
End Namespace")
End Function
End Class
End Namespace
\ No newline at end of file
......@@ -100,6 +100,11 @@ public TypeSyntax CreateSimpleTypeSyntax(INamedTypeSymbol symbol)
}
}
if (symbol.IsTupleType && symbol.TupleUnderlyingType != null && !symbol.Equals(symbol.TupleUnderlyingType))
{
return CreateSimpleTypeSyntax(symbol.TupleUnderlyingType);
}
if (symbol.Name == string.Empty || symbol.IsAnonymousType)
{
return CreateSystemObject();
......@@ -145,7 +150,7 @@ private TypeSyntax TryCreateSpecializedNamedTypeSyntax(INamedTypeSymbol symbol)
return SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword));
}
if (symbol.IsTupleType)
if (symbol.IsTupleType && symbol.TupleElements.Length >= 2)
{
return CreateTupleTypeSyntax(symbol);
}
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Collections.Generic
Imports System.Runtime.CompilerServices
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Simplification
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
......
......@@ -67,6 +67,35 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
End Function
Public Function CreateSimpleTypeSyntax(symbol As INamedTypeSymbol) As TypeSyntax
Dim syntax = TryCreateSpecializedNamedTypeSyntax(symbol)
If syntax IsNot Nothing Then
Return syntax
End If
If symbol.IsTupleType AndAlso
symbol.TupleUnderlyingType IsNot Nothing AndAlso
Not symbol.Equals(symbol.TupleUnderlyingType) Then
Return CreateSimpleTypeSyntax(symbol.TupleUnderlyingType)
End If
If symbol.Name = String.Empty OrElse symbol.IsAnonymousType Then
Return SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("System"), SyntaxFactory.IdentifierName("Object"))
End If
If symbol.OriginalDefinition.SpecialType = SpecialType.System_Nullable_T Then
Return AddInformationTo(SyntaxFactory.NullableType(symbol.TypeArguments.First().Accept(Me)), symbol)
End If
If symbol.TypeParameters.Length = 0 Then
Return symbol.Name.ToIdentifierName
End If
Return SyntaxFactory.GenericName(
symbol.Name.ToIdentifierToken,
SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList(symbol.TypeArguments.[Select](Function(t) t.Accept(Me)))))
End Function
Private Function TryCreateSpecializedNamedTypeSyntax(symbol As INamedTypeSymbol) As TypeSyntax
Select Case symbol.SpecialType
Case SpecialType.System_Object
Return SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("System"), SyntaxFactory.IdentifierName("Object"))
......@@ -102,25 +131,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
Return SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("System"), SyntaxFactory.IdentifierName("DateTime"))
End Select
If symbol.IsTupleType Then
If symbol.IsTupleType AndAlso symbol.TupleElements.Length >= 2 Then
Return CreateTupleTypeSyntax(symbol)
End If
If symbol.Name = String.Empty OrElse symbol.IsAnonymousType Then
Return SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("System"), SyntaxFactory.IdentifierName("Object"))
End If
If symbol.OriginalDefinition.SpecialType = SpecialType.System_Nullable_T Then
Return AddInformationTo(SyntaxFactory.NullableType(symbol.TypeArguments.First().Accept(Me)), symbol)
End If
If symbol.TypeParameters.Length = 0 Then
Return symbol.Name.ToIdentifierName
End If
Return SyntaxFactory.GenericName(
symbol.Name.ToIdentifierToken,
SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList(symbol.TypeArguments.[Select](Function(t) t.Accept(Me)))))
Return Nothing
End Function
Private Shared Function CreateTupleTypeSyntax(symbol As INamedTypeSymbol) As TypeSyntax
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册