Skip to content

Commit ed49bad

Browse files
committed
Auto merge of #27056 - Eljay:doc-comments, r=nikomatsakis
Fixes #23812 by stripping the decoration when desugaring macro doc comments into #[doc] attributes, and detects whether the attribute should be inner or outer style and outputs the appropriate token tree.
2 parents 2fe870a + a219917 commit ed49bad

File tree

6 files changed

+142
-13
lines changed

6 files changed

+142
-13
lines changed

src/libcore/str/mod.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -636,10 +636,10 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
636636

637637
generate_pattern_iterators! {
638638
forward:
639-
#[doc="Created with the method `.split()`."]
639+
/// Created with the method `.split()`.
640640
struct Split;
641641
reverse:
642-
#[doc="Created with the method `.rsplit()`."]
642+
/// Created with the method `.rsplit()`.
643643
struct RSplit;
644644
stability:
645645
#[stable(feature = "rust1", since = "1.0.0")]
@@ -650,10 +650,10 @@ generate_pattern_iterators! {
650650

651651
generate_pattern_iterators! {
652652
forward:
653-
#[doc="Created with the method `.split_terminator()`."]
653+
/// Created with the method `.split_terminator()`.
654654
struct SplitTerminator;
655655
reverse:
656-
#[doc="Created with the method `.rsplit_terminator()`."]
656+
/// Created with the method `.rsplit_terminator()`.
657657
struct RSplitTerminator;
658658
stability:
659659
#[stable(feature = "rust1", since = "1.0.0")]
@@ -696,10 +696,10 @@ impl<'a, P: Pattern<'a>> SplitNInternal<'a, P> {
696696

697697
generate_pattern_iterators! {
698698
forward:
699-
#[doc="Created with the method `.splitn()`."]
699+
/// Created with the method `.splitn()`.
700700
struct SplitN;
701701
reverse:
702-
#[doc="Created with the method `.rsplitn()`."]
702+
/// Created with the method `.rsplitn()`.
703703
struct RSplitN;
704704
stability:
705705
#[stable(feature = "rust1", since = "1.0.0")]
@@ -730,10 +730,10 @@ impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
730730

731731
generate_pattern_iterators! {
732732
forward:
733-
#[doc="Created with the method `.match_indices()`."]
733+
/// Created with the method `.match_indices()`.
734734
struct MatchIndices;
735735
reverse:
736-
#[doc="Created with the method `.rmatch_indices()`."]
736+
/// Created with the method `.rmatch_indices()`.
737737
struct RMatchIndices;
738738
stability:
739739
#[unstable(feature = "str_match_indices",
@@ -771,10 +771,10 @@ impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {
771771

772772
generate_pattern_iterators! {
773773
forward:
774-
#[doc="Created with the method `.matches()`."]
774+
/// Created with the method `.matches()`.
775775
struct Matches;
776776
reverse:
777-
#[doc="Created with the method `.rmatches()`."]
777+
/// Created with the method `.rmatches()`.
778778
struct RMatches;
779779
stability:
780780
#[stable(feature = "str_matches", since = "1.2.0")]

src/libsyntax/ast.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ use owned_slice::OwnedSlice;
6363
use parse::token::{InternedString, str_to_ident};
6464
use parse::token;
6565
use parse::lexer;
66+
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
6667
use print::pprust;
6768
use ptr::P;
6869

@@ -1079,7 +1080,12 @@ pub enum TokenTree {
10791080
impl TokenTree {
10801081
pub fn len(&self) -> usize {
10811082
match *self {
1082-
TtToken(_, token::DocComment(_)) => 2,
1083+
TtToken(_, token::DocComment(name)) => {
1084+
match doc_comment_style(name.as_str()) {
1085+
AttrOuter => 2,
1086+
AttrInner => 3
1087+
}
1088+
}
10831089
TtToken(_, token::SpecialVarNt(..)) => 2,
10841090
TtToken(_, token::MatchNt(..)) => 3,
10851091
TtDelimited(_, ref delimed) => {
@@ -1097,14 +1103,20 @@ impl TokenTree {
10971103
(&TtToken(sp, token::DocComment(_)), 0) => {
10981104
TtToken(sp, token::Pound)
10991105
}
1100-
(&TtToken(sp, token::DocComment(name)), 1) => {
1106+
(&TtToken(sp, token::DocComment(name)), 1)
1107+
if doc_comment_style(name.as_str()) == AttrInner => {
1108+
TtToken(sp, token::Not)
1109+
}
1110+
(&TtToken(sp, token::DocComment(name)), _) => {
1111+
let stripped = strip_doc_comment_decoration(name.as_str());
11011112
TtDelimited(sp, Rc::new(Delimited {
11021113
delim: token::Bracket,
11031114
open_span: sp,
11041115
tts: vec![TtToken(sp, token::Ident(token::str_to_ident("doc"),
11051116
token::Plain)),
11061117
TtToken(sp, token::Eq),
1107-
TtToken(sp, token::Literal(token::StrRaw(name, 0), None))],
1118+
TtToken(sp, token::Literal(
1119+
token::StrRaw(token::intern(&stripped), 0), None))],
11081120
close_span: sp,
11091121
}))
11101122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
macro_rules! outer {
12+
(#[$outer:meta]) => ()
13+
}
14+
15+
outer! {
16+
//! Inner
17+
} //~^ ERROR no rules expected the token `!`
18+
19+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
macro_rules! inner {
12+
(#![$inner:meta]) => ()
13+
}
14+
15+
inner! {
16+
/// Outer
17+
} //~^ ERROR no rules expected the token `[`
18+
19+
fn main() { }
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
macro_rules! doc {
12+
(
13+
$(#[$outer:meta])*
14+
mod $i:ident {
15+
$(#![$inner:meta])*
16+
}
17+
) =>
18+
(
19+
$(#[$outer])*
20+
pub mod $i {
21+
$(#![$inner])*
22+
}
23+
)
24+
}
25+
26+
doc! {
27+
/// Outer doc
28+
mod Foo {
29+
//! Inner doc
30+
}
31+
}
32+
33+
fn main() { }

src/test/rustdoc/issue-23812.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
macro_rules! doc {
12+
(#[$outer:meta] mod $i:ident { #![$inner:meta] }) =>
13+
(
14+
#[$outer]
15+
pub mod $i {
16+
#![$inner]
17+
}
18+
)
19+
}
20+
21+
doc! {
22+
/// Outer comment
23+
mod Foo {
24+
//! Inner comment
25+
}
26+
}
27+
28+
// @has issue_23812/Foo/index.html
29+
// @has - 'Outer comment'
30+
// @!has - '/// Outer comment'
31+
// @has - 'Inner comment'
32+
// @!has - '//! Inner comment'
33+
34+
35+
doc! {
36+
/** Outer block comment */
37+
mod Bar {
38+
/*! Inner block comment */
39+
}
40+
}
41+
42+
// @has issue_23812/Bar/index.html
43+
// @has - 'Outer block comment'
44+
// @!has - '/** Outer block comment */'
45+
// @has - 'Inner block comment'
46+
// @!has - '/*! Inner block comment */'

0 commit comments

Comments
 (0)