Repository navigation
fix: Don't source-map #[doc] strings that contain escapes - #23443
Merged
Merged
Conversation
simonether
force-pushed
the
doc-attr-escapes
branch
from
September 30, 2026 16:50
7cca063 to
f3211c3
Compare
ChayimFriedman2
requested changes
Oct 7, 2026
simonether
force-pushed
the
doc-attr-escapes
branch
from
October 9, 2026 01:02
f3211c3 to
2ad5322
Compare
ChayimFriedman2
approved these changes
Oct 9, 2026
Contributor
Author
|
Thanks for the review! |
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.
Fixes #23259
Syntax highlighting panics on a
#[doc = "..."]attribute that has escape sequences followed by non-ASCII text:Docs::extend_with_doc_attrstores 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_rangereturnsNonefor 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_23259insyntax_highlighting/tests.rsuses the example from the issue. On master it panics with the message from the issue; with the fix it passes. Theide,hir-defandide-dbtest 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.