Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc-json: switch from HashMap to FxHashMap to fix non-determinism #108626

Merged
merged 1 commit into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,7 @@ dependencies = [
"anyhow",
"clap 4.1.4",
"fs-err",
"rustc-hash",
"rustdoc-json-types",
"serde",
"serde_json",
Expand Down Expand Up @@ -4846,6 +4847,7 @@ dependencies = [
name = "rustdoc-json-types"
version = "0.1.0"
dependencies = [
"rustc-hash",
"serde",
"serde_json",
]
Expand Down
1 change: 1 addition & 0 deletions src/rustdoc-json-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ path = "lib.rs"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
rustc-hash = "1.1.0"

[dev-dependencies]
serde_json = "1.0"
17 changes: 8 additions & 9 deletions src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
//! 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 std::collections::HashMap;
use std::path::PathBuf;

use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// rustdoc format-version.
pub const FORMAT_VERSION: u32 = 24;
Expand All @@ -24,11 +23,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: HashMap<Id, Item>,
pub index: FxHashMap<Id, Item>,
/// Maps IDs to fully qualified paths and other info helpful for generating links.
pub paths: HashMap<Id, ItemSummary>,
pub paths: FxHashMap<Id, ItemSummary>,
/// Maps `crate_id` of items to a crate name and html_root_url if it exists.
pub external_crates: HashMap<u32, ExternalCrate>,
pub external_crates: FxHashMap<u32, ExternalCrate>,
/// A single version number to be used in the future when making backwards incompatible changes
/// to the JSON output.
pub format_version: u32,
Expand All @@ -54,8 +53,8 @@ pub struct ItemSummary {
///
/// Note that items can appear in multiple paths, and the one chosen is implementation
/// defined. Currently, 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.
/// [`String`] is currently `["alloc", "string", "String"]` and [`HashMap`][`std::collections::HashMap`]
/// is `["std", "collections", "hash", "map", "HashMap"]`, but this is subject to change.
pub path: Vec<String>,
/// Whether this item is a struct, trait, macro, etc.
pub kind: ItemKind,
Expand All @@ -80,7 +79,7 @@ pub struct Item {
/// Some("") if there is some documentation but it is empty (EG `#[doc = ""]`).
pub docs: Option<String>,
/// 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: HashMap<String, Id>,
pub links: FxHashMap<String, Id>,
/// Stringified versions of the attributes on this item (e.g. `"#[inline]"`)
pub attrs: Vec<String>,
pub deprecation: Option<Deprecation>,
Expand Down
1 change: 1 addition & 0 deletions src/tools/jsondoclint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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"
29 changes: 14 additions & 15 deletions src/tools/jsondoclint/src/validator/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;

use rustc_hash::FxHashMap;
use rustdoc_json_types::{Crate, Item, ItemKind, ItemSummary, Visibility, FORMAT_VERSION};

use crate::json_find::SelectorPart;
Expand Down Expand Up @@ -27,7 +26,7 @@ fn errors_on_missing_links() {
root: id("0"),
crate_version: None,
includes_private: false,
index: HashMap::from_iter([(
index: FxHashMap::from_iter([(
id("0"),
Item {
name: Some("root".to_owned()),
Expand All @@ -36,7 +35,7 @@ fn errors_on_missing_links() {
span: None,
visibility: Visibility::Public,
docs: None,
links: HashMap::from_iter([("Not Found".to_owned(), id("1"))]),
links: FxHashMap::from_iter([("Not Found".to_owned(), id("1"))]),
attrs: vec![],
deprecation: None,
inner: ItemEnum::Module(Module {
Expand All @@ -46,8 +45,8 @@ fn errors_on_missing_links() {
}),
},
)]),
paths: HashMap::new(),
external_crates: HashMap::new(),
paths: FxHashMap::default(),
external_crates: FxHashMap::default(),
format_version: rustdoc_json_types::FORMAT_VERSION,
};

Expand All @@ -73,7 +72,7 @@ fn errors_on_local_in_paths_and_not_index() {
root: id("0:0:1572"),
crate_version: None,
includes_private: false,
index: HashMap::from_iter([
index: FxHashMap::from_iter([
(
id("0:0:1572"),
Item {
Expand All @@ -83,7 +82,7 @@ fn errors_on_local_in_paths_and_not_index() {
span: None,
visibility: Visibility::Public,
docs: None,
links: HashMap::from_iter([(("prim@i32".to_owned(), id("0:1:1571")))]),
links: FxHashMap::from_iter([(("prim@i32".to_owned(), id("0:1:1571")))]),
attrs: Vec::new(),
deprecation: None,
inner: ItemEnum::Module(Module {
Expand All @@ -102,22 +101,22 @@ fn errors_on_local_in_paths_and_not_index() {
span: None,
visibility: Visibility::Public,
docs: None,
links: HashMap::default(),
links: FxHashMap::default(),
attrs: Vec::new(),
deprecation: None,
inner: ItemEnum::Primitive(Primitive { name: "i32".to_owned(), impls: vec![] }),
},
),
]),
paths: HashMap::from_iter([(
paths: FxHashMap::from_iter([(
id("0:1:1571"),
ItemSummary {
crate_id: 0,
path: vec!["microcore".to_owned(), "i32".to_owned()],
kind: ItemKind::Primitive,
},
)]),
external_crates: HashMap::default(),
external_crates: FxHashMap::default(),
format_version: rustdoc_json_types::FORMAT_VERSION,
};

Expand All @@ -137,7 +136,7 @@ fn checks_local_crate_id_is_correct() {
root: id("root"),
crate_version: None,
includes_private: false,
index: HashMap::from_iter([(
index: FxHashMap::from_iter([(
id("root"),
Item {
id: id("root"),
Expand All @@ -146,7 +145,7 @@ fn checks_local_crate_id_is_correct() {
span: None,
visibility: Visibility::Public,
docs: None,
links: HashMap::default(),
links: FxHashMap::default(),
attrs: Vec::new(),
deprecation: None,
inner: ItemEnum::Module(Module {
Expand All @@ -156,8 +155,8 @@ fn checks_local_crate_id_is_correct() {
}),
},
)]),
paths: HashMap::default(),
external_crates: HashMap::default(),
paths: FxHashMap::default(),
external_crates: FxHashMap::default(),
format_version: FORMAT_VERSION,
};
check(&krate, &[]);
Expand Down
14 changes: 5 additions & 9 deletions tests/run-make/rustdoc-verify-output-files/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ all:
# Check if expected json file is generated
[ -e $(OUTPUT_DIR)/foobar.json ]

# TODO
# We should re-generate json doc once again and compare the diff with previously
# generated one. Because layout of json docs changes in each compilation, we can't
# do that currently.
#
# See https://github.com/rust-lang/rust/issues/103785#issuecomment-1307425590 for details.
# Copy first json output to check if it's exactly same after second compilation
cp -R $(OUTPUT_DIR)/foobar.json $(TMP_OUTPUT_DIR)/foobar.json

# remove generated json doc
rm $(OUTPUT_DIR)/foobar.json
# Generate json doc on the same output
$(RUSTDOC) src/lib.rs --crate-name foobar --crate-type lib --out-dir $(OUTPUT_DIR) -Z unstable-options --output-format json

# Check if json doc compilation broke any of the html files generated previously
# Check if all docs(including both json and html formats) are still the same after multiple compilations
$(DIFF) -r -q $(OUTPUT_DIR) $(TMP_OUTPUT_DIR)