Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace Microsoft.CodeAnalysis.CSharp.CodeGeneration
{
[ExportLanguageService(typeof(SyntaxGenerator), LanguageNames.CSharp), Shared]
internal class CSharpSyntaxGenerator : SyntaxGenerator
internal sealed class CSharpSyntaxGenerator : SyntaxGenerator
{
// A bit hacky, but we need to actually run ParseToken on the "nameof" text as there's no
// other way to get a token back that has the appropriate internal bit set that indicates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -49,7 +50,7 @@ private static void VerifySyntax<TSyntax>(SyntaxNode node, string expectedText)
{
Assert.IsAssignableFrom<TSyntax>(node);
var normalized = node.NormalizeWhitespace().ToFullString();
Assert.Equal(expectedText, normalized);
AssertEx.Equal(expectedText, normalized);
}

private static void VerifySyntaxRaw<TSyntax>(SyntaxNode node, string expectedText) where TSyntax : SyntaxNode
Expand Down Expand Up @@ -968,6 +969,25 @@ public void TestOperatorDeclaration()
"explicit operator bool (global::System.Int32 p0, global::System.String p1)\r\n{\r\n}");
}

[Fact, WorkItem(65833, "https://github.com/dotnet/roslyn/issues/65833")]
public void TestConversionOperatorDeclaration()
{
var gcHandleType = _emptyCompilation.GetTypeByMetadataName(typeof(GCHandle).FullName);
var conversion = gcHandleType.GetMembers().OfType<IMethodSymbol>().Single(m =>
m.Name == WellKnownMemberNames.ExplicitConversionName && m.Parameters[0].Type.Equals(gcHandleType));

VerifySyntax<ConversionOperatorDeclarationSyntax>(
Generator.Declaration(conversion),
"public static explicit operator global::System.IntPtr(global::System.Runtime.InteropServices.GCHandle value)\r\n{\r\n}");

var doubleType = _emptyCompilation.GetSpecialType(SpecialType.System_Decimal);
conversion = doubleType.GetMembers().OfType<IMethodSymbol>().Single(m =>
m.Name == WellKnownMemberNames.ImplicitConversionName && m.Parameters[0].Type.Equals(_emptyCompilation.GetSpecialType(SpecialType.System_Byte)));
VerifySyntax<ConversionOperatorDeclarationSyntax>(
Generator.Declaration(conversion),
"public static implicit operator global::System.Decimal(global::System.Byte value)\r\n{\r\n}");
}

[Fact]
public void TestConstructorDeclaration()
{
Expand Down
8 changes: 3 additions & 5 deletions src/Workspaces/Core/Portable/Editing/SyntaxGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,12 @@ public virtual SyntaxNode OperatorDeclaration(
}

/// <summary>
/// Creates a method declaration matching an existing method symbol.
/// Creates a operator or conversion declaration matching an existing method symbol.
/// </summary>
public SyntaxNode OperatorDeclaration(IMethodSymbol method, IEnumerable<SyntaxNode>? statements = null)
{
if (method.MethodKind != MethodKind.UserDefinedOperator)
{
if (method.MethodKind is not (MethodKind.UserDefinedOperator or MethodKind.Conversion))
throw new ArgumentException("Method is not an operator.");
}

var decl = OperatorDeclaration(
GetOperatorKind(method),
Expand Down Expand Up @@ -579,7 +577,7 @@ public SyntaxNode Declaration(ISymbol symbol)
case MethodKind.Ordinary:
return MethodDeclaration(method);

case MethodKind.UserDefinedOperator:
case MethodKind.UserDefinedOperator or MethodKind.Conversion:
return OperatorDeclaration(method);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
' See the LICENSE file in the project root for more information.

Imports System.Globalization
Imports System.Runtime.InteropServices
Imports Microsoft.CodeAnalysis.Editing
Imports Microsoft.CodeAnalysis.Shared.Extensions
Imports Microsoft.CodeAnalysis.Test.Utilities
Expand Down Expand Up @@ -1059,6 +1060,26 @@ End Operator")
End Operator")
End Sub

<Fact, WorkItem(65833, "https://github.com/dotnet/roslyn/issues/65833")>
Public Sub TestConversionOperatorDeclaration()
Dim gcHandleType = _emptyCompilation.GetTypeByMetadataName(GetType(GCHandle).FullName)
Dim Conversion = gcHandleType.GetMembers().OfType(Of IMethodSymbol)().Single(
Function(m) m.Name = WellKnownMemberNames.ExplicitConversionName AndAlso m.Parameters(0).Type.Equals(gcHandleType))

VerifySyntax(Of OperatorBlockSyntax)(
Generator.Declaration(Conversion),
"Public Shared Narrowing Operator CType(value As Global.System.Runtime.InteropServices.GCHandle) As Global.System.IntPtr
End Operator")

Dim doubleType = _emptyCompilation.GetSpecialType(SpecialType.System_Decimal)
Conversion = doubleType.GetMembers().OfType(Of IMethodSymbol)().Single(
Function(m) m.Name = WellKnownMemberNames.ImplicitConversionName AndAlso m.Parameters(0).Type.Equals(_emptyCompilation.GetSpecialType(SpecialType.System_Byte)))
VerifySyntax(Of OperatorBlockSyntax)(
Generator.Declaration(Conversion),
"Public Shared Widening Operator CType(value As System.Byte) As System.Decimal
End Operator")
End Sub

<Fact>
Public Sub MethodDeclarationCanRoundTrip()
Dim tree = VisualBasicSyntaxTree.ParseText(
Expand Down