KirillOsenkov / RoslynQuoter

Roslyn tool that for a given C# program shows syntax tree API calls to construct its syntax tree
http://roslynquoter.azurewebsites.net
Apache License 2.0
918 stars 118 forks source link

Wrong API call for record struct #82

Closed Youssef1313 closed 11 months ago

Youssef1313 commented 11 months ago
public readonly record struct A();

Currently produces:

CompilationUnit()
.WithMembers(
    SingletonList<MemberDeclarationSyntax>(
        RecordDeclaration(
            Token(SyntaxKind.RecordKeyword),
            Identifier("A"))
        .WithModifiers(
            TokenList(
                new []{
                    Token(SyntaxKind.PublicKeyword),
                    Token(SyntaxKind.ReadOnlyKeyword)}))
        .WithClassOrStructKeyword(
            Token(SyntaxKind.StructKeyword))
        .WithParameterList(
            ParameterList())
        .WithSemicolonToken(
            Token(SyntaxKind.SemicolonToken))))
.NormalizeWhitespace()

If this is added to a compilation, the compilation is going to have error CS0106: The "readonly" modifier is invalid for this element.

The record declaration call should be:

        RecordDeclaration(
            SyntaxKind.RecordStructDeclaration,
            Token(SyntaxKind.RecordKeyword),
            Identifier("A"))

Found this while looking into https://stackoverflow.com/q/77094804/5108631

KirillOsenkov commented 11 months ago

Thanks for reporting!

It now generates this:

RecordDeclaration(
    SyntaxKind.RecordStructDeclaration,
    Token(SyntaxKind.RecordKeyword),
    Identifier("A"))
.WithClassOrStructKeyword(
    Token(SyntaxKind.StructKeyword))
.WithParameterList(
    ParameterList())
.WithSemicolonToken(
    Token(SyntaxKind.SemicolonToken))
.NormalizeWhitespace()