diff --git a/src/EditorFeatures/CSharpTest/Diagnostics/GenerateEnumMember/GenerateEnumMemberTests.cs b/src/EditorFeatures/CSharpTest/Diagnostics/GenerateEnumMember/GenerateEnumMemberTests.cs index 98b0f1e8eea8654902f242a09875a66bb51c3cd6..6214dc3078c50e0d669c81c74aa44b3a884d9531 100644 --- a/src/EditorFeatures/CSharpTest/Diagnostics/GenerateEnumMember/GenerateEnumMemberTests.cs +++ b/src/EditorFeatures/CSharpTest/Diagnostics/GenerateEnumMember/GenerateEnumMemberTests.cs @@ -1690,6 +1690,102 @@ class Program void Main() { E.C } +}"); + } + + [WorkItem(49679, "https://github.com/dotnet/roslyn/issues/49679")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)] + public async Task TestWithLeftShift_Long() + { + await TestInRegularAndScriptAsync( +@"class Program +{ + void Main() + { + Color.[|Blue|]; + } +} + +enum Color : long +{ + Green = 1L << 0 +}", +@"class Program +{ + void Main() + { + Color.Blue; + } +} + +enum Color : long +{ + Green = 1L << 0, + Blue = 1L << 1 +}"); + } + + [WorkItem(49679, "https://github.com/dotnet/roslyn/issues/49679")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)] + public async Task TestWithLeftShift_UInt() + { + await TestInRegularAndScriptAsync( +@"class Program +{ + void Main() + { + Color.[|Blue|]; + } +} + +enum Color : uint +{ + Green = 1u << 0 +}", +@"class Program +{ + void Main() + { + Color.Blue; + } +} + +enum Color : uint +{ + Green = 1u << 0, + Blue = 1u << 1 +}"); + } + + [WorkItem(49679, "https://github.com/dotnet/roslyn/issues/49679")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEnumMember)] + public async Task TestWithLeftShift_ULong() + { + await TestInRegularAndScriptAsync( +@"class Program +{ + void Main() + { + Color.[|Blue|]; + } +} + +enum Color : ulong +{ + Green = 1UL << 0 +}", +@"class Program +{ + void Main() + { + Color.Blue; + } +} + +enum Color : ulong +{ + Green = 1UL << 0, + Blue = 1UL << 1 }"); } } diff --git a/src/EditorFeatures/VisualBasicTest/Diagnostics/GenerateEnumMember/GenerateEnumMemberTests.vb b/src/EditorFeatures/VisualBasicTest/Diagnostics/GenerateEnumMember/GenerateEnumMemberTests.vb index d8a44db49bb2b2d2d1e60a541af6b141e925479b..45bdad8d11564ddd4cdbaa523dc17ad780a4a55c 100644 --- a/src/EditorFeatures/VisualBasicTest/Diagnostics/GenerateEnumMember/GenerateEnumMemberTests.vb +++ b/src/EditorFeatures/VisualBasicTest/Diagnostics/GenerateEnumMember/GenerateEnumMemberTests.vb @@ -1339,5 +1339,28 @@ Enum E As UShort Y = &H8000US End Enum") End Function + + + + Public Async Function TestWithLeftShift_Long() As Task + Await TestInRegularAndScriptAsync( +"Module Program + Sub Main(args As String()) + Goo([|Color.Blue|]) + End Sub + Enum Color + Green = 1L << 0 + End Enum +End Module", +"Module Program + Sub Main(args As String()) + Goo(Color.Blue) + End Sub + Enum Color + Green = 1L << 0 + Blue = 1L << 1 + End Enum +End Module") + End Function End Class End Namespace diff --git a/src/Workspaces/CSharp/Portable/CodeGeneration/EnumMemberGenerator.cs b/src/Workspaces/CSharp/Portable/CodeGeneration/EnumMemberGenerator.cs index 3907ff92264db4fbc1239357f13fdc890c7ace4a..d5bc0b1225291c3ccb16dedd3c9298a4f69bdfa3 100644 --- a/src/Workspaces/CSharp/Portable/CodeGeneration/EnumMemberGenerator.cs +++ b/src/Workspaces/CSharp/Portable/CodeGeneration/EnumMemberGenerator.cs @@ -123,9 +123,11 @@ private static ExpressionSyntax CreateEnumMemberValue(EnumDeclarationSyntax dest { // The user is left shifting ones, stick with that pattern var shiftValue = IntegerUtilities.LogBase2(value); + + // Re-use the numericLiteral text so type suffixes match too return SyntaxFactory.BinaryExpression( SyntaxKind.LeftShiftExpression, - SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal("1", 1)), + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(numericLiteral.Token.Text, 1)), SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(shiftValue.ToString(), shiftValue))); } } diff --git a/src/Workspaces/VisualBasic/Portable/CodeGeneration/EnumMemberGenerator.vb b/src/Workspaces/VisualBasic/Portable/CodeGeneration/EnumMemberGenerator.vb index 068b1dca17a4fc784242a84210859d3711360758..005621ae6345ef50c85f3e44ee5d79c6a076d9dd 100644 --- a/src/Workspaces/VisualBasic/Portable/CodeGeneration/EnumMemberGenerator.vb +++ b/src/Workspaces/VisualBasic/Portable/CodeGeneration/EnumMemberGenerator.vb @@ -93,8 +93,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration If numericLiteral.Token.ValueText = "1" Then ' The user is left shifting ones, stick with that pattern Dim shiftValue = IntegerUtilities.LogBase2(value) + + ' Using the numericLiteral text will ensure the correct type character, ignoring the None that is passed in below Return SyntaxFactory.LeftShiftExpression( - left:=SyntaxFactory.NumericLiteralExpression(SyntaxFactory.IntegerLiteralToken("1", LiteralBase.Decimal, TypeCharacter.None, 1)), + left:=SyntaxFactory.NumericLiteralExpression(SyntaxFactory.IntegerLiteralToken(numericLiteral.Token.Text, LiteralBase.Decimal, TypeCharacter.None, 1)), right:=SyntaxFactory.NumericLiteralExpression(SyntaxFactory.IntegerLiteralToken(shiftValue.ToString(), LiteralBase.Decimal, TypeCharacter.None, IntegerUtilities.ToUnsigned(shiftValue)))) End If End If