Fix nested block comment grammar - #2348
Merged
Merged
Conversation
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.
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
force-pushed
the
fix-more-comments
branch
from
September 2, 2026 04:23
8fbbe9b to
c49c560
Compare
Contributor
|
Thanks @ehuss. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes some issues with nested block comments, particularly with the way it handled CRs.
Fixes #2333