-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be better to synthesize a |
||
} else if let Some(i) = try_inline(cx, item.res, item.ident.name, None, visited) { | ||
items.extend(i); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
hir::PrimTy::Str => PrimitiveType::Str, | ||
hir::PrimTy::Bool => PrimitiveType::Bool, | ||
hir::PrimTy::Char => PrimitiveType::Char, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub enum TypeKind { | ||
Enum, | ||
|
@@ -1060,6 +1084,7 @@ pub enum TypeKind { | |
Attr, | ||
Derive, | ||
TraitAlias, | ||
Primitive, | ||
} | ||
|
||
pub trait GetDefId { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this should not be a |
||
} | ||
} |
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; | ||
} |
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::*; | ||
} |
There was a problem hiding this comment.
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 thedef_id
of modules so usingmod_def_id()
should be all that's needed to fix the ICE: