Skip to content

Fix doc comment parsing in macros. #27056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Jul 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,10 +636,10 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {

generate_pattern_iterators! {
forward:
#[doc="Created with the method `.split()`."]
/// Created with the method `.split()`.
struct Split;
reverse:
#[doc="Created with the method `.rsplit()`."]
/// Created with the method `.rsplit()`.
struct RSplit;
stability:
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -650,10 +650,10 @@ generate_pattern_iterators! {

generate_pattern_iterators! {
forward:
#[doc="Created with the method `.split_terminator()`."]
/// Created with the method `.split_terminator()`.
struct SplitTerminator;
reverse:
#[doc="Created with the method `.rsplit_terminator()`."]
/// Created with the method `.rsplit_terminator()`.
struct RSplitTerminator;
stability:
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -696,10 +696,10 @@ impl<'a, P: Pattern<'a>> SplitNInternal<'a, P> {

generate_pattern_iterators! {
forward:
#[doc="Created with the method `.splitn()`."]
/// Created with the method `.splitn()`.
struct SplitN;
reverse:
#[doc="Created with the method `.rsplitn()`."]
/// Created with the method `.rsplitn()`.
struct RSplitN;
stability:
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -730,10 +730,10 @@ impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {

generate_pattern_iterators! {
forward:
#[doc="Created with the method `.match_indices()`."]
/// Created with the method `.match_indices()`.
struct MatchIndices;
reverse:
#[doc="Created with the method `.rmatch_indices()`."]
/// Created with the method `.rmatch_indices()`.
struct RMatchIndices;
stability:
#[unstable(feature = "str_match_indices",
Expand Down Expand Up @@ -771,10 +771,10 @@ impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {

generate_pattern_iterators! {
forward:
#[doc="Created with the method `.matches()`."]
/// Created with the method `.matches()`.
struct Matches;
reverse:
#[doc="Created with the method `.rmatches()`."]
/// Created with the method `.rmatches()`.
struct RMatches;
stability:
#[stable(feature = "str_matches", since = "1.2.0")]
Expand Down
18 changes: 15 additions & 3 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use owned_slice::OwnedSlice;
use parse::token::{InternedString, str_to_ident};
use parse::token;
use parse::lexer;
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
use print::pprust;
use ptr::P;

Expand Down Expand Up @@ -1079,7 +1080,12 @@ pub enum TokenTree {
impl TokenTree {
pub fn len(&self) -> usize {
match *self {
TtToken(_, token::DocComment(_)) => 2,
TtToken(_, token::DocComment(name)) => {
match doc_comment_style(name.as_str()) {
AttrOuter => 2,
AttrInner => 3
}
}
TtToken(_, token::SpecialVarNt(..)) => 2,
TtToken(_, token::MatchNt(..)) => 3,
TtDelimited(_, ref delimed) => {
Expand All @@ -1097,14 +1103,20 @@ impl TokenTree {
(&TtToken(sp, token::DocComment(_)), 0) => {
TtToken(sp, token::Pound)
}
(&TtToken(sp, token::DocComment(name)), 1) => {
(&TtToken(sp, token::DocComment(name)), 1)
if doc_comment_style(name.as_str()) == AttrInner => {
TtToken(sp, token::Not)
}
(&TtToken(sp, token::DocComment(name)), _) => {
let stripped = strip_doc_comment_decoration(name.as_str());
TtDelimited(sp, Rc::new(Delimited {
delim: token::Bracket,
open_span: sp,
tts: vec![TtToken(sp, token::Ident(token::str_to_ident("doc"),
token::Plain)),
TtToken(sp, token::Eq),
TtToken(sp, token::Literal(token::StrRaw(name, 0), None))],
TtToken(sp, token::Literal(
token::StrRaw(token::intern(&stripped), 0), None))],
close_span: sp,
}))
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/parse-fail/macro-doc-comments-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! outer {
(#[$outer:meta]) => ()
}

outer! {
//! Inner
} //~^ ERROR no rules expected the token `!`

fn main() { }
19 changes: 19 additions & 0 deletions src/test/parse-fail/macro-doc-comments-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! inner {
(#![$inner:meta]) => ()
}

inner! {
/// Outer
} //~^ ERROR no rules expected the token `[`

fn main() { }
33 changes: 33 additions & 0 deletions src/test/run-pass/macro-doc-comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! doc {
(
$(#[$outer:meta])*
mod $i:ident {
$(#![$inner:meta])*
}
) =>
(
$(#[$outer])*
pub mod $i {
$(#![$inner])*
}
)
}

doc! {
/// Outer doc
mod Foo {
//! Inner doc
}
}

fn main() { }
46 changes: 46 additions & 0 deletions src/test/rustdoc/issue-23812.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! doc {
(#[$outer:meta] mod $i:ident { #![$inner:meta] }) =>
(
#[$outer]
pub mod $i {
#![$inner]
}
)
}

doc! {
/// Outer comment
mod Foo {
//! Inner comment
}
}

// @has issue_23812/Foo/index.html
// @has - 'Outer comment'
// @!has - '/// Outer comment'
// @has - 'Inner comment'
// @!has - '//! Inner comment'


doc! {
/** Outer block comment */
mod Bar {
/*! Inner block comment */
}
}

// @has issue_23812/Bar/index.html
// @has - 'Outer block comment'
// @!has - '/** Outer block comment */'
// @has - 'Inner block comment'
// @!has - '/*! Inner block comment */'