Skip to content

Rustdoc: memoize pub use-reexported macros so they don't appear twice in docs #40814

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 2 commits into from
Mar 30, 2017
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
22 changes: 20 additions & 2 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use syntax_pos::Span;

use rustc::hir::map as hir_map;
use rustc::hir::def::Def;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::middle::cstore::LoadedMacro;
use rustc::middle::privacy::AccessLevel;
use rustc::util::nodemap::FxHashSet;
Expand All @@ -48,6 +48,7 @@ pub struct RustdocVisitor<'a, 'tcx: 'a> {
inlining: bool,
/// Is the current module and all of its parents public?
inside_public_path: bool,
reexported_macros: FxHashSet<DefId>,
}

impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
Expand All @@ -62,6 +63,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
view_item_stack: stack,
inlining: false,
inside_public_path: true,
reexported_macros: FxHashSet(),
}
}

Expand Down Expand Up @@ -201,9 +203,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
if let Some(exports) = self.cx.tcx.export_map.get(&id) {
for export in exports {
if let Def::Macro(def_id, ..) = export.def {
if def_id.krate == LOCAL_CRATE {
if def_id.krate == LOCAL_CRATE || self.reexported_macros.contains(&def_id) {
continue // These are `krate.exported_macros`, handled in `self.visit()`.
}

let imported_from = self.cx.sess().cstore.original_crate_name(def_id.krate);
let def = match self.cx.sess().cstore.load_macro(def_id, self.cx.sess()) {
LoadedMacro::MacroDef(macro_def) => macro_def,
Expand All @@ -217,6 +220,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
} else {
unreachable!()
};

om.macros.push(Macro {
def_id: def_id,
attrs: def.attrs.clone().into(),
Expand Down Expand Up @@ -263,6 +267,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
false
}

debug!("maybe_inline_local def: {:?}", def);

let tcx = self.cx.tcx;
if def == Def::Err {
return false;
Expand All @@ -274,6 +280,17 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
let is_no_inline = use_attrs.lists("doc").has_word("no_inline") ||
use_attrs.lists("doc").has_word("hidden");

// Memoize the non-inlined `pub use`'d macros so we don't push an extra
// declaration in `visit_mod_contents()`
if !def_did.is_local() {
if let Def::Macro(did, _) = def {
if please_inline { return true }
debug!("memoizing non-inlined macro export: {:?}", def);
self.reexported_macros.insert(did);
return false;
}
}

// For cross-crate impl inlining we need to know whether items are
// reachable in documentation - a previously nonreachable item can be
// made reachable by cross-crate inlining which we're checking here.
Expand All @@ -294,6 +311,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
},
_ => {},
}

return false
}

Expand Down
30 changes: 30 additions & 0 deletions src/test/rustdoc/auxiliary/pub-use-extern-macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2017 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.
#![crate_name="macros"]

#[macro_export]
macro_rules! foo {
() => {};
}

#[macro_export]
macro_rules! bar {
() => {};
}

#[macro_export]
macro_rules! baz {
() => {};
}

#[macro_export]
macro_rules! quux {
() => {};
}
31 changes: 31 additions & 0 deletions src/test/rustdoc/pub-use-extern-macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2017 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.

// aux-build:pub-use-extern-macros.rs

#![feature(use_extern_macros, macro_reexport)]

// @has pub_use_extern_macros/macro.foo.html
// @!has pub_use_extern_macros/index.html 'pub use macros::foo;'
#[macro_reexport(foo)] extern crate macros;

// @has pub_use_extern_macros/index.html 'pub use macros::bar;'
// @!has pub_use_extern_macros/macro.bar.html
pub use macros::bar;

// @has pub_use_extern_macros/macro.baz.html
// @!has pub_use_extern_macros/index.html 'pub use macros::baz;'
#[doc(inline)]
pub use macros::baz;

// @!has pub_use_extern_macros/macro.quux.html
// @!has pub_use_extern_macros/index.html 'pub use macros::quux;'
#[doc(hidden)]
pub use macros::quux;