Skip to content

Commit edfa0f4

Browse files
authored
Rollup merge of #68556 - ollie27:rustdoc_primitive_re-export, r=GuillaumeGomez
rustdoc: Fix re-exporting primitive types * Generate links to the primitive type docs for re-exports. * Don't ICE on cross crate primitive type re-exports. * Make primitive type re-exports show up cross crate. Fixes #67646 Closes #67972 r? @GuillaumeGomez
2 parents 85f3240 + bbc2ae7 commit edfa0f4

File tree

6 files changed

+91
-17
lines changed

6 files changed

+91
-17
lines changed

src/librustdoc/clean/inline.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,41 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
445445
// two namespaces, so the target may be listed twice. Make sure we only
446446
// visit each node at most once.
447447
for &item in cx.tcx.item_children(did).iter() {
448-
let def_id = item.res.def_id();
449448
if item.vis == ty::Visibility::Public {
450-
if did == def_id || !visited.insert(def_id) {
451-
continue;
449+
if let Some(def_id) = item.res.mod_def_id() {
450+
if did == def_id || !visited.insert(def_id) {
451+
continue;
452+
}
452453
}
453-
if let Some(i) = try_inline(cx, item.res, item.ident.name, None, visited) {
454+
if let Res::PrimTy(p) = item.res {
455+
// Primitive types can't be inlined so generate an import instead.
456+
items.push(clean::Item {
457+
name: None,
458+
attrs: clean::Attributes::default(),
459+
source: clean::Span::empty(),
460+
def_id: cx.tcx.hir().local_def_id_from_node_id(ast::CRATE_NODE_ID),
461+
visibility: clean::Public,
462+
stability: None,
463+
deprecation: None,
464+
inner: clean::ImportItem(clean::Import::Simple(
465+
item.ident.to_string(),
466+
clean::ImportSource {
467+
path: clean::Path {
468+
global: false,
469+
res: item.res,
470+
segments: vec![clean::PathSegment {
471+
name: clean::PrimitiveType::from(p).as_str().to_string(),
472+
args: clean::GenericArgs::AngleBracketed {
473+
args: Vec::new(),
474+
bindings: Vec::new(),
475+
},
476+
}],
477+
},
478+
did: None,
479+
},
480+
)),
481+
});
482+
} else if let Some(i) = try_inline(cx, item.res, item.ident.name, None, visited) {
454483
items.extend(i)
455484
}
456485
}

src/librustdoc/clean/types.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,19 @@ impl From<ast::FloatTy> for PrimitiveType {
12901290
}
12911291
}
12921292

1293+
impl From<hir::PrimTy> for PrimitiveType {
1294+
fn from(prim_ty: hir::PrimTy) -> PrimitiveType {
1295+
match prim_ty {
1296+
hir::PrimTy::Int(int_ty) => int_ty.into(),
1297+
hir::PrimTy::Uint(uint_ty) => uint_ty.into(),
1298+
hir::PrimTy::Float(float_ty) => float_ty.into(),
1299+
hir::PrimTy::Str => PrimitiveType::Str,
1300+
hir::PrimTy::Bool => PrimitiveType::Bool,
1301+
hir::PrimTy::Char => PrimitiveType::Char,
1302+
}
1303+
}
1304+
}
1305+
12931306
#[derive(Clone, PartialEq, Eq, Debug)]
12941307
pub enum Visibility {
12951308
Public,

src/librustdoc/clean/utils.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -570,14 +570,7 @@ pub fn resolve_type(cx: &DocContext<'_>, path: Path, id: hir::HirId) -> Type {
570570
}
571571

572572
let is_generic = match path.res {
573-
Res::PrimTy(p) => match p {
574-
hir::PrimTy::Str => return Primitive(PrimitiveType::Str),
575-
hir::PrimTy::Bool => return Primitive(PrimitiveType::Bool),
576-
hir::PrimTy::Char => return Primitive(PrimitiveType::Char),
577-
hir::PrimTy::Int(int_ty) => return Primitive(int_ty.into()),
578-
hir::PrimTy::Uint(uint_ty) => return Primitive(uint_ty.into()),
579-
hir::PrimTy::Float(float_ty) => return Primitive(float_ty.into()),
580-
},
573+
Res::PrimTy(p) => return Primitive(PrimitiveType::from(p)),
581574
Res::SelfTy(..) if path.segments.len() == 1 => {
582575
return Generic(kw::SelfUpper.to_string());
583576
}

src/librustdoc/html/format.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1171,11 +1171,14 @@ impl clean::ImportSource {
11711171
display_fn(move |f| match self.did {
11721172
Some(did) => resolved_path(f, did, &self.path, true, false),
11731173
_ => {
1174-
for (i, seg) in self.path.segments.iter().enumerate() {
1175-
if i > 0 {
1176-
write!(f, "::")?
1177-
}
1178-
write!(f, "{}", seg.name)?;
1174+
for seg in &self.path.segments[..self.path.segments.len() - 1] {
1175+
write!(f, "{}::", seg.name)?;
1176+
}
1177+
let name = self.path.last_name();
1178+
if let hir::def::Res::PrimTy(p) = self.path.res {
1179+
primitive_link(f, PrimitiveType::from(p), name)?;
1180+
} else {
1181+
write!(f, "{}", name)?;
11791182
}
11801183
Ok(())
11811184
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// compile-flags: --emit metadata --crate-type lib --edition 2018
2+
3+
#![crate_name = "foo"]
4+
5+
pub mod bar {
6+
pub use bool;
7+
pub use char as my_char;
8+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// aux-build: primitive-reexport.rs
2+
// compile-flags:--extern foo --edition 2018
3+
4+
#![crate_name = "bar"]
5+
6+
// @has bar/p/index.html
7+
// @has - '//code' 'pub use bool;'
8+
// @has - '//code/a[@href="https://doc.rust-lang.org/nightly/std/primitive.bool.html"]' 'bool'
9+
// @has - '//code' 'pub use char as my_char;'
10+
// @has - '//code/a[@href="https://doc.rust-lang.org/nightly/std/primitive.char.html"]' 'char'
11+
pub mod p {
12+
pub use foo::bar::*;
13+
}
14+
15+
// @has bar/baz/index.html
16+
// @has - '//code' 'pub use bool;'
17+
// @has - '//code/a[@href="https://doc.rust-lang.org/nightly/std/primitive.bool.html"]' 'bool'
18+
// @has - '//code' 'pub use char as my_char;'
19+
// @has - '//code/a[@href="https://doc.rust-lang.org/nightly/std/primitive.char.html"]' 'char'
20+
pub use foo::bar as baz;
21+
22+
// @has bar/index.html
23+
// @has - '//code' 'pub use str;'
24+
// @has - '//code/a[@href="https://doc.rust-lang.org/nightly/std/primitive.str.html"]' 'str'
25+
// @has - '//code' 'pub use i32 as my_i32;'
26+
// @has - '//code/a[@href="https://doc.rust-lang.org/nightly/std/primitive.i32.html"]' 'i32'
27+
pub use str;
28+
pub use i32 as my_i32;

0 commit comments

Comments
 (0)