From 06cb136014ce811fe951adf8c449f82cd6808cb6 Mon Sep 17 00:00:00 2001 From: Alona Enraght-Moony Date: Fri, 7 Apr 2023 15:04:38 +0100 Subject: [PATCH] Use same `FxHashMap` in `rustdoc-json-types` and `librustdoc`. The original motivation was me trying to remove the `#![allow(rustc::default_hash_types)]`, as after #108626, we should be using `FxHashMap` here. I then realized I should also be able to remove the `.into_iter().collect()`, as we no longer need to convert from `FxHashMap` to `std::HashMap`. However, this didn't work, and I got the following error ``` error[E0308]: mismatched types --> src/librustdoc/json/mod.rs:235:13 | 235 | index, | ^^^^^ expected `rustc_hash::FxHasher`, found `FxHasher` | = note: `FxHasher` and `rustc_hash::FxHasher` have similar names, but are actually distinct types note: `FxHasher` is defined in crate `rustc_hash` --> /cargo/registry/src/index.crates.io-6f17d22bba15001f/rustc-hash-1.1.0/src/lib.rs:60:1 note: `rustc_hash::FxHasher` is defined in crate `rustc_hash` --> /home/alona/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rustc-hash-1.1.0/src/lib.rs:60:1 | 60 | pub struct FxHasher { | ^^^^^^^^^^^^^^^^^^^ = note: perhaps two different versions of crate `rustc_hash` are being used? ``` This is because `librustdoc` got it's `FxHashMap` via the sysroot `rustc-data-strucures`, whereas `rustdoc-json-types` got it via `rustc-hash` on crates.io. To avoid this, `rustdoc-json-types` now uses `#![feature(rustc_private)]` to load the same version as `librustdoc`. However, this needs to be placed behind a feature, as `rustdoc-json-types` is also dependency of `src/tools/jsondocck`, which means need needs to be buildable without nightly features. --- Cargo.lock | 1 - src/librustdoc/Cargo.toml | 2 +- src/librustdoc/json/mod.rs | 5 +--- src/rustdoc-json-types/Cargo.toml | 2 +- src/rustdoc-json-types/lib.rs | 14 +++++++---- src/tools/jsondoclint/Cargo.toml | 1 - src/tools/jsondoclint/src/validator/tests.rs | 26 ++++++++++---------- 7 files changed, 25 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fbe1abf2a3317..69da5005d01da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2074,7 +2074,6 @@ dependencies = [ "anyhow", "clap", "fs-err", - "rustc-hash", "rustdoc-json-types", "serde", "serde_json", diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml index 51fb126cb3407..e2f3cd1db738e 100644 --- a/src/librustdoc/Cargo.toml +++ b/src/librustdoc/Cargo.toml @@ -15,7 +15,7 @@ indexmap = "2" minifier = "0.3.0" pulldown-cmark-old = { version = "0.9.6", package = "pulldown-cmark", default-features = false } regex = "1" -rustdoc-json-types = { path = "../rustdoc-json-types" } +rustdoc-json-types = { path = "../rustdoc-json-types", features = ["rustc-hash"] } serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } smallvec = "1.8.1" diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 0ef24818515c6..256ac4eff138d 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -228,14 +228,11 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { let index = (*self.index).clone().into_inner(); debug!("Constructing Output"); - // This needs to be the default HashMap for compatibility with the public interface for - // rustdoc-json-types - #[allow(rustc::default_hash_types)] let output = types::Crate { root: types::Id(format!("0:0:{}", e.name(self.tcx).as_u32())), crate_version: self.cache.crate_version.clone(), includes_private: self.cache.document_private, - index: index.into_iter().collect(), + index, paths: self .cache .paths diff --git a/src/rustdoc-json-types/Cargo.toml b/src/rustdoc-json-types/Cargo.toml index d3548036d4c72..f0153095a7697 100644 --- a/src/rustdoc-json-types/Cargo.toml +++ b/src/rustdoc-json-types/Cargo.toml @@ -8,7 +8,7 @@ path = "lib.rs" [dependencies] serde = { version = "1.0", features = ["derive"] } -rustc-hash = "1.1.0" +rustc-hash = { version = "1.1.0", optional = true } [dev-dependencies] serde_json = "1.0" diff --git a/src/rustdoc-json-types/lib.rs b/src/rustdoc-json-types/lib.rs index 89115d4d7d667..a9f57aa644c17 100644 --- a/src/rustdoc-json-types/lib.rs +++ b/src/rustdoc-json-types/lib.rs @@ -3,7 +3,11 @@ //! These types are the public API exposed through the `--output-format json` flag. The [`Crate`] //! struct is the root of the JSON blob and all other items are contained within. -use rustc_hash::FxHashMap; +#[cfg(feature = "rustc-hash")] +use rustc_hash::FxHashMap as HashMap; +#[cfg(not(feature = "rustc-hash"))] +use std::collections::HashMap; + use serde::{Deserialize, Serialize}; use std::path::PathBuf; @@ -23,11 +27,11 @@ pub struct Crate { pub includes_private: bool, /// A collection of all items in the local crate as well as some external traits and their /// items that are referenced locally. - pub index: FxHashMap, + pub index: HashMap, /// Maps IDs to fully qualified paths and other info helpful for generating links. - pub paths: FxHashMap, + pub paths: HashMap, /// Maps `crate_id` of items to a crate name and html_root_url if it exists. - pub external_crates: FxHashMap, + pub external_crates: HashMap, /// A single version number to be used in the future when making backwards incompatible changes /// to the JSON output. pub format_version: u32, @@ -79,7 +83,7 @@ pub struct Item { /// Some("") if there is some documentation but it is empty (EG `#[doc = ""]`). pub docs: Option, /// This mapping resolves [intra-doc links](https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md) from the docstring to their IDs - pub links: FxHashMap, + pub links: HashMap, /// Stringified versions of the attributes on this item (e.g. `"#[inline]"`) pub attrs: Vec, pub deprecation: Option, diff --git a/src/tools/jsondoclint/Cargo.toml b/src/tools/jsondoclint/Cargo.toml index 1318a1f447620..8990310a4f474 100644 --- a/src/tools/jsondoclint/Cargo.toml +++ b/src/tools/jsondoclint/Cargo.toml @@ -9,7 +9,6 @@ edition = "2021" anyhow = "1.0.62" clap = { version = "4.0.15", features = ["derive"] } fs-err = "2.8.1" -rustc-hash = "1.1.0" rustdoc-json-types = { version = "0.1.0", path = "../../rustdoc-json-types" } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.85" diff --git a/src/tools/jsondoclint/src/validator/tests.rs b/src/tools/jsondoclint/src/validator/tests.rs index ba0fe11ea2698..d9fb16d0ec058 100644 --- a/src/tools/jsondoclint/src/validator/tests.rs +++ b/src/tools/jsondoclint/src/validator/tests.rs @@ -26,7 +26,7 @@ fn errors_on_missing_links() { root: id("0"), crate_version: None, includes_private: false, - index: FxHashMap::from_iter([( + index: HashMap::from_iter([( id("0"), Item { name: Some("root".to_owned()), @@ -35,7 +35,7 @@ fn errors_on_missing_links() { span: None, visibility: Visibility::Public, docs: None, - links: FxHashMap::from_iter([("Not Found".to_owned(), id("1"))]), + links: HashMap::from_iter([("Not Found".to_owned(), id("1"))]), attrs: vec![], deprecation: None, inner: ItemEnum::Module(Module { @@ -45,8 +45,8 @@ fn errors_on_missing_links() { }), }, )]), - paths: FxHashMap::default(), - external_crates: FxHashMap::default(), + paths: HashMap::default(), + external_crates: HashMap::default(), format_version: rustdoc_json_types::FORMAT_VERSION, }; @@ -72,7 +72,7 @@ fn errors_on_local_in_paths_and_not_index() { root: id("0:0:1572"), crate_version: None, includes_private: false, - index: FxHashMap::from_iter([ + index: HashMap::from_iter([ ( id("0:0:1572"), Item { @@ -82,7 +82,7 @@ fn errors_on_local_in_paths_and_not_index() { span: None, visibility: Visibility::Public, docs: None, - links: FxHashMap::from_iter([(("prim@i32".to_owned(), id("0:1:1571")))]), + links: HashMap::from_iter([(("prim@i32".to_owned(), id("0:1:1571")))]), attrs: Vec::new(), deprecation: None, inner: ItemEnum::Module(Module { @@ -101,14 +101,14 @@ fn errors_on_local_in_paths_and_not_index() { span: None, visibility: Visibility::Public, docs: None, - links: FxHashMap::default(), + links: HashMap::default(), attrs: Vec::new(), deprecation: None, inner: ItemEnum::Primitive(Primitive { name: "i32".to_owned(), impls: vec![] }), }, ), ]), - paths: FxHashMap::from_iter([( + paths: HashMap::from_iter([( id("0:1:1571"), ItemSummary { crate_id: 0, @@ -116,7 +116,7 @@ fn errors_on_local_in_paths_and_not_index() { kind: ItemKind::Primitive, }, )]), - external_crates: FxHashMap::default(), + external_crates: HashMap::default(), format_version: rustdoc_json_types::FORMAT_VERSION, }; @@ -136,7 +136,7 @@ fn checks_local_crate_id_is_correct() { root: id("root"), crate_version: None, includes_private: false, - index: FxHashMap::from_iter([( + index: HashMap::from_iter([( id("root"), Item { id: id("root"), @@ -145,7 +145,7 @@ fn checks_local_crate_id_is_correct() { span: None, visibility: Visibility::Public, docs: None, - links: FxHashMap::default(), + links: HashMap::default(), attrs: Vec::new(), deprecation: None, inner: ItemEnum::Module(Module { @@ -155,8 +155,8 @@ fn checks_local_crate_id_is_correct() { }), }, )]), - paths: FxHashMap::default(), - external_crates: FxHashMap::default(), + paths: HashMap::default(), + external_crates: HashMap::default(), format_version: FORMAT_VERSION, }; check(&krate, &[]);