Skip to content

Fix nested block comment grammar - #2348

Merged
traviscross merged 10 commits into
rust-lang:masterfrom
ehuss:fix-more-comments
Sep 2, 2026
Merged

Fix nested block comment grammar#2348
traviscross merged 10 commits into
rust-lang:masterfrom
ehuss:fix-more-comments

Conversation

@ehuss

@ehuss ehuss commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

This fixes some issues with nested block comments, particularly with the way it handled CRs.

Fixes #2333

These were identified by matheww in
rust-lang#2325 (comment)
and we should have captured them when they were fixed.
When the grammar check fails, it prints the input on the screen.
However, when there are embedded CRs, it disrupts the output. This
replaces it with the Unicode character for CR Symbol.
These are some problems identified in
rust-lang#2333.
This adds a BLOCK_CHAR rule so that both block comments and doc-block
comments match each other. This is just done for aesthetics.
This reverts rust-lang@5b328bc
which is no longer needed due to rust-lang#2331
changing shebang to use its own dedicated comment rule.
This fixes some issues with nested block comments when they contain
carriage returns.

The old logic was that a block comment could contain nested block
comments of the form of either regular block comments or doc-block
comments. This wasn't correct, though, because when inter-mixing regular
block comments and doc-block comments, the rules are different with
respect to things like carriage returns. However, in reality, when rustc
encounters a regular block comment, it just eats everything that looks
block-like without any validation. And conversely, for a doc-block
comment, it requires that everything inside does not contain a carriage
return, even if it is a nested regular block comment.

The logic for how nested comments are handled is at
https://github.com/rust-lang/rust/blob/a4330234a776684c36428d001721d0320d24dd77/compiler/rustc_lexer/src/lib.rs#L783-L815.
This includes the tests from the `cases` module. Eventually this should
be changed in some way. Either these tests should be moved to
rust-lang/rust, or there should be separate files that contain the
tests. For now it doesn't hurt to run some of these that don't show up
in either the permutation or the rust-lang/rust tests.
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Sep 1, 2026
In the grammar rules, we put spaces between parentheses and what comes
inside; let's do that here.
Before the fix in this branch, our grammar disagreed with rustc on
comments nested three layers deep.  Let's add tests for that and for
an inner block doc variant of the CR-starting case.
The only way to match the `NESTED_BLOCK_DOC_COMMENT` rule is for the
string to start with `/`, but we've already ruled that out with a
negative lookahead, making this alternative dead.  Let's remove it.
@traviscross
traviscross added this pull request to the merge queue Sep 2, 2026
Merged via the queue into rust-lang:master with commit e24eecf Sep 2, 2026
7 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Sep 2, 2026
@traviscross

Copy link
Copy Markdown
Contributor

Thanks @ehuss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grammar-check discrepancies from nested block comments

3 participants