Skip to content

MethodDeclarationVisitor.VisitInternal Crashing Processing Parameter Named "struct" #147

@lordmilko

Description

@lordmilko

While attempting to convert a Java file, JavaToCSharp crashed while parsing the following method parameter

Structure struct

struct is a keyword in C#, but not in Java. As such, JavaToCSharp.Declarations.MethodDeclarationVisitor.VisitInternal was crashing here

var paramSyntax = SyntaxFactory.Parameter(
    attributeLists: [],
    modifiers: modifiers,
    type: TypeHelper.ConvertTypeSyntax(type, arrayLevel),
    identifier: SyntaxFactory.ParseToken(identifier),
    @default: null);

on the basis that the value passed to the identifier parameter was invalid. SyntaxFactory.ParseToken will produce a token of type SyntaxKind.StructKeyword in response to parsing struct rather than a simple identifier token. Earlier in VisitInternal the method TypeHelper.EscapeIdentifier is called, which prepends @ to several types of keywords. Not all keywords are listed here however, so I have updated the list to specify all standard keywords as defined here. There are additional contextual keywords in C# but these should not apply for the purposes of escaping an identifier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions