Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when reexporting primitive type in rustdoc #67972

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/librustc_hir/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,11 @@ impl<Id> Res<Id> {
Res::Err => true,
}
}

pub fn is_primitive(&self) -> bool {
match self {
Res::PrimTy(..) => true,
_ => false,
}
}
}
32 changes: 23 additions & 9 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,29 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
// If we're re-exporting a re-export it may actually re-export something in
// two namespaces, so the target may be listed twice. Make sure we only
// visit each node at most once.
for &item in cx.tcx.item_children(did).iter() {
let def_id = item.res.def_id();
if item.vis == ty::Visibility::Public {
if did == def_id || !visited.insert(def_id) {
continue;
}
if let Some(i) = try_inline(cx, item.res, item.ident.name, None, visited) {
items.extend(i)
}
for &item in cx.tcx.item_children(did).iter().filter(|item| item.vis == ty::Visibility::Public) {
let def_id = match clean::utils::res_to_def_id(cx, &item.res) {
Some(did) => did,
None => continue,
};
if did == def_id || !visited.insert(def_id) {
continue;
}
Comment on lines +431 to +437
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the def_id is only used to avoid infinite recursion, we only really care about the def_id of modules so using mod_def_id() should be all that's needed to fix the ICE:

if let Some(def_id) = item.res.mod_def_id() {
    if did == def_id || !visited.insert(def_id) {
        continue;
    }
}

if let Res::PrimTy(ref primitive) = item.res {
record_extern_fqn(cx, def_id, TypeKind::Primitive);
cx.renderinfo.borrow_mut().inlined.insert(def_id);
items.push(clean::Item {
source: cx.tcx.def_span(def_id).clean(cx),
name: Some(item.ident.clean(cx)),
attrs: cx.tcx.get_attrs(def_id).clean(cx),
inner: clean::ItemEnum::PrimitiveItem(clean::PrimitiveType::from(primitive)),
visibility: clean::Public,
stability: cx.tcx.lookup_stability(def_id).clean(cx),
deprecation: cx.tcx.lookup_deprecation(def_id).clean(cx),
def_id,
});
Comment on lines +439 to +450
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to synthesize a clean::Import here rather than trying to directly inline primitive types.

} else if let Some(i) = try_inline(cx, item.res, item.ident.name, None, visited) {
items.extend(i);
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,30 @@ pub enum PrimitiveType {
Never,
}

impl From<&hir::PrimTy> for PrimitiveType {
fn from(other: &hir::PrimTy) -> PrimitiveType {
match other {
hir::PrimTy::Uint(syntax::ast::UintTy::U8) => PrimitiveType::U8,
hir::PrimTy::Uint(syntax::ast::UintTy::U16) => PrimitiveType::U16,
hir::PrimTy::Uint(syntax::ast::UintTy::U32) => PrimitiveType::U32,
hir::PrimTy::Uint(syntax::ast::UintTy::U64) => PrimitiveType::U64,
hir::PrimTy::Uint(syntax::ast::UintTy::U128) => PrimitiveType::U128,
hir::PrimTy::Uint(syntax::ast::UintTy::Usize) => PrimitiveType::Usize,
hir::PrimTy::Int(syntax::ast::IntTy::I8) => PrimitiveType::I8,
hir::PrimTy::Int(syntax::ast::IntTy::I16) => PrimitiveType::I16,
hir::PrimTy::Int(syntax::ast::IntTy::I32) => PrimitiveType::I32,
hir::PrimTy::Int(syntax::ast::IntTy::I64) => PrimitiveType::I64,
hir::PrimTy::Int(syntax::ast::IntTy::I128) => PrimitiveType::I128,
hir::PrimTy::Int(syntax::ast::IntTy::Isize) => PrimitiveType::Isize,
hir::PrimTy::Float(syntax::ast::FloatTy::F32) => PrimitiveType::F32,
hir::PrimTy::Float(syntax::ast::FloatTy::F64) => PrimitiveType::F64,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we already ave impls for the IntTy/FloatTy types so maybe we can just call into() on that?

hir::PrimTy::Str => PrimitiveType::Str,
hir::PrimTy::Bool => PrimitiveType::Bool,
hir::PrimTy::Char => PrimitiveType::Char,
}
}
}

#[derive(Clone, Copy, Debug)]
pub enum TypeKind {
Enum,
Expand All @@ -1060,6 +1084,7 @@ pub enum TypeKind {
Attr,
Derive,
TraitAlias,
Primitive,
}

pub trait GetDefId {
Expand Down
57 changes: 57 additions & 0 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,60 @@ where
*cx.impl_trait_bounds.borrow_mut() = old_bounds;
r
}

pub const PRIMITIVES: &[(&str, Res)] = &[
("u8", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U8))),
("u16", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U16))),
("u32", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U32))),
("u64", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U64))),
("u128", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U128))),
("usize", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::Usize))),
("i8", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I8))),
("i16", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I16))),
("i32", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I32))),
("i64", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I64))),
("i128", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I128))),
("isize", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::Isize))),
("f32", Res::PrimTy(hir::PrimTy::Float(syntax::ast::FloatTy::F32))),
("f64", Res::PrimTy(hir::PrimTy::Float(syntax::ast::FloatTy::F64))),
("str", Res::PrimTy(hir::PrimTy::Str)),
("bool", Res::PrimTy(hir::PrimTy::Bool)),
("char", Res::PrimTy(hir::PrimTy::Char)),
];

pub fn res_to_def_id(cx: &DocContext<'_>, res: &Res) -> Option<DefId> {
if res.is_primitive() {
for (path, primitive) in PRIMITIVES.iter() {
if primitive == res {
return primitive_path_impl(cx, path);
}
}
None
} else {
Some(res.def_id())
}
}

pub fn primitive_path_impl(cx: &DocContext<'_>, path_str: &str) -> Option<DefId> {
let tcx = cx.tcx;
match path_str {
"u8" => tcx.lang_items().u8_impl(),
"u16" => tcx.lang_items().u16_impl(),
"u32" => tcx.lang_items().u32_impl(),
"u64" => tcx.lang_items().u64_impl(),
"u128" => tcx.lang_items().u128_impl(),
"usize" => tcx.lang_items().usize_impl(),
"i8" => tcx.lang_items().i8_impl(),
"i16" => tcx.lang_items().i16_impl(),
"i32" => tcx.lang_items().i32_impl(),
"i64" => tcx.lang_items().i64_impl(),
"i128" => tcx.lang_items().i128_impl(),
"isize" => tcx.lang_items().isize_impl(),
"f32" => tcx.lang_items().f32_impl(),
"f64" => tcx.lang_items().f64_impl(),
"str" => tcx.lang_items().str_impl(),
"bool" => tcx.lang_items().bool_impl(),
"char" => tcx.lang_items().char_impl(),
_ => None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this should not be a _ but rather an error (e.g., panic!()).

}
}
1 change: 1 addition & 0 deletions src/librustdoc/html/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl From<clean::TypeKind> for ItemType {
clean::TypeKind::Attr => ItemType::ProcAttribute,
clean::TypeKind::Derive => ItemType::ProcDerive,
clean::TypeKind::TraitAlias => ItemType::TraitAlias,
clean::TypeKind::Primitive => ItemType::Primitive,
}
}
}
Expand Down
52 changes: 6 additions & 46 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use rustc_hir::def::{
Namespace::{self, *},
PerNS, Res,
};
use rustc_hir::def_id::DefId;
use rustc_resolve::ParentScope;
use rustc_span::symbol::Symbol;
use rustc_span::DUMMY_SP;
Expand Down Expand Up @@ -203,7 +202,8 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
.ok_or(ErrorKind::ResolutionFailure)?;

if let Some(prim) = is_primitive(&path, TypeNS) {
let did = primitive_impl(cx, &path).ok_or(ErrorKind::ResolutionFailure)?;
let did =
utils::primitive_path_impl(cx, &path).ok_or(ErrorKind::ResolutionFailure)?;
return cx
.tcx
.associated_items(did)
Expand Down Expand Up @@ -888,50 +888,10 @@ fn handle_variant(
Ok((parent_def, Some(format!("{}.v", variant.ident.name))))
}

const PRIMITIVES: &[(&str, Res)] = &[
("u8", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U8))),
("u16", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U16))),
("u32", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U32))),
("u64", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U64))),
("u128", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U128))),
("usize", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::Usize))),
("i8", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I8))),
("i16", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I16))),
("i32", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I32))),
("i64", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I64))),
("i128", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I128))),
("isize", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::Isize))),
("f32", Res::PrimTy(hir::PrimTy::Float(syntax::ast::FloatTy::F32))),
("f64", Res::PrimTy(hir::PrimTy::Float(syntax::ast::FloatTy::F64))),
("str", Res::PrimTy(hir::PrimTy::Str)),
("bool", Res::PrimTy(hir::PrimTy::Bool)),
("char", Res::PrimTy(hir::PrimTy::Char)),
];

fn is_primitive(path_str: &str, ns: Namespace) -> Option<Res> {
if ns == TypeNS { PRIMITIVES.iter().find(|x| x.0 == path_str).map(|x| x.1) } else { None }
}

fn primitive_impl(cx: &DocContext<'_>, path_str: &str) -> Option<DefId> {
let tcx = cx.tcx;
match path_str {
"u8" => tcx.lang_items().u8_impl(),
"u16" => tcx.lang_items().u16_impl(),
"u32" => tcx.lang_items().u32_impl(),
"u64" => tcx.lang_items().u64_impl(),
"u128" => tcx.lang_items().u128_impl(),
"usize" => tcx.lang_items().usize_impl(),
"i8" => tcx.lang_items().i8_impl(),
"i16" => tcx.lang_items().i16_impl(),
"i32" => tcx.lang_items().i32_impl(),
"i64" => tcx.lang_items().i64_impl(),
"i128" => tcx.lang_items().i128_impl(),
"isize" => tcx.lang_items().isize_impl(),
"f32" => tcx.lang_items().f32_impl(),
"f64" => tcx.lang_items().f64_impl(),
"str" => tcx.lang_items().str_impl(),
"bool" => tcx.lang_items().bool_impl(),
"char" => tcx.lang_items().char_impl(),
_ => None,
if ns == TypeNS {
utils::PRIMITIVES.iter().find(|x| x.0 == path_str).map(|x| x.1)
} else {
None
}
}
7 changes: 7 additions & 0 deletions src/test/rustdoc/auxiliary/reexport-primitive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// compile-flags: --emit metadata --crate-type lib --edition 2018

#![crate_name = "foo"]

pub mod bar {
pub use bool;
}
10 changes: 10 additions & 0 deletions src/test/rustdoc/reexport-primitive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// aux-build: reexport-primitive.rs
// compile-flags:--extern foo --edition 2018

#![crate_name = "bar"]

// @has bar/p/index.html
// @has - 'bool'
pub mod p {
pub use foo::bar::*;
}