diff options
| author | Guillaume Gomez <guillaume.gomez@huawei.com> | 2023-02-14 14:22:25 +0100 |
|---|---|---|
| committer | Guillaume Gomez <guillaume.gomez@huawei.com> | 2023-02-18 18:28:17 +0100 |
| commit | 1ae875f0a30b7a71755fac43a8c6fe00dd205d33 (patch) | |
| tree | 18cf78af41ed82cad529af5338b5f87d7cc44dbd | |
| parent | 3adc0812542e6c9b13c78b85407dcc9d54885e0e (diff) | |
Improve code readability
| -rw-r--r-- | src/librustdoc/json/conversions.rs | 27 | ||||
| -rw-r--r-- | src/librustdoc/json/mod.rs | 4 |
2 files changed, 21 insertions, 10 deletions
diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 89e61404ed0..d5e9010eb4e 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -38,7 +38,7 @@ impl JsonRenderer<'_> { Some(UrlFragment::UserWritten(_)) | None => *page_id, }; - (link.clone(), id_from_item_inner(id.into(), self.tcx, None, None)) + (link.clone(), id_from_item_default(id.into(), self.tcx)) }) .collect(); let docs = item.attrs.collapsed_doc_value(); @@ -108,7 +108,7 @@ impl JsonRenderer<'_> { Some(ty::Visibility::Public) => Visibility::Public, Some(ty::Visibility::Restricted(did)) if did.is_crate_root() => Visibility::Crate, Some(ty::Visibility::Restricted(did)) => Visibility::Restricted { - parent: id_from_item_inner(did.into(), self.tcx, None, None), + parent: id_from_item_default(did.into(), self.tcx), path: self.tcx.def_path(did).to_string_no_crate_verbose(), }, } @@ -205,14 +205,25 @@ impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind { } } +#[inline] +pub(crate) fn id_from_item_default(item_id: ItemId, tcx: TyCtxt<'_>) -> Id { + id_from_item_inner(item_id, tcx, None, None) +} + /// It generates an ID as follows: /// -/// `CRATE_ID:ITEM_ID[:NAME_ID]` (if there is no name, NAME_ID is not generated). +/// `CRATE_ID:ITEM_ID[:NAME_ID][-EXTRA]`: +/// * If there is no `name`, `NAME_ID` is not generated. +/// * If there is no `extra`, `EXTRA` is not generated. +/// +/// * `name` is the item's name if available (it's not for impl blocks for example). +/// * `extra` is used for reexports: it contains the ID of the reexported item. It is used to allow +/// to have items with the same name but different types to both appear in the generated JSON. pub(crate) fn id_from_item_inner( item_id: ItemId, tcx: TyCtxt<'_>, - extra: Option<&Id>, name: Option<Symbol>, + extra: Option<&Id>, ) -> Id { struct DisplayDefId<'a, 'b>(DefId, TyCtxt<'a>, Option<&'b Id>, Option<Symbol>); @@ -275,9 +286,9 @@ pub(crate) fn id_from_item(item: &clean::Item, tcx: TyCtxt<'_>) -> Id { clean::ItemKind::ImportItem(ref import) => { let extra = import.source.did.map(ItemId::from).map(|i| id_from_item_inner(i, tcx, None, None)); - id_from_item_inner(item.item_id, tcx, extra.as_ref(), item.name) + id_from_item_inner(item.item_id, tcx, item.name, extra.as_ref()) } - _ => id_from_item_inner(item.item_id, tcx, None, item.name), + _ => id_from_item_inner(item.item_id, tcx, item.name, None), } } @@ -551,7 +562,7 @@ impl FromWithTcx<clean::Path> for Path { fn from_tcx(path: clean::Path, tcx: TyCtxt<'_>) -> Path { Path { name: path.whole_name(), - id: id_from_item_inner(path.def_id().into(), tcx, None, None), + id: id_from_item_default(path.def_id().into(), tcx), args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))), } } @@ -728,7 +739,7 @@ impl FromWithTcx<clean::Import> for Import { Import { source: import.source.path.whole_name(), name, - id: import.source.did.map(ItemId::from).map(|i| id_from_item_inner(i, tcx, None, None)), + id: import.source.did.map(ItemId::from).map(|i| id_from_item_default(i, tcx)), glob, } } diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index a0785f32cf2..08bceb59cfd 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -28,7 +28,7 @@ use crate::docfs::PathError; use crate::error::Error; use crate::formats::cache::Cache; use crate::formats::FormatRenderer; -use crate::json::conversions::{id_from_item, id_from_item_inner, IntoWithTcx}; +use crate::json::conversions::{id_from_item, id_from_item_default, IntoWithTcx}; use crate::{clean, try_err}; #[derive(Clone)] @@ -243,7 +243,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { .chain(&self.cache.external_paths) .map(|(&k, &(ref path, kind))| { ( - id_from_item_inner(k.into(), self.tcx, None, None), + id_from_item_default(k.into(), self.tcx), types::ItemSummary { crate_id: k.krate.as_u32(), path: path.iter().map(|s| s.to_string()).collect(), |
