Skip to content

swift should offer fixit for "#elif" #1221

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

Closed
ahoppen opened this issue Jan 11, 2023 · 4 comments
Closed

swift should offer fixit for "#elif" #1221

ahoppen opened this issue Jan 11, 2023 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers SwiftParser Bugs in the (new) Parser written in Swift

Comments

@ahoppen
Copy link
Member

ahoppen commented Jan 11, 2023

Description

Swift offers a fixit for #else if -> #elseif.

We should also offer a fixit for #elif, eg:

error: use of unknown directive '#elif'
                    #elif arch(arm64)
                    ^

error: unexpected 'if' keyword following '#else' conditional compilation directive; did you mean '#elseif'?
                    #else if arch(arm64)
                    ~~~~~~^~
                    #elseif

rdar://103506087

@ahoppen ahoppen added enhancement New feature or request good first issue Good for newcomers SwiftParser Bugs in the (new) Parser written in Swift labels Jan 11, 2023
@TiagoMaiaL
Copy link
Contributor

TiagoMaiaL commented Jan 16, 2023

I've started investigating this issue. I've used the following code to check on the parser diagnostics:

let sourceFile = Parser.parse(source: """
    #if arch(arm64)
    debugPrint("arm64")
    #elif arch(x86_64)
    debugPrint("x86_64")
    #endif
""")

let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: sourceFile)
print(diagnostics)
print(diagnostics.flatMap(\.fixIts).map(\.message.message))

The above code prints the following:

[3:6: consecutive statements on a line must be separated by ';']
["insert \';\'"]

Swift offers a fixit for #else if -> #elseif.

I've tried using ´#else if´ instead of ´#elseif´ in the code above, but here's the emitted diagnostic (which is different from the message in this issue):

[3:22: expected '{' in 'if' statement, 4:21: expected '}' to end 'if' statement]
["insert \'{\'", "insert \'}\'"]

@ahoppen Am I on the right track? Should I be looking for fixIts in ParseDiagnosticsGenerator?

Thanks in advance.

@bnbarham
Copy link
Contributor

bnbarham commented Jan 18, 2023

Thanks for having a look @TiagoMaiaL!

Swift offers a fixit for #else if -> #elseif.

Is referring to the C++ parser in the Swift compiler. It outputs a useful diagnostic on a #elseif. This bug is about adding a similar diagnostic to this parser + also handling elif as the current diagnostics for these cases aren't great (as you saw).

https://github.com/apple/swift-syntax/blob/main/Sources/SwiftParser/SwiftParser.docc/FixingBugs.md talks about this a little. In this case parsePoundIfDirective will need to be updated to produce an unexpected node for the #else + if and then a missing node for #elseif. Similar for the #elif case, ie. an unexpected for #elif and then missing #elseif. After that you'll want to add a custom diagnostic in the diagnostics generator.

@TiagoMaiaL
Copy link
Contributor

@ahoppen I think this can be finally closed, right? Thank you for the help!

@ahoppen
Copy link
Member Author

ahoppen commented May 5, 2023

Yes, thanks for your patience and seeing this Fix-It through 🙏🏽

@ahoppen ahoppen closed this as completed May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers SwiftParser Bugs in the (new) Parser written in Swift
Projects
None yet
Development

No branches or pull requests

3 participants