Skip to content

Commit

Permalink
[rustdoc-json] Partially remove paths and introduce external_index
Browse files Browse the repository at this point in the history
Introduce a new `external_index` that only contains a `ExternalItem`
struct from the previous `ItemSummary`. This "new" index only contains
external item and is only here so that IDs continue to referring to
an "item".

A previous version of this patch also removed `path` but at least until
rust-lang#99513 is fixed it is
unfortunately still required (as a workaround).
  • Loading branch information
Urgau committed Oct 29, 2022
1 parent 33b530e commit 5c9982d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
7 changes: 3 additions & 4 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,14 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
crate_version: self.cache.crate_version.clone(),
includes_private: self.cache.document_private,
index: index.into_iter().collect(),
paths: self
external_index: self
.cache
.paths
.external_paths
.iter()
.chain(&self.cache.external_paths)
.map(|(&k, &(ref path, kind))| {
(
from_item_id(k.into(), self.tcx),
types::ItemSummary {
types::ExternalItem {
crate_id: k.krate.as_u32(),
path: path.iter().map(|s| s.to_string()).collect(),
kind: kind.into_tcx(self.tcx),
Expand Down
12 changes: 8 additions & 4 deletions src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::path::PathBuf;
use serde::{Deserialize, Serialize};

/// rustdoc format-version.
pub const FORMAT_VERSION: u32 = 22;
pub const FORMAT_VERSION: u32 = 23;

/// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
/// about the language items in the local crate, as well as info about external items to allow
Expand All @@ -25,8 +25,8 @@ pub struct Crate {
/// A collection of all items in the local crate as well as some external traits and their
/// items that are referenced locally.
pub index: HashMap<Id, Item>,
/// Maps IDs to fully qualified paths and other info helpful for generating links.
pub paths: HashMap<Id, ItemSummary>,
/// Maps external IDs to basic informations.
pub external_index: HashMap<Id, ExternalItem>,
/// Maps `crate_id` of items to a crate name and html_root_url if it exists.
pub external_crates: HashMap<u32, ExternalCrate>,
/// A single version number to be used in the future when making backwards incompatible changes
Expand All @@ -45,7 +45,7 @@ pub struct ExternalCrate {
/// question, or can be used by a tool that takes the json output of multiple crates to find
/// the actual item definition with all the relevant info.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ItemSummary {
pub struct ExternalItem {
/// Can be used to look up the name and html_root_url of the crate this item came from in the
/// `external_crates` map.
pub crate_id: u32,
Expand All @@ -56,6 +56,10 @@ pub struct ItemSummary {
/// defined. Currenty, this is the full path to where the item was defined. Eg
/// [`String`] is currently `["alloc", "string", "String"]` and [`HashMap`] is
/// `["std", "collections", "hash", "map", "HashMap"]`, but this is subject to change.
///
/// Be aware that this field maybe be subject to change (like complete removal) in the
/// future, see issue <https://github.com/rust-lang/rust/issues/93522> and
/// <https://github.com/rust-lang/rust/pull/103085> for more information.
pub path: Vec<String>,
/// Whether this item is a struct, trait, macro, etc.
pub kind: ItemKind,
Expand Down
4 changes: 2 additions & 2 deletions src/tools/jsondoclint/src/item_kind.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustdoc_json_types::{Item, ItemEnum, ItemKind, ItemSummary};
use rustdoc_json_types::{ExternalItem, Item, ItemEnum, ItemKind};

/// A univeral way to represent an [`ItemEnum`] or [`ItemKind`]
#[derive(Debug)]
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Kind {
}
}

pub fn from_summary(s: &ItemSummary) -> Self {
pub fn from_summary(s: &ExternalItem) -> Self {
use Kind::*;
match s.kind {
ItemKind::AssocConst => AssocConst,
Expand Down
4 changes: 2 additions & 2 deletions src/tools/jsondoclint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<'a> Validator<'a> {
}
}
} else {
assert!(self.krate.paths.contains_key(id));
assert!(self.krate.external_index.contains_key(id));
}
}

Expand Down Expand Up @@ -424,7 +424,7 @@ impl<'a> Validator<'a> {
fn kind_of(&mut self, id: &Id) -> Option<Kind> {
if let Some(item) = self.krate.index.get(id) {
Some(Kind::from_item(item))
} else if let Some(summary) = self.krate.paths.get(id) {
} else if let Some(summary) = self.krate.external_index.get(id) {
Some(Kind::from_summary(summary))
} else {
None
Expand Down

0 comments on commit 5c9982d

Please sign in to comment.