Skip to content

fix: Don't source-map #[doc] strings that contain escapes - #23443

Merged
ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
simonether:doc-attr-escapes
Oct 9, 2026
Merged

ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
simonether:doc-attr-escapes

Conversation

@simonether

@simonether simonether commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #23259

Syntax highlighting panics on a #[doc = "..."] attribute that has escape sequences followed by non-ASCII text:

#[doc = "
~~~
let s = \"中华Việt Nam\";
8: ệ
"]
fn main() {}

Docs::extend_with_doc_attr stores the unescaped string, but records line offsets as if they pointed into the source literal. Each \" takes two bytes in the source and one in the docs, so every line after an escape maps to a range shifted left by one byte per escape before it. Doctest injection passes that range to the HTML renderer, which slices the source in the middle of ệ.

With this change, the docs keep their source map only when the unescaped value is identical to the text between the quotes, i.e. the string has no escapes or is a raw string. Otherwise the string goes through extend_with_unmapped_doc_str, the path macro-expanded docs already use. find_ast_range returns None for those lines, so they get no doctest highlighting, and nothing panics.

rustc makes the same check in source_span_for_markdown_range_inner. It maps a markdown range to the source by offset only when a single non-sugared fragment has the same bytes as the docs:
https://github.com/rust-lang/rust/blob/78ae589adaba26eed6660acc99a6eb515605e0ab/compiler/rustc_resolve/src/rustdoc.rs#L574-L591

When the bytes differ, rustc falls back to searching the source for the substring (L593-L638). I didn't port that fallback. The trade-off: in a string with escapes, lines before the first escape used to map correctly and now lose their highlighting too. The FIXME now points at mapping the text between escapes, which would bring those lines back.

Testing: regression_23259 in syntax_highlighting/tests.rs uses the example from the issue. On master it panics with the message from the issue; with the fix it passes. The ide, hir-def and ide-db test suites pass, and no other snapshot changed.

AI disclosure: Claude Code helped identify the root cause and drafted parts of the implementation. I reviewed all changes line by line, tested them, and take full responsibility for the code.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 30, 2026
Comment thread crates/hir-def/src/attrs/docs.rs Resolved
Comment thread .typos.toml Resolved

@ChayimFriedman2 ChayimFriedman2 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ChayimFriedman2
ChayimFriedman2 added this pull request to the merge queue Oct 9, 2026
@simonether

simonether commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

Merged via the queue into rust-lang:master with commit 4b0d084 Oct 9, 2026
20 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 9, 2026
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.

highlight: panic: end byte index 48 is not a char boundary; it is inside 'ệ' (bytes 47..50 of string)

3 participants