Skip to content

Commit cfd74e9

Browse files
committed
fix(mbe): desugar doc correctly for mbe
Fixes #16110. The way rust desugars doc comments when expanding macros is rendering it as raw strings delimited with hashes. Rust-analyzer wasn't aware of this, so the desugared doc comments wouldn't match correctly when on the LHS of macro declarations. This PR fixes this by porting the code used by rustc: https://github.com/rust-lang/rust/blob/4cfdbd328b7171b2328d11b950b1af0978d6b1ef/compiler/rustc_ast/src/tokenstream.rs#L6837
1 parent f663521 commit cfd74e9

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Diff for: crates/mbe/src/syntax_bridge.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,20 @@ fn doc_comment_text(comment: &ast::Comment) -> SmolStr {
406406
text = &text[0..text.len() - 2];
407407
}
408408

409-
// Quote the string
409+
let mut num_of_hashes = 0;
410+
let mut count = 0;
411+
for ch in text.chars() {
412+
count = match ch {
413+
'"' => 1,
414+
'#' if count > 0 => count + 1,
415+
_ => 0,
416+
};
417+
num_of_hashes = num_of_hashes.max(count);
418+
}
419+
420+
// Quote raw string with delimiters
410421
// Note that `tt::Literal` expect an escaped string
411-
let text = format!("\"{}\"", text.escape_debug());
422+
let text = format!("r{delim}\"{text}\"{delim}", delim = "#".repeat(num_of_hashes));
412423
text.into()
413424
}
414425

0 commit comments

Comments
 (0)