diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index db173d0830815..cc44bddc43e97 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1097,6 +1097,17 @@ impl TokenTree { } (&TtToken(sp, token::DocComment(name)), _) => { let stripped = strip_doc_comment_decoration(&name.as_str()); + + // Count number of consecutive characters in string. + fn max_consecutive(s: &str, c: char) -> usize { + let it = s.chars().scan(0, |count, x| { + if x == c { *count += 1 } else { *count = 0 } + Some(*count) + }); + it.max().unwrap_or(0) + } + let delims = max_consecutive(&stripped, '#') + 1; + TtDelimited(sp, Rc::new(Delimited { delim: token::Bracket, open_span: sp, @@ -1104,7 +1115,7 @@ impl TokenTree { token::Plain)), TtToken(sp, token::Eq), TtToken(sp, token::Literal( - token::StrRaw(token::intern(&stripped), 0), None))], + token::StrRaw(token::intern(&stripped), delims), None))], close_span: sp, })) } diff --git a/src/test/run-pass/issue-27489.rs b/src/test/run-pass/issue-27489.rs new file mode 100644 index 0000000000000..9eafc21b1def1 --- /dev/null +++ b/src/test/run-pass/issue-27489.rs @@ -0,0 +1,42 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +macro_rules! stringify_passthrough { + ($t:item) => { + pub const TEST: &'static str = stringify!($t); + } +} + +mod foo { + stringify_passthrough! { + /// Hello world + pub fn bar() { } + } +} + +mod bar { + stringify_passthrough! { + /// Hello # world + pub fn bar() { } + } +} + +mod baz { + stringify_passthrough! { + /// ## Hello "### world # + pub fn bar() { } + } +} + +fn main() { + assert_eq!(foo::TEST, "#[doc = r#\" Hello world\"#]\npub fn bar() { }"); + assert_eq!(bar::TEST, "#[doc = r##\" Hello # world\"##]\npub fn bar() { }"); + assert_eq!(baz::TEST, "#[doc = r####\" ## Hello \"### world #\"####]\npub fn bar() { }"); +}