Skip to content

Commit 7a09755

Browse files
authored
Rollup merge of #88234 - hkmatsumoto:rustdoc-impls-for-primitive, r=jyn514
rustdoc-json: Don't ignore impls for primitive types Fix the issue discussed at [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/.E2.9C.94.20Json.20output.20lacks.20some.20item.20which.20are.20supposed.20to.20be.20there) r? ``@jyn514``
2 parents 0fb0122 + e18a8ef commit 7a09755

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

src/librustdoc/json/conversions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,15 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
218218
ConstantItem(c) => ItemEnum::Constant(c.into_tcx(tcx)),
219219
MacroItem(m) => ItemEnum::Macro(m.source),
220220
ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
221+
PrimitiveItem(p) => ItemEnum::PrimitiveType(p.as_sym().to_string()),
221222
AssocConstItem(t, s) => ItemEnum::AssocConst { type_: t.into_tcx(tcx), default: s },
222223
AssocTypeItem(g, t) => ItemEnum::AssocType {
223224
bounds: g.into_iter().map(|x| x.into_tcx(tcx)).collect(),
224225
default: t.map(|x| x.into_tcx(tcx)),
225226
},
226227
// `convert_item` early returns `None` for striped items
227228
StrippedItem(_) => unreachable!(),
228-
PrimitiveItem(_) | KeywordItem(_) => {
229+
KeywordItem(_) => {
229230
panic!("{:?} is not supported for JSON output", item)
230231
}
231232
ExternCrateItem { ref src } => ItemEnum::ExternCrate {

src/librustdoc/json/mod.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,21 @@ impl JsonRenderer<'tcx> {
6969
.iter()
7070
.filter_map(|i| {
7171
let item = &i.impl_item;
72-
if item.def_id.is_local() {
72+
73+
// HACK(hkmatsumoto): For impls of primitive types, we index them
74+
// regardless of whether they're local. This is because users can
75+
// document primitive items in an arbitrary crate by using
76+
// `doc(primitive)`.
77+
let mut is_primitive_impl = false;
78+
if let clean::types::ItemKind::ImplItem(ref impl_) = *item.kind {
79+
if impl_.trait_.is_none() {
80+
if let clean::types::Type::Primitive(_) = impl_.for_ {
81+
is_primitive_impl = true;
82+
}
83+
}
84+
}
85+
86+
if item.def_id.is_local() || is_primitive_impl {
7387
self.item(item.clone()).unwrap();
7488
Some(from_item_id(item.def_id))
7589
} else {
@@ -191,6 +205,11 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
191205

192206
fn after_krate(&mut self) -> Result<(), Error> {
193207
debug!("Done with crate");
208+
209+
for primitive in Rc::clone(&self.cache).primitive_locations.values() {
210+
self.get_impls(primitive.clone());
211+
}
212+
194213
let mut index = (*self.index).clone().into_inner();
195214
index.extend(self.get_trait_items());
196215
// This needs to be the default HashMap for compatibility with the public interface for
@@ -236,7 +255,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
236255
)
237256
})
238257
.collect(),
239-
format_version: 7,
258+
format_version: 8,
240259
};
241260
let mut p = self.out_path.clone();
242261
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());

src/rustdoc-json-types/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ pub enum ItemEnum {
221221
Macro(String),
222222
ProcMacro(ProcMacro),
223223

224+
PrimitiveType(String),
225+
224226
AssocConst {
225227
#[serde(rename = "type")]
226228
type_: Type,

src/test/rustdoc-json/primitive.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// edition:2018
2+
3+
#![feature(doc_primitive)]
4+
5+
#[doc(primitive = "usize")]
6+
mod usize {}
7+
8+
// @set local_crate_id = primitive.json "$.index[*][?(@.name=='primitive')].crate_id"
9+
10+
// @has - "$.index[*][?(@.name=='log10')]"
11+
// @!is - "$.index[*][?(@.name=='log10')].crate_id" $local_crate_id
12+
// @has - "$.index[*][?(@.name=='checked_add')]"
13+
// @!is - "$.index[*][?(@.name=='checked_add')]" $local_crate_id
14+
// @!has - "$.index[*][?(@.name=='is_ascii_uppercase')]"

0 commit comments

Comments
 (0)