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

rustdoc-json: Don't ignore impls for primitive types #88234

Merged
merged 1 commit into from
Oct 5, 2021
Merged
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
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
hkmatsumoto marked this conversation as resolved.
Show resolved Hide resolved
// `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() {
jyn514 marked this conversation as resolved.
Show resolved Hide resolved
self.get_impls(primitive.clone());
}
hkmatsumoto marked this conversation as resolved.
Show resolved Hide resolved

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 {}
Copy link
Member

Choose a reason for hiding this comment

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

Can you also test the behavior when the primitive is only defined in libstd, not the current crate?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added tests to check crate_id of methods of usize does not match to local crate's. We cannot check if they come from libstd because the std module item is not in the index, but hope it suffices.

Copy link
Member

Choose a reason for hiding this comment

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

Added tests to check crate_id of methods of usize does not match to local crate's.

In that case, this mod usize is doing nothing and should be removed.

Copy link
Member Author

@hkmatsumoto hkmatsumoto Sep 29, 2021

Choose a reason for hiding this comment

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

doc(primitive) requires an item to be defined right after itself, so we need this meaningless mod usize like we do in library/std/src/primitive_docs.rs.

Copy link
Member

@jyn514 jyn514 Sep 29, 2021

Choose a reason for hiding this comment

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

No, my point is that doc(primitive) is doing nothing. If it's using the methods in libstd then whether the docs come from this crate or libstd doesn't matter.

Copy link
Member

@jyn514 jyn514 Sep 29, 2021

Choose a reason for hiding this comment

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

Actually, I'm not sure this is the right approach at all - the original problem was that documenting libcore wouldn't show the primitive impls, right? but we don't want to show them on all crates unconditionally, only crates where the primitive is documented.


// @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')]"