Skip to content

Commit 678ec9b

Browse files
Rollup merge of #141831 - lolbinarycat:rustdoc-extern-reexport-135092, r=GuillaumeGomez
rustdoc: fix attrs of locally reexported foreign items fixes #135092 also tweaks a few outdated/misleading comments. r? `@GuillaumeGomez`
2 parents a413f77 + 3ed1c87 commit 678ec9b

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ use crate::visit_ast::Module as DocModule;
6666
pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
6767
let mut items: Vec<Item> = vec![];
6868
let mut inserted = FxHashSet::default();
69-
items.extend(doc.foreigns.iter().map(|(item, renamed)| {
70-
let item = clean_maybe_renamed_foreign_item(cx, item, *renamed);
69+
items.extend(doc.foreigns.iter().map(|(item, renamed, import_id)| {
70+
let item = clean_maybe_renamed_foreign_item(cx, item, *renamed, *import_id);
7171
if let Some(name) = item.name
7272
&& (cx.render_options.document_hidden || !item.is_doc_hidden())
7373
{
@@ -89,7 +89,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
8989
Some(item)
9090
}));
9191

92-
// Split up imports from all other items.
92+
// Split up glob imports from all other items.
9393
//
9494
// This covers the case where somebody does an import which should pull in an item,
9595
// but there's already an item with the same namespace and same name. Rust gives
@@ -2762,7 +2762,6 @@ fn clean_maybe_renamed_item<'tcx>(
27622762
import_id: Option<LocalDefId>,
27632763
) -> Vec<Item> {
27642764
use hir::ItemKind;
2765-
27662765
fn get_name(
27672766
cx: &DocContext<'_>,
27682767
item: &hir::Item<'_>,
@@ -2974,6 +2973,7 @@ fn clean_extern_crate<'tcx>(
29742973
&& !cx.is_json_output();
29752974

29762975
let krate_owner_def_id = krate.owner_id.def_id;
2976+
29772977
if please_inline
29782978
&& let Some(items) = inline::try_inline(
29792979
cx,
@@ -3135,6 +3135,7 @@ fn clean_maybe_renamed_foreign_item<'tcx>(
31353135
cx: &mut DocContext<'tcx>,
31363136
item: &hir::ForeignItem<'tcx>,
31373137
renamed: Option<Symbol>,
3138+
import_id: Option<LocalDefId>,
31383139
) -> Item {
31393140
let def_id = item.owner_id.to_def_id();
31403141
cx.with_param_env(def_id, |cx| {
@@ -3150,11 +3151,13 @@ fn clean_maybe_renamed_foreign_item<'tcx>(
31503151
hir::ForeignItemKind::Type => ForeignTypeItem,
31513152
};
31523153

3153-
Item::from_def_id_and_parts(
3154-
item.owner_id.def_id.to_def_id(),
3155-
Some(renamed.unwrap_or(item.ident.name)),
3156-
kind,
3154+
generate_item_with_correct_attrs(
31573155
cx,
3156+
kind,
3157+
item.owner_id.def_id.to_def_id(),
3158+
item.ident.name,
3159+
import_id,
3160+
renamed,
31583161
)
31593162
})
31603163
}

src/librustdoc/visit_ast.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,18 @@ pub(crate) struct Module<'hir> {
3838
(LocalDefId, Option<Symbol>),
3939
(&'hir hir::Item<'hir>, Option<Symbol>, Option<LocalDefId>),
4040
>,
41-
/// Same as for `items`.
41+
42+
/// (def_id, renamed) -> (res, local_import_id)
43+
///
44+
/// `inlined_foreigns` only contains `extern` items
45+
/// that are cross-crate inlined.
46+
///
47+
/// Locally inlined `extern` items are
48+
/// stored in `foreigns` with the `import_id` set,
49+
/// analogous to how `items` is.
4250
pub(crate) inlined_foreigns: FxIndexMap<(DefId, Option<Symbol>), (Res, LocalDefId)>,
43-
pub(crate) foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
51+
/// (item, renamed, import_id)
52+
pub(crate) foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>, Option<LocalDefId>)>,
4453
}
4554

4655
impl Module<'_> {
@@ -327,7 +336,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
327336
}
328337
Node::ForeignItem(it) if !glob => {
329338
let prev = mem::replace(&mut self.inlining, true);
330-
self.visit_foreign_item_inner(it, renamed);
339+
self.visit_foreign_item_inner(it, renamed, Some(def_id));
331340
self.inlining = prev;
332341
true
333342
}
@@ -432,7 +441,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
432441
hir::ItemKind::ForeignMod { items, .. } => {
433442
for item in items {
434443
let item = tcx.hir_foreign_item(item.id);
435-
self.visit_foreign_item_inner(item, None);
444+
self.visit_foreign_item_inner(item, None, None);
436445
}
437446
}
438447
// If we're inlining, skip private items.
@@ -527,10 +536,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
527536
&mut self,
528537
item: &'tcx hir::ForeignItem<'_>,
529538
renamed: Option<Symbol>,
539+
import_id: Option<LocalDefId>,
530540
) {
531541
// If inlining we only want to include public functions.
532542
if !self.inlining || self.cx.tcx.visibility(item.owner_id).is_public() {
533-
self.modules.last_mut().unwrap().foreigns.push((item, renamed));
543+
self.modules.last_mut().unwrap().foreigns.push((item, renamed, import_id));
534544
}
535545
}
536546

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Test to make sure reexports of extern items are combined
2+
// <https://github.com/rust-lang/rust/issues/135092>
3+
4+
#![crate_name = "foo"]
5+
6+
mod native {
7+
extern "C" {
8+
/// bar.
9+
pub fn bar();
10+
}
11+
12+
/// baz.
13+
pub fn baz() {}
14+
}
15+
16+
//@ has 'foo/fn.bar.html'
17+
//@ has - '//div[@class="docblock"]' 'bar.'
18+
//@ has - '//div[@class="docblock"]' 'foo'
19+
/// foo
20+
pub use native::bar;
21+
22+
//@ has 'foo/fn.baz.html'
23+
//@ has - '//div[@class="docblock"]' 'baz.'
24+
//@ has - '//div[@class="docblock"]' 'foo'
25+
/// foo
26+
pub use native::baz;

0 commit comments

Comments
 (0)