Skip to content
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

[SR-9616] SwiftSyntax is not following trivia rule #438

Open
swift-ci opened this issue Jan 8, 2019 · 3 comments
Open

[SR-9616] SwiftSyntax is not following trivia rule #438

swift-ci opened this issue Jan 8, 2019 · 3 comments
Labels
bug Compiler Parser Syntax

Comments

@swift-ci
Copy link
Contributor

@swift-ci swift-ci commented Jan 8, 2019

Previous ID SR-9616
Radar None
Original Reporter inamiy (JIRA User)
Type Bug

Attachment: Download

Environment
Additional Detail from JIRA
Votes 0
Component/s Compiler, SwiftSyntax
Labels Bug, Parser, Syntax
Assignee None
Priority Medium

md5: b19e5c1a45f2ad0872b99a66f9211c27

relates to:

  • SR-2373 Trailing doc comment should not be attached to the next declaration

Issue Description:

According to the libSyntax documentation:

1. A token owns all of its trailing trivia up to, but not including, the next newline character.
2. Looking backward in the text, a token owns all of the leading trivia up to and including the first contiguous sequence of newlines characters.

The following code should have leading / trailing trivias as follows:

let x = 1 // This should be "1"'s trailing trivia
/// This should be `var`'s leading trivia
var y = 2

But actually, both comments are bundled as `var`'s leading trivia.

You can test the code at https://swift-ast-explorer.kishikawakatsumi.com/ (screenshot attached).

By the way, in TypeScript, it seems to work as is:
https://github.com/basarat/typescript-book/blob/master/docs/compiler/ast-trivia.md

@rintaro
Copy link
Mannequin

@rintaro rintaro mannequin commented Jan 8, 2019

I agree this is a bug.

https://github.com/apple/swift/blob/75033ed05a8f5ecf79e77b41dabd2c3863cbc9e5/lib/Parse/Lexer.cpp#L2560
I intentionally did this because, currently, we want such comments to be attached to the next token (i.e. possible start of a declaration on the next line).

https://bugs.swift.org/browse/SR-2373

Lexing trailing-comment as trailingTrivia makes parsing difficult as long as we keep current "trailing doc-comment" behavior. @nkcsgexi @akyrtzi What do you think?

@nkcsgexi
Copy link
Member

@nkcsgexi nkcsgexi commented Jan 8, 2019

This example inamiy (JIRA User) gave here is an obvious bug since the comments are in different forms ("//" and "///"), so it's reasonable to assume they're
commenting different pieces.

If they are both "///", i have no strong opinion on whether we split them as trailing and leading trivia or combine them as leading trivia, because the lexer cannot
be always right in either way.

@swift-ci
Copy link
Contributor Author

@swift-ci swift-ci commented Jan 9, 2019

Comment by Yasuhiro Inami (JIRA)

@nkcsgexi

If they are both "///", i have no strong opinion on whether we split them as trailing and leading trivia or combine them as leading trivia, because the lexer cannot
be always right in either way.

I think trailing and leading trivias should be separated in the above case too, and it's probably possible to distinguish them by checking if token exists in the same line or not.

If this can be done, it's exciting to see SR-2373 trailing-doc support can be implemented as well 🙂

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Compiler Parser Syntax
Projects
None yet
Development

No branches or pull requests

3 participants