Skip to content

Commit

Permalink
Don't ignore impls for primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
hkmatsumoto committed Sep 29, 2021
1 parent f6e6ddc commit e18a8ef
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,15 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
ConstantItem(c) => ItemEnum::Constant(c.into_tcx(tcx)),
MacroItem(m) => ItemEnum::Macro(m.source),
ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
PrimitiveItem(p) => ItemEnum::PrimitiveType(p.as_sym().to_string()),
AssocConstItem(t, s) => ItemEnum::AssocConst { type_: t.into_tcx(tcx), default: s },
AssocTypeItem(g, t) => ItemEnum::AssocType {
bounds: g.into_iter().map(|x| x.into_tcx(tcx)).collect(),
default: t.map(|x| x.into_tcx(tcx)),
},
// `convert_item` early returns `None` for striped items
StrippedItem(_) => unreachable!(),
PrimitiveItem(_) | KeywordItem(_) => {
KeywordItem(_) => {
panic!("{:?} is not supported for JSON output", item)
}
ExternCrateItem { ref src } => ItemEnum::ExternCrate {
Expand Down
23 changes: 21 additions & 2 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,21 @@ impl JsonRenderer<'tcx> {
.iter()
.filter_map(|i| {
let item = &i.impl_item;
if item.def_id.is_local() {

// HACK(hkmatsumoto): For impls of primitive types, we index them
// regardless of whether they're local. This is because users can
// document primitive items in an arbitrary crate by using
// `doc(primitive)`.
let mut is_primitive_impl = false;
if let clean::types::ItemKind::ImplItem(ref impl_) = *item.kind {
if impl_.trait_.is_none() {
if let clean::types::Type::Primitive(_) = impl_.for_ {
is_primitive_impl = true;
}
}
}

if item.def_id.is_local() || is_primitive_impl {
self.item(item.clone()).unwrap();
Some(from_item_id(item.def_id))
} else {
Expand Down Expand Up @@ -189,6 +203,11 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {

fn after_krate(&mut self) -> Result<(), Error> {
debug!("Done with crate");

for primitive in Rc::clone(&self.cache).primitive_locations.values() {
self.get_impls(primitive.clone());
}

let mut index = (*self.index).clone().into_inner();
index.extend(self.get_trait_items());
// This needs to be the default HashMap for compatibility with the public interface for
Expand Down Expand Up @@ -234,7 +253,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
)
})
.collect(),
format_version: 7,
format_version: 8,
};
let mut p = self.out_path.clone();
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
Expand Down
2 changes: 2 additions & 0 deletions src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ pub enum ItemEnum {
Macro(String),
ProcMacro(ProcMacro),

PrimitiveType(String),

AssocConst {
#[serde(rename = "type")]
type_: Type,
Expand Down
14 changes: 14 additions & 0 deletions src/test/rustdoc-json/primitive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// edition:2018

#![feature(doc_primitive)]

#[doc(primitive = "usize")]
mod usize {}

// @set local_crate_id = primitive.json "$.index[*][?(@.name=='primitive')].crate_id"

// @has - "$.index[*][?(@.name=='log10')]"
// @!is - "$.index[*][?(@.name=='log10')].crate_id" $local_crate_id
// @has - "$.index[*][?(@.name=='checked_add')]"
// @!is - "$.index[*][?(@.name=='checked_add')]" $local_crate_id
// @!has - "$.index[*][?(@.name=='is_ascii_uppercase')]"

0 comments on commit e18a8ef

Please sign in to comment.