Auto merge of #16158 - saiintbrisson:fix/mbe/desugar-comment-to-raw-string, r=Veykril

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:
59096cdad0/compiler/rustc_ast/src/tokenstream.rs (L662-L671)
This commit is contained in:
bors 2023-12-19 07:00:35 +00:00
commit 484525f8d7
2 changed files with 22 additions and 7 deletions

View file

@ -1218,8 +1218,10 @@ m! {
macro_rules! m {
($(#[$m:meta])+) => ( $(#[$m])+ fn bar() {} )
}
#[doc = " Single Line Doc 1"]
#[doc = "\n MultiLines Doc\n "] fn bar() {}
#[doc = r" Single Line Doc 1"]
#[doc = r"
MultiLines Doc
"] fn bar() {}
"##]],
);
}
@ -1260,8 +1262,10 @@ m! {
macro_rules! m {
($(#[$ m:meta])+) => ( $(#[$m])+ fn bar() {} )
}
#[doc = " 錦瑟無端五十弦,一弦一柱思華年。"]
#[doc = "\n 莊生曉夢迷蝴蝶,望帝春心託杜鵑。\n "] fn bar() {}
#[doc = r" 錦瑟無端五十弦,一弦一柱思華年。"]
#[doc = r"
"] fn bar() {}
"##]],
);
}
@ -1281,7 +1285,7 @@ m! {
macro_rules! m {
($(#[$m:meta])+) => ( $(#[$m])+ fn bar() {} )
}
#[doc = " \\ \" \'"] fn bar() {}
#[doc = r#" \ " '"#] fn bar() {}
"##]],
);
}

View file

@ -406,9 +406,20 @@ fn doc_comment_text(comment: &ast::Comment) -> SmolStr {
text = &text[0..text.len() - 2];
}
// Quote the string
let mut num_of_hashes = 0;
let mut count = 0;
for ch in text.chars() {
count = match ch {
'"' => 1,
'#' if count > 0 => count + 1,
_ => 0,
};
num_of_hashes = num_of_hashes.max(count);
}
// Quote raw string with delimiters
// Note that `tt::Literal` expect an escaped string
let text = format!("\"{}\"", text.escape_debug());
let text = format!("r{delim}\"{text}\"{delim}", delim = "#".repeat(num_of_hashes));
text.into()
}