Skip to content

Fix syntax generation of explict checked operator from symbol #77102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2025
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 @@ -277,15 +277,15 @@ private protected override SyntaxNode OperatorDeclaration(string operatorName, b
var modifierList = AsModifierList(accessibility, modifiers, SyntaxKind.OperatorDeclaration);
var attributes = default(SyntaxList<AttributeListSyntax>);

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(
attributes, modifierList,
isImplicit ? ImplicitKeyword : ExplicitKeyword,
explicitInterfaceSpecifier: null,
OperatorKeyword,
checkedKeyword: default,
checkedKeyword: CSharp.SyntaxFacts.IsCheckedOperator(operatorName) ? CheckedKeyword : default,
returnTypeNode, parameterList, body, expressionBody: null, semicolon);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 TestExplicitCheckedOperatorFromSymbol()
{
var compilation = CSharpCompilation.Create("Test", [SyntaxFactory.ParseSyntaxTree("""
public class C
{
public static explicit operator checked int(C c) => 0;
}
""")]);

var c = compilation.GetTypeByMetadataName("C");
var op = c.GetMembers().OfType<IMethodSymbol>().Where(m => m.MethodKind == MethodKind.Conversion).Single();

VerifySyntax<ConversionOperatorDeclarationSyntax>(
Generator.OperatorDeclaration(op),
"public static explicit operator checked global::System.Int32(global::C c)\r\n{\r\n}");
}

[Fact]
public void TestConstructorDeclaration()
{
Expand Down
Loading