mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Escape characters in doc comments in macros correctly
Previously they were escaped twice, both by `.escape_default()` and the debug view of strings (`{:?}`). This leads to things like newlines or tabs in documentation comments being `\\n`, but we unescape literals only once, ending up with `\n`. This was hard to spot because CMark unescaped them (at least for `'` and `"`), but it did not do so in code blocks. This also was the root cause of #7781. This issue was solved by using `.escape_debug()` instead of `.escape_default()`, but the real issue remained. We can bring the `.escape_default()` back by now, however I didn't do it because it is probably slower than `.escape_debug()` (more work to do), and also in order to change the code the least.
This commit is contained in:
parent
bb1d925dab
commit
f92be7eaab
@ -213,7 +213,7 @@ fn doc_comment_text(comment: &ast::Comment) -> SmolStr {
|
|||||||
|
|
||||||
// Quote the string
|
// Quote the string
|
||||||
// Note that `tt::Literal` expect an escaped string
|
// Note that `tt::Literal` expect an escaped string
|
||||||
let text = format!("{:?}", text.escape_debug().to_string());
|
let text = format!("\"{}\"", text.escape_debug());
|
||||||
text.into()
|
text.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -921,7 +921,7 @@ fn test_meta_doc_comments() {
|
|||||||
MultiLines Doc
|
MultiLines Doc
|
||||||
*/
|
*/
|
||||||
}"#,
|
}"#,
|
||||||
"# [doc = \" Single Line Doc 1\"] # [doc = \"\\\\n MultiLines Doc\\\\n \"] fn bar () {}",
|
"# [doc = \" Single Line Doc 1\"] # [doc = \"\\n MultiLines Doc\\n \"] fn bar () {}",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -944,7 +944,27 @@ fn test_meta_doc_comments_non_latin() {
|
|||||||
莊生曉夢迷蝴蝶,望帝春心託杜鵑。
|
莊生曉夢迷蝴蝶,望帝春心託杜鵑。
|
||||||
*/
|
*/
|
||||||
}"#,
|
}"#,
|
||||||
"# [doc = \" 錦瑟無端五十弦,一弦一柱思華年。\"] # [doc = \"\\\\n 莊生曉夢迷蝴蝶,望帝春心託杜鵑。\\\\n \"] fn bar () {}",
|
"# [doc = \" 錦瑟無端五十弦,一弦一柱思華年。\"] # [doc = \"\\n 莊生曉夢迷蝴蝶,望帝春心託杜鵑。\\n \"] fn bar () {}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_meta_doc_comments_escaped_characters() {
|
||||||
|
parse_macro(
|
||||||
|
r#"
|
||||||
|
macro_rules! foo {
|
||||||
|
($(#[$ i:meta])+) => (
|
||||||
|
$(#[$ i])+
|
||||||
|
fn bar() {}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.assert_expand_items(
|
||||||
|
r#"foo! {
|
||||||
|
/// \ " '
|
||||||
|
}"#,
|
||||||
|
r#"# [doc = " \\ \" \'"] fn bar () {}"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user