Skip to content

Commit 11549df

Browse files
authored
Rollup merge of #57508 - DebugSteven:inline-extern, r=GuillaumeGomez
rustdoc: Allow inlining of reexported crates and crate items Fixes #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.
2 parents 461244a + ca47808 commit 11549df

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/librustdoc/clean/mod.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ impl Clean<Item> for doctree::Module {
587587
let attrs = self.attrs.clean(cx);
588588

589589
let mut items: Vec<Item> = vec![];
590-
items.extend(self.extern_crates.iter().map(|x| x.clean(cx)));
590+
items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
591591
items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
592592
items.extend(self.structs.iter().map(|x| x.clean(cx)));
593593
items.extend(self.unions.iter().map(|x| x.clean(cx)));
@@ -3503,9 +3503,30 @@ fn build_deref_target_impls(cx: &DocContext,
35033503
}
35043504
}
35053505

3506-
impl Clean<Item> for doctree::ExternCrate {
3507-
fn clean(&self, cx: &DocContext) -> Item {
3508-
Item {
3506+
impl Clean<Vec<Item>> for doctree::ExternCrate {
3507+
fn clean(&self, cx: &DocContext) -> Vec<Item> {
3508+
3509+
let please_inline = self.vis.node.is_pub() && self.attrs.iter().any(|a| {
3510+
a.name() == "doc" && match a.meta_item_list() {
3511+
Some(l) => attr::list_contains_name(&l, "inline"),
3512+
None => false,
3513+
}
3514+
});
3515+
3516+
if please_inline {
3517+
let mut visited = FxHashSet::default();
3518+
3519+
let def = Def::Mod(DefId {
3520+
krate: self.cnum,
3521+
index: CRATE_DEF_INDEX,
3522+
});
3523+
3524+
if let Some(items) = inline::try_inline(cx, def, self.name, &mut visited) {
3525+
return items;
3526+
}
3527+
}
3528+
3529+
vec![Item {
35093530
name: None,
35103531
attrs: self.attrs.clean(cx),
35113532
source: self.whence.clean(cx),
@@ -3514,7 +3535,7 @@ impl Clean<Item> for doctree::ExternCrate {
35143535
stability: None,
35153536
deprecation: None,
35163537
inner: ExternCrateItem(self.name.clean(cx), self.path.clone())
3517-
}
3538+
}]
35183539
}
35193540
}
35203541

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#![crate_name = "inner"]
2+
pub struct SomeStruct;

src/test/rustdoc/pub-extern-crate.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// aux-build:pub-extern-crate.rs
2+
3+
// @has pub_extern_crate/index.html
4+
// @!has - '//code' 'pub extern crate inner'
5+
// @has - '//a/@href' 'inner/index.html'
6+
// @has pub_extern_crate/inner/index.html
7+
// @has pub_extern_crate/inner/struct.SomeStruct.html
8+
#[doc(inline)]
9+
pub extern crate inner;

0 commit comments

Comments
 (0)