Skip to content

Commit cc6b88c

Browse files
committed
Auto merge of rust-lang#46129 - kennytm:fix-46098-rustdoc-reexport-extern-type, r=GuillaumeGomez
Properly handle reexport of foreign items. Handles `pub use` of `extern { fn, static, type }`. Also plug in some more `match` arms where handling `extern type` is reasonable. Fixed rust-lang#46098.
2 parents 59bf09d + f0fcdbc commit cc6b88c

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/librustdoc/clean/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3118,6 +3118,7 @@ fn register_def(cx: &DocContext, def: Def) -> DefId {
31183118
Def::Struct(i) => (i, TypeKind::Struct),
31193119
Def::Union(i) => (i, TypeKind::Union),
31203120
Def::Mod(i) => (i, TypeKind::Module),
3121+
Def::TyForeign(i) => (i, TypeKind::Foreign),
31213122
Def::Static(i, _) => (i, TypeKind::Static),
31223123
Def::Variant(i) => (cx.tcx.parent_def_id(i).unwrap(), TypeKind::Enum),
31233124
Def::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),

src/librustdoc/visit_ast.rs

+12
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
306306
Def::Struct(did) |
307307
Def::Union(did) |
308308
Def::Enum(did) |
309+
Def::TyForeign(did) |
309310
Def::TyAlias(did) if !self_is_hidden => {
310311
self.cx.access_levels.borrow_mut().map.insert(did, AccessLevel::Public);
311312
},
@@ -348,6 +349,17 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
348349
self.inlining = prev;
349350
true
350351
}
352+
hir_map::NodeForeignItem(it) if !glob => {
353+
// generate a fresh `extern {}` block if we want to inline a foreign item.
354+
om.foreigns.push(hir::ForeignMod {
355+
abi: tcx.hir.get_foreign_abi(it.id),
356+
items: vec![hir::ForeignItem {
357+
name: renamed.unwrap_or(it.name),
358+
.. it.clone()
359+
}].into(),
360+
});
361+
true
362+
}
351363
_ => false,
352364
};
353365
self.view_item_stack.remove(&def_node_id);
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(extern_types)]
12+
13+
mod sub {
14+
extern {
15+
/// Another extern type.
16+
pub type C2;
17+
pub fn f2();
18+
pub static K: usize;
19+
}
20+
}
21+
22+
pub mod sub2 {
23+
extern {
24+
// @has foreigntype_reexport/sub2/foreigntype.C.html
25+
pub type C;
26+
// @has foreigntype_reexport/sub2/fn.f.html
27+
pub fn f();
28+
// @has foreigntype_reexport/sub2/static.K3.html
29+
pub static K3: usize;
30+
}
31+
}
32+
33+
mod sub3 {
34+
extern {
35+
pub type C4;
36+
pub fn f4();
37+
pub static K4: usize;
38+
type X4;
39+
}
40+
}
41+
42+
// @has foreigntype_reexport/foreigntype.C2.html
43+
// @has foreigntype_reexport/fn.f2.html
44+
// @has foreigntype_reexport/static.K2.html
45+
// @has foreigntype_reexport/index.html '//a[@class="foreigntype"]' 'C2'
46+
// @has foreigntype_reexport/index.html '//a[@class="fn"]' 'f2'
47+
// @has foreigntype_reexport/index.html '//a[@class="static"]' 'K2'
48+
pub use self::sub::{C2, f2, K as K2};
49+
50+
// @has foreigntype_reexport/index.html '//a[@class="foreigntype"]' 'C'
51+
// @has foreigntype_reexport/index.html '//a[@class="fn"]' 'f'
52+
// @has foreigntype_reexport/index.html '//a[@class="static"]' 'K3'
53+
// @has foreigntype_reexport/index.html '//code' 'pub use self::sub2::C as C3;'
54+
// @has foreigntype_reexport/index.html '//code' 'pub use self::sub2::f as f3;'
55+
// @has foreigntype_reexport/index.html '//code' 'pub use self::sub2::K3;'
56+
pub use self::sub2::{C as C3, f as f3, K3};
57+
58+
// @has foreigntype_reexport/foreigntype.C4.html
59+
// @has foreigntype_reexport/fn.f4.html
60+
// @has foreigntype_reexport/static.K4.html
61+
// @!has foreigntype_reexport/foreigntype.X4.html
62+
// @has foreigntype_reexport/index.html '//a[@class="foreigntype"]' 'C4'
63+
// @has foreigntype_reexport/index.html '//a[@class="fn"]' 'f4'
64+
// @has foreigntype_reexport/index.html '//a[@class="static"]' 'K4'
65+
// @!has foreigntype_reexport/index.html '//a[@class="foreigntype"]' 'X4'
66+
pub use self::sub3::*;

0 commit comments

Comments
 (0)