Skip to content

internal: Represent doc comments using their own node and not as COMMENT trivia - #23218

Open
ChayimFriedman2 wants to merge 1 commit into
rust-lang:masterfrom
ChayimFriedman2:doc-comment-no-trivia
Open

internal: Represent doc comments using their own node and not as COMMENT trivia#23218
ChayimFriedman2 wants to merge 1 commit into
rust-lang:masterfrom
ChayimFriedman2:doc-comment-no-trivia

Conversation

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

For two reasons:

  • This simplifies my work to fix FP unexpected token in input macro with doc comment #23088; to fix that issue, macros must have to be able to return doc comments (and not just desugared doc comments), and code in syntax-bridge doesn't expect macros to return trivia. Making doc comments non-trivia solves that.
  • It should simplify the work to attach trivia to tokens; doc comments have no obvious place to attach (for example, when between two attributes we must attach them to either the preceding ] or the following #, both will complicate code handling them).

Furthermore, arguably doc comments are really not a trivia: it's an error to put them in an unexpected place, and reason 2 above reveals that they're more like a kind of an attribute than a comment.

This touches a lot of places (especially assists etc.) subtly; I fixed what I found and the tests helped reveal more, but it's certainly possible some places are still not handling them correctly now.

CC @bit-aloo.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 23, 2026
@bit-aloo
bit-aloo self-requested a review August 23, 2026 11:13

@bit-aloo bit-aloo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have some places where we’re matching on attr and trivia or just trivia. We might need to handle doc comments there as well.

View changes since this review

Comment thread crates/syntax/src/ast/token_ext.rs Outdated
Comment on lines -387 to -394
do_check(
r"
/// foo $0$0omment
mod { }
",
"c",
14,
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we deleting this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I forgot to comment about this. Since incremental parsing is to-be-removed anyway when we rewrite rowan (and we currently only use it for completion), I didn't bother to investigate why this test fails.

Comment thread crates/syntax/src/ast/token_ext.rs
Comment on lines +678 to 686
/// Hello$0
/// world!
fn foo() {
/// Hello$0
/// world!
}
",
r"
/// Hello$0 world!
fn foo() {
/// Hello$0 world!
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why we changed this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An outer doc comment is invalid inside the body. Now the parser produces an error on this, which made this test fail.

For two reasons:

 - This simplifies my work to fix rust-lang#23088; to fix that issue, macros must have to be able to return doc comments (and not just desugared doc comments), and code in `syntax-bridge` doesn't expect macros to return trivia. Making doc comments non-trivia solves that.
 - It should simplify the work to attach trivia to tokens; doc comments have no obvious place to attach (for example, when between two attributes we must attach them to either the preceding `]` or the following `#`, both will complicate code handling them).

Furthermore, arguably doc comments are really not a trivia: it's an error to put them in an unexpected place, and reason 2 above reveals that they're more like a kind of an attribute than a comment.

This touches a lot of places (especially assists etc.) subtly; I fixed what I found and the tests helped reveal more, but it's certainly possible some places are still not handling them correctly now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FP unexpected token in input macro with doc comment

3 participants