From 487e274496019b460a9907129a119fe838353e61 Mon Sep 17 00:00:00 2001 From: Eric StJohn Date: Thu, 6 Feb 2025 17:37:39 -0800 Subject: [PATCH 1/2] Fix syntax generation of explict operator from symbol --- .../Syntax.xml.Internal.Generated.cs | 6 ++++-- .../Syntax.xml.Main.Generated.cs | 3 ++- .../CSharp/Portable/Syntax/Syntax.xml | 1 + .../CodeGeneration/SyntaxGeneratorTests.cs | 18 ++++++++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Generated/CSharpSyntaxGenerator/CSharpSyntaxGenerator.SourceGenerator/Syntax.xml.Internal.Generated.cs b/src/Compilers/CSharp/Portable/Generated/CSharpSyntaxGenerator/CSharpSyntaxGenerator.SourceGenerator/Syntax.xml.Internal.Generated.cs index 78d8aedb8b8f2..0c6bc0fa16bd4 100644 --- a/src/Compilers/CSharp/Portable/Generated/CSharpSyntaxGenerator/CSharpSyntaxGenerator.SourceGenerator/Syntax.xml.Internal.Generated.cs +++ b/src/Compilers/CSharp/Portable/Generated/CSharpSyntaxGenerator/CSharpSyntaxGenerator.SourceGenerator/Syntax.xml.Internal.Generated.cs @@ -31740,7 +31740,8 @@ public OperatorDeclarationSyntax OperatorDeclaration(CoreSyntax.SyntaxList + diff --git a/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs b/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs index 717c86bb6493b..841028bfa9058 100644 --- a/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs +++ b/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs @@ -1001,6 +1001,24 @@ public void TestConversionOperatorDeclaration() "public static implicit operator global::System.Decimal(global::System.Byte value)\r\n{\r\n}"); } + [Fact, WorkItem(77101, "https://github.com/dotnet/roslyn/issues/77101")] + public void TestExplicitOperatorFromSymbol() + { + var compilation = CSharpCompilation.Create("Test", [SyntaxFactory.ParseSyntaxTree(""" + public class C + { + public static explicit operator int(C c) => 0; + } + """)]); + + var c = compilation.GetTypeByMetadataName("C"); + var op = c.GetMembers().OfType().Where(m => m.MethodKind == MethodKind.Conversion).Single(); + + VerifySyntax( + Generator.OperatorDeclaration(op), + "public static explicit operator global::System.Int32(global::C c)\r\n{\r\n}"); + } + [Fact] public void TestConstructorDeclaration() { From a68f3e4250c42d117e500da18c11dca6ba088c19 Mon Sep 17 00:00:00 2001 From: Eric StJohn Date: Thu, 6 Feb 2025 18:40:22 -0800 Subject: [PATCH 2/2] Update to fix the `checked` operator --- .../Syntax.xml.Internal.Generated.cs | 6 ++---- .../Syntax.xml.Main.Generated.cs | 3 +-- src/Compilers/CSharp/Portable/Syntax/Syntax.xml | 1 - .../CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs | 4 ++-- .../CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs | 6 +++--- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Generated/CSharpSyntaxGenerator/CSharpSyntaxGenerator.SourceGenerator/Syntax.xml.Internal.Generated.cs b/src/Compilers/CSharp/Portable/Generated/CSharpSyntaxGenerator/CSharpSyntaxGenerator.SourceGenerator/Syntax.xml.Internal.Generated.cs index 0c6bc0fa16bd4..78d8aedb8b8f2 100644 --- a/src/Compilers/CSharp/Portable/Generated/CSharpSyntaxGenerator/CSharpSyntaxGenerator.SourceGenerator/Syntax.xml.Internal.Generated.cs +++ b/src/Compilers/CSharp/Portable/Generated/CSharpSyntaxGenerator/CSharpSyntaxGenerator.SourceGenerator/Syntax.xml.Internal.Generated.cs @@ -31740,8 +31740,7 @@ public OperatorDeclarationSyntax OperatorDeclaration(CoreSyntax.SyntaxList - diff --git a/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs b/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs index 2c005f7ad2bd6..d5c6575451d44 100644 --- a/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs +++ b/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs @@ -277,7 +277,7 @@ private protected override SyntaxNode OperatorDeclaration(string operatorName, b var modifierList = AsModifierList(accessibility, modifiers, SyntaxKind.OperatorDeclaration); var attributes = default(SyntaxList); - if (operatorName is WellKnownMemberNames.ImplicitConversionName or WellKnownMemberNames.ExplicitConversionName) + if (operatorName is WellKnownMemberNames.ImplicitConversionName or WellKnownMemberNames.ExplicitConversionName or WellKnownMemberNames.CheckedExplicitConversionName) { var isImplicit = operatorName is WellKnownMemberNames.ImplicitConversionName; return SyntaxFactory.ConversionOperatorDeclaration( @@ -285,7 +285,7 @@ private protected override SyntaxNode OperatorDeclaration(string operatorName, b isImplicit ? ImplicitKeyword : ExplicitKeyword, explicitInterfaceSpecifier: null, OperatorKeyword, - checkedKeyword: default, + checkedKeyword: CSharp.SyntaxFacts.IsCheckedOperator(operatorName) ? CheckedKeyword : default, returnTypeNode, parameterList, body, expressionBody: null, semicolon); } else diff --git a/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs b/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs index 841028bfa9058..9883526bd0041 100644 --- a/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs +++ b/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs @@ -1002,12 +1002,12 @@ public void TestConversionOperatorDeclaration() } [Fact, WorkItem(77101, "https://github.com/dotnet/roslyn/issues/77101")] - public void TestExplicitOperatorFromSymbol() + public void TestExplicitCheckedOperatorFromSymbol() { var compilation = CSharpCompilation.Create("Test", [SyntaxFactory.ParseSyntaxTree(""" public class C { - public static explicit operator int(C c) => 0; + public static explicit operator checked int(C c) => 0; } """)]); @@ -1016,7 +1016,7 @@ public class C VerifySyntax( Generator.OperatorDeclaration(op), - "public static explicit operator global::System.Int32(global::C c)\r\n{\r\n}"); + "public static explicit operator checked global::System.Int32(global::C c)\r\n{\r\n}"); } [Fact]