fix(rust) reuse the string rules inside attributes - #4461
Open
MaksZhukov wants to merge 3 commits into
Open
Conversation
The `meta` mode for Rust attributes declared its own minimal string rule, so only plain `"..."` literals were recognised inside `#[...]`. Raw strings and byte strings were mis-highlighted: in `#[doc = r#"a "quoted" word"#]` the literal was cut at the first inner quote, leaving the rest of the attribute wrongly scoped. Attributes hold arbitrary token trees, so any string literal that is valid elsewhere is valid there too. Extract the existing string modes into named constants and reuse them in `meta` instead of duplicating a weaker version. The character-literal mode is deliberately left out of `meta` so that lifetimes in attribute token trees (`#[foo(Bar<'a>)]`) are not treated as an unterminated char literal. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
joshgoebel
reviewed
Aug 8, 2026
joshgoebel
reviewed
Aug 8, 2026
joshgoebel
requested changes
Aug 8, 2026
rustc rejects raw string literals delimited by more than 255 # symbols,
so tighten the rule from #* to #{0,255} and add a many-hash spec sample.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Build Size ReportChanges to minified artifacts in 5 files changedTotal change -42 B View Changes
|
joshgoebel
reviewed
Aug 10, 2026
Comment on lines
+171
to
+177
| begin: /b?'/, | ||
| end: /'/, | ||
| contains: [ | ||
| { | ||
| scope: "char.escape", | ||
| match: /\\('|"|\\|\w|x\w{2}|u\w{4}|U\w{8})/ | ||
| } |
Member
There was a problem hiding this comment.
is CHARACTER meant to match a single character?
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.
Resolves #3817
Changes
The
metamode for Rust attributes declared its own minimal string rule ("…"+BACKSLASH_ESCAPE), so only plain double-quoted literals were recognised inside#[...]. Raw strings and byte strings were mis-highlighted:#[doc = r#"a "quoted" word"#]was cut at the first inner quote, so
quotedfell out of the string and the rest of the attribute was wrongly scoped.Attributes hold arbitrary token trees, so any string literal that is valid elsewhere is valid there too — which answers the question raised in the issue. This extracts the existing string modes into named constants (
QUOTE_STRING,RAW_STRING,CHARACTER) and reusesQUOTE_STRING/RAW_STRINGinsidemetainstead of duplicating a weaker version. The top-levelcontainsorder is unchanged.The character-literal mode is deliberately not added to
meta, so lifetimes in attribute token trees (#[foo(Bar<'a>)]) are not treated as an unterminated char literal.Note on the original report: the exact snippet in the issue (
#[error("\" appears in a string")]) has been highlighted correctly since 11.10.0, but the underlying cause it pointed at —metanot reusing the string rules — was still present. Before/after for the remaining cases:#[doc = r#"a "quoted" word"#]r#outside the string, literal split in twor#"…"#is one string#[doc = b"bytes"]boutside the stringb"bytes"is one stringr##"…"##in an attributeHow to test
Or in the demo/playground, paste the Rust snippets above.
Checklist
test/markup/rust/strings.txt/.expect.txt— raw, byte, multi-line raw strings in attributes, plus a lifetime-in-attribute regression case)CHANGES.md🤖 Generated with Claude Code