Skip to content

Commit 76e0025

Browse files
committed
Rollup merge of rust-lang#27499 - barosl:macro-doc-raw-str-hashes, r=nikomatsakis
Any documentation comments that contain raw-string-looking sequences may pretty-print invalid code when expanding them, as the current logic always uses the `r"literal"` form, without appending any `#`s. This commit calculates the minimum number of `#`s required to wrap a comment correctly and appends `#`s appropriately. Fixes rust-lang#27489.
2 parents 59b7c90 + 1a8cdc0 commit 76e0025

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

src/libstd/macros.rs

+3
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ pub mod builtin {
365365
/// stringification of all the tokens passed to the macro. No restrictions
366366
/// are placed on the syntax of the macro invocation itself.
367367
///
368+
/// Note that the expanded results of the input tokens may change in the
369+
/// future. You should be careful if you rely on the output.
370+
///
368371
/// # Examples
369372
///
370373
/// ```

src/libsyntax/ast.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1174,14 +1174,28 @@ impl TokenTree {
11741174
}
11751175
(&TokenTree::Token(sp, token::DocComment(name)), _) => {
11761176
let stripped = strip_doc_comment_decoration(&name.as_str());
1177+
1178+
// Searches for the occurrences of `"#*` and returns the minimum number of `#`s
1179+
// required to wrap the text.
1180+
let num_of_hashes = stripped.chars().scan(0, |cnt, x| {
1181+
*cnt = if x == '"' {
1182+
1
1183+
} else if *cnt != 0 && x == '#' {
1184+
*cnt + 1
1185+
} else {
1186+
0
1187+
};
1188+
Some(*cnt)
1189+
}).max().unwrap_or(0);
1190+
11771191
TokenTree::Delimited(sp, Rc::new(Delimited {
11781192
delim: token::Bracket,
11791193
open_span: sp,
11801194
tts: vec![TokenTree::Token(sp, token::Ident(token::str_to_ident("doc"),
11811195
token::Plain)),
11821196
TokenTree::Token(sp, token::Eq),
11831197
TokenTree::Token(sp, token::Literal(
1184-
token::StrRaw(token::intern(&stripped), 0), None))],
1198+
token::StrRaw(token::intern(&stripped), num_of_hashes), None))],
11851199
close_span: sp,
11861200
}))
11871201
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// The number of `#`s used to wrap the documentation comment should differ regarding the content.
12+
//
13+
// Related issue: #27489
14+
15+
macro_rules! homura {
16+
($x:expr, #[$y:meta]) => (assert_eq!($x, stringify!($y)))
17+
}
18+
19+
fn main() {
20+
homura! {
21+
r#"doc = r" Madoka""#,
22+
/// Madoka
23+
};
24+
25+
homura! {
26+
r##"doc = r#" One quote mark: ["]"#"##,
27+
/// One quote mark: ["]
28+
};
29+
30+
homura! {
31+
r##"doc = r#" Two quote marks: [""]"#"##,
32+
/// Two quote marks: [""]
33+
};
34+
35+
homura! {
36+
r#####"doc = r####" Raw string ending sequences: ["###]"####"#####,
37+
/// Raw string ending sequences: ["###]
38+
};
39+
}

0 commit comments

Comments
 (0)