Skip to content

Commit

Permalink
Rollup merge of rust-lang#57508 - DebugSteven:inline-extern, r=Guilla…
Browse files Browse the repository at this point in the history
…umeGomez

rustdoc: Allow inlining of reexported crates and crate items

Fixes rust-lang#46296

This PR checks for when a `pub extern crate` statement has a `#[doc(inline)]` attribute & inlines its contents. Code is based off of the inlining statements for `pub use` statements.
  • Loading branch information
Centril authored Jan 13, 2019
2 parents ce448f3 + ca47808 commit 8f11da4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ impl Clean<Item> for doctree::Module {
let attrs = self.attrs.clean(cx);

let mut items: Vec<Item> = vec![];
items.extend(self.extern_crates.iter().map(|x| x.clean(cx)));
items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
items.extend(self.structs.iter().map(|x| x.clean(cx)));
items.extend(self.unions.iter().map(|x| x.clean(cx)));
Expand Down Expand Up @@ -3503,9 +3503,30 @@ fn build_deref_target_impls(cx: &DocContext,
}
}

impl Clean<Item> for doctree::ExternCrate {
fn clean(&self, cx: &DocContext) -> Item {
Item {
impl Clean<Vec<Item>> for doctree::ExternCrate {
fn clean(&self, cx: &DocContext) -> Vec<Item> {

let please_inline = self.vis.node.is_pub() && self.attrs.iter().any(|a| {
a.name() == "doc" && match a.meta_item_list() {
Some(l) => attr::list_contains_name(&l, "inline"),
None => false,
}
});

if please_inline {
let mut visited = FxHashSet::default();

let def = Def::Mod(DefId {
krate: self.cnum,
index: CRATE_DEF_INDEX,
});

if let Some(items) = inline::try_inline(cx, def, self.name, &mut visited) {
return items;
}
}

vec![Item {
name: None,
attrs: self.attrs.clean(cx),
source: self.whence.clean(cx),
Expand All @@ -3514,7 +3535,7 @@ impl Clean<Item> for doctree::ExternCrate {
stability: None,
deprecation: None,
inner: ExternCrateItem(self.name.clean(cx), self.path.clone())
}
}]
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/rustdoc/auxiliary/pub-extern-crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#![crate_name = "inner"]
pub struct SomeStruct;
9 changes: 9 additions & 0 deletions src/test/rustdoc/pub-extern-crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// aux-build:pub-extern-crate.rs

// @has pub_extern_crate/index.html
// @!has - '//code' 'pub extern crate inner'
// @has - '//a/@href' 'inner/index.html'
// @has pub_extern_crate/inner/index.html
// @has pub_extern_crate/inner/struct.SomeStruct.html
#[doc(inline)]
pub extern crate inner;

0 comments on commit 8f11da4

Please sign in to comment.