Skip to content

Commit 3adc081

Browse files
Fix bad handling of primitive types
1 parent 7d47d7c commit 3adc081

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

src/librustdoc/json/conversions.rs

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl JsonRenderer<'_> {
3838
Some(UrlFragment::UserWritten(_)) | None => *page_id,
3939
};
4040

41-
(link.clone(), id_from_item_inner(id.into(), self.tcx, None))
41+
(link.clone(), id_from_item_inner(id.into(), self.tcx, None, None))
4242
})
4343
.collect();
4444
let docs = item.attrs.collapsed_doc_value();
@@ -108,7 +108,7 @@ impl JsonRenderer<'_> {
108108
Some(ty::Visibility::Public) => Visibility::Public,
109109
Some(ty::Visibility::Restricted(did)) if did.is_crate_root() => Visibility::Crate,
110110
Some(ty::Visibility::Restricted(did)) => Visibility::Restricted {
111-
parent: id_from_item_inner(did.into(), self.tcx, None),
111+
parent: id_from_item_inner(did.into(), self.tcx, None, None),
112112
path: self.tcx.def_path(did).to_string_no_crate_verbose(),
113113
},
114114
}
@@ -208,12 +208,17 @@ impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind {
208208
/// It generates an ID as follows:
209209
///
210210
/// `CRATE_ID:ITEM_ID[:NAME_ID]` (if there is no name, NAME_ID is not generated).
211-
pub(crate) fn id_from_item_inner(item_id: ItemId, tcx: TyCtxt<'_>, extra: Option<&Id>) -> Id {
212-
struct DisplayDefId<'a, 'b>(DefId, TyCtxt<'a>, Option<&'b Id>);
211+
pub(crate) fn id_from_item_inner(
212+
item_id: ItemId,
213+
tcx: TyCtxt<'_>,
214+
extra: Option<&Id>,
215+
name: Option<Symbol>,
216+
) -> Id {
217+
struct DisplayDefId<'a, 'b>(DefId, TyCtxt<'a>, Option<&'b Id>, Option<Symbol>);
213218

214219
impl<'a, 'b> fmt::Display for DisplayDefId<'a, 'b> {
215220
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
216-
let DisplayDefId(def_id, tcx, extra) = self;
221+
let DisplayDefId(def_id, tcx, extra, name) = self;
217222
// We need this workaround because primitive types' DefId actually refers to
218223
// their parent module, which isn't present in the output JSON items. So
219224
// instead, we directly get the primitive symbol and convert it to u32 to
@@ -225,41 +230,54 @@ pub(crate) fn id_from_item_inner(item_id: ItemId, tcx: TyCtxt<'_>, extra: Option
225230
} else {
226231
""
227232
};
228-
let name = if matches!(tcx.def_kind(def_id), DefKind::Mod) &&
229-
let Some(prim) = tcx.get_attrs(*def_id, sym::doc)
230-
.flat_map(|attr| attr.meta_item_list().unwrap_or_default())
231-
.filter(|attr| attr.has_name(sym::primitive))
232-
.find_map(|attr| attr.value_str()) {
233-
format!(":{}", prim.as_u32())
234-
} else {
235-
tcx
236-
.opt_item_name(*def_id)
237-
.map(|n| format!(":{}", n.as_u32()))
238-
.unwrap_or_default()
233+
let name = match name {
234+
Some(name) => format!(":{}", name.as_u32()),
235+
None => {
236+
// We need this workaround because primitive types' DefId actually refers to
237+
// their parent module, which isn't present in the output JSON items. So
238+
// instead, we directly get the primitive symbol and convert it to u32 to
239+
// generate the ID.
240+
if matches!(tcx.def_kind(def_id), DefKind::Mod) &&
241+
let Some(prim) = tcx.get_attrs(*def_id, sym::doc)
242+
.flat_map(|attr| attr.meta_item_list().unwrap_or_default())
243+
.filter(|attr| attr.has_name(sym::primitive))
244+
.find_map(|attr| attr.value_str()) {
245+
format!(":{}", prim.as_u32())
246+
} else {
247+
tcx
248+
.opt_item_name(*def_id)
249+
.map(|n| format!(":{}", n.as_u32()))
250+
.unwrap_or_default()
251+
}
252+
}
239253
};
240-
write!(f, "{}:{}{name}{extra}", self.0.krate.as_u32(), u32::from(self.0.index))
254+
write!(f, "{}:{}{name}{extra}", def_id.krate.as_u32(), u32::from(def_id.index))
241255
}
242256
}
243257

244258
match item_id {
245-
ItemId::DefId(did) => Id(format!("{}", DisplayDefId(did, tcx, extra))),
246-
ItemId::Blanket { for_, impl_id } => {
247-
Id(format!("b:{}-{}", DisplayDefId(impl_id, tcx, None), DisplayDefId(for_, tcx, extra)))
248-
}
249-
ItemId::Auto { for_, trait_ } => {
250-
Id(format!("a:{}-{}", DisplayDefId(trait_, tcx, None), DisplayDefId(for_, tcx, extra)))
251-
}
259+
ItemId::DefId(did) => Id(format!("{}", DisplayDefId(did, tcx, extra, name))),
260+
ItemId::Blanket { for_, impl_id } => Id(format!(
261+
"b:{}-{}",
262+
DisplayDefId(impl_id, tcx, None, None),
263+
DisplayDefId(for_, tcx, extra, name)
264+
)),
265+
ItemId::Auto { for_, trait_ } => Id(format!(
266+
"a:{}-{}",
267+
DisplayDefId(trait_, tcx, None, None),
268+
DisplayDefId(for_, tcx, extra, name)
269+
)),
252270
}
253271
}
254272

255273
pub(crate) fn id_from_item(item: &clean::Item, tcx: TyCtxt<'_>) -> Id {
256274
match *item.kind {
257275
clean::ItemKind::ImportItem(ref import) => {
258276
let extra =
259-
import.source.did.map(ItemId::from).map(|i| id_from_item_inner(i, tcx, None));
260-
id_from_item_inner(item.item_id, tcx, extra.as_ref())
277+
import.source.did.map(ItemId::from).map(|i| id_from_item_inner(i, tcx, None, None));
278+
id_from_item_inner(item.item_id, tcx, extra.as_ref(), item.name)
261279
}
262-
_ => id_from_item_inner(item.item_id, tcx, None),
280+
_ => id_from_item_inner(item.item_id, tcx, None, item.name),
263281
}
264282
}
265283

@@ -533,7 +551,7 @@ impl FromWithTcx<clean::Path> for Path {
533551
fn from_tcx(path: clean::Path, tcx: TyCtxt<'_>) -> Path {
534552
Path {
535553
name: path.whole_name(),
536-
id: id_from_item_inner(path.def_id().into(), tcx, None),
554+
id: id_from_item_inner(path.def_id().into(), tcx, None, None),
537555
args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
538556
}
539557
}
@@ -710,7 +728,7 @@ impl FromWithTcx<clean::Import> for Import {
710728
Import {
711729
source: import.source.path.whole_name(),
712730
name,
713-
id: import.source.did.map(ItemId::from).map(|i| id_from_item_inner(i, tcx, None)),
731+
id: import.source.did.map(ItemId::from).map(|i| id_from_item_inner(i, tcx, None, None)),
714732
glob,
715733
}
716734
}

src/librustdoc/json/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
243243
.chain(&self.cache.external_paths)
244244
.map(|(&k, &(ref path, kind))| {
245245
(
246-
id_from_item_inner(k.into(), self.tcx, None),
246+
id_from_item_inner(k.into(), self.tcx, None, None),
247247
types::ItemSummary {
248248
crate_id: k.krate.as_u32(),
249249
path: path.iter().map(|s| s.to_string()).collect(),

0 commit comments

Comments
 (0)