Skip to content

Commit f92be7e

Browse files
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.
1 parent bb1d925 commit f92be7e

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

crates/mbe/src/syntax_bridge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ fn doc_comment_text(comment: &ast::Comment) -> SmolStr {
213213

214214
// Quote the string
215215
// Note that `tt::Literal` expect an escaped string
216-
let text = format!("{:?}", text.escape_debug().to_string());
216+
let text = format!("\"{}\"", text.escape_debug());
217217
text.into()
218218
}
219219

crates/mbe/src/tests/expand.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ fn test_meta_doc_comments() {
921921
MultiLines Doc
922922
*/
923923
}"#,
924-
"# [doc = \" Single Line Doc 1\"] # [doc = \"\\\\n MultiLines Doc\\\\n \"] fn bar () {}",
924+
"# [doc = \" Single Line Doc 1\"] # [doc = \"\\n MultiLines Doc\\n \"] fn bar () {}",
925925
);
926926
}
927927

@@ -944,7 +944,27 @@ fn test_meta_doc_comments_non_latin() {
944944
莊生曉夢迷蝴蝶,望帝春心託杜鵑。
945945
*/
946946
}"#,
947-
"# [doc = \" 錦瑟無端五十弦,一弦一柱思華年。\"] # [doc = \"\\\\n 莊生曉夢迷蝴蝶,望帝春心託杜鵑。\\\\n \"] fn bar () {}",
947+
"# [doc = \" 錦瑟無端五十弦,一弦一柱思華年。\"] # [doc = \"\\n 莊生曉夢迷蝴蝶,望帝春心託杜鵑。\\n \"] fn bar () {}",
948+
);
949+
}
950+
951+
#[test]
952+
fn test_meta_doc_comments_escaped_characters() {
953+
parse_macro(
954+
r#"
955+
macro_rules! foo {
956+
($(#[$ i:meta])+) => (
957+
$(#[$ i])+
958+
fn bar() {}
959+
)
960+
}
961+
"#,
962+
)
963+
.assert_expand_items(
964+
r#"foo! {
965+
/// \ " '
966+
}"#,
967+
r#"# [doc = " \\ \" \'"] fn bar () {}"#,
948968
);
949969
}
950970

0 commit comments

Comments
 (0)