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.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 8, 2026
Update books

## rust-lang/book

1 commits in 917544888a55e4da7109bdba8c88c893c0da70f4..1500248d8f230566e4ec9f27fcbb8fe9e2898ab1
2026-09-02 16:04:34 UTC to 2026-09-02 16:04:34 UTC

- Update to Rust 1.98 (rust-lang/book#4823)

## rust-lang/edition-guide

1 commits in f5abcf137698e5ad6ebed359d69654ff705346af..ab8544aeed7b792984366aa122ac19bd47ad9a2f
2026-08-25 19:50:54 UTC to 2026-08-25 19:50:54 UTC

- Update never-type-fallback for never type stabilization (rust-lang/edition-guide#384)

## rust-lang/reference

12 commits in 3b38834b39f732c64686f7c64aa29dcf3cd83ba5..e24eecf97b0c9a6dbac67191098204dc8a190aaa
2026-09-02 04:25:27 UTC to 2026-08-25 07:52:18 UTC

- Fix nested block comment grammar (rust-lang/reference#2348)
- dangling pointers: turn some consequences of the definition into notes (rust-lang/reference#2336)
- Fix the nightly grammar validation job (rust-lang/reference#2347)
- Order grammar summary deterministically (rust-lang/reference#2346)
- Remove leftover `types/textual.md` file (rust-lang/reference#2345)
- Fix non-leaf rules with bodies (rust-lang/reference#2344)
- Fix rule IDs not following the header hierarchy (rust-lang/reference#2343)
- Fix heading level of the `verbatim` modifier section (rust-lang/reference#2342)
- Fix `...diagnostics.deprecated...` rule ID (rust-lang/reference#2341)
- Update for stabilization of the never type (rust-lang/reference#2283)
- Add missing punctuation (rust-lang/reference#2339)
- Fix field-less `repr(C)` enum docs (rust-lang/reference#2018)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 8, 2026
Update books

## rust-lang/book

1 commits in 917544888a55e4da7109bdba8c88c893c0da70f4..1500248d8f230566e4ec9f27fcbb8fe9e2898ab1
2026-09-02 16:04:34 UTC to 2026-09-02 16:04:34 UTC

- Update to Rust 1.98 (rust-lang/book#4823)

## rust-lang/edition-guide

1 commits in f5abcf137698e5ad6ebed359d69654ff705346af..ab8544aeed7b792984366aa122ac19bd47ad9a2f
2026-08-25 19:50:54 UTC to 2026-08-25 19:50:54 UTC

- Update never-type-fallback for never type stabilization (rust-lang/edition-guide#384)

## rust-lang/reference

12 commits in 3b38834b39f732c64686f7c64aa29dcf3cd83ba5..e24eecf97b0c9a6dbac67191098204dc8a190aaa
2026-09-02 04:25:27 UTC to 2026-08-25 07:52:18 UTC

- Fix nested block comment grammar (rust-lang/reference#2348)
- dangling pointers: turn some consequences of the definition into notes (rust-lang/reference#2336)
- Fix the nightly grammar validation job (rust-lang/reference#2347)
- Order grammar summary deterministically (rust-lang/reference#2346)
- Remove leftover `types/textual.md` file (rust-lang/reference#2345)
- Fix non-leaf rules with bodies (rust-lang/reference#2344)
- Fix rule IDs not following the header hierarchy (rust-lang/reference#2343)
- Fix heading level of the `verbatim` modifier section (rust-lang/reference#2342)
- Fix `...diagnostics.deprecated...` rule ID (rust-lang/reference#2341)
- Update for stabilization of the never type (rust-lang/reference#2283)
- Add missing punctuation (rust-lang/reference#2339)
- Fix field-less `repr(C)` enum docs (rust-lang/reference#2018)
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