From 815f5bbcc53dbb40c11127f26080e0ad9b7dc935 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 6 Apr 2023 14:53:29 -0700 Subject: [PATCH 1/5] rustdoc: clean up JS * Stop checking `func` in `onEach`. It's always hard-coded right at the call site, so there's no point. * Use the ternary operator in a few spots where it makes sense. * No point in making `onEach` store `arr.length` in a variable if it's only used once anyway. --- src/librustdoc/html/static/js/main.js | 10 ++-------- src/librustdoc/html/static/js/settings.js | 8 ++------ src/librustdoc/html/static/js/storage.js | 19 +++++-------------- 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 45c0360a49ea6..56ee4c1510e8f 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -332,13 +332,7 @@ function preLoadCss(cssUrl) { }; function getPageId() { - if (window.location.hash) { - const tmp = window.location.hash.replace(/^#/, ""); - if (tmp.length > 0) { - return tmp; - } - } - return null; + return window.location.hash.replace(/^#/, ""); } const toggleAllDocsId = "toggle-all-docs"; @@ -707,7 +701,7 @@ function preLoadCss(cssUrl) { }); const pageId = getPageId(); - if (pageId !== null) { + if (pageId !== "") { expandSection(pageId); } }()); diff --git a/src/librustdoc/html/static/js/settings.js b/src/librustdoc/html/static/js/settings.js index 1cd552e7f25b7..ebbe6c1ca9a24 100644 --- a/src/librustdoc/html/static/js/settings.js +++ b/src/librustdoc/html/static/js/settings.js @@ -86,12 +86,8 @@ if (settingId === "theme") { const useSystem = getSettingValue("use-system-theme"); if (useSystem === "true" || settingValue === null) { - if (useSystem !== "false") { - settingValue = "system preference"; - } else { - // This is the default theme. - settingValue = "light"; - } + // "light" is the default theme + settingValue = useSystem === "false" ? "light" : "system preference"; } } if (settingValue !== null && settingValue !== "null") { diff --git a/src/librustdoc/html/static/js/storage.js b/src/librustdoc/html/static/js/storage.js index 8d82b5b78edbb..9ce09cd502e61 100644 --- a/src/librustdoc/html/static/js/storage.js +++ b/src/librustdoc/html/static/js/storage.js @@ -53,10 +53,9 @@ function removeClass(elem, className) { * @param {boolean} [reversed] - Whether to iterate in reverse */ function onEach(arr, func, reversed) { - if (arr && arr.length > 0 && func) { + if (arr && arr.length > 0) { if (reversed) { - const length = arr.length; - for (let i = length - 1; i >= 0; --i) { + for (let i = arr.length - 1; i >= 0; --i) { if (func(arr[i])) { return true; } @@ -150,26 +149,18 @@ const updateTheme = (function() { * … dictates that it should be. */ function updateTheme() { - const use = (theme, saveTheme) => { - switchTheme(theme, saveTheme); - }; - // maybe the user has disabled the setting in the meantime! if (getSettingValue("use-system-theme") !== "false") { const lightTheme = getSettingValue("preferred-light-theme") || "light"; const darkTheme = getSettingValue("preferred-dark-theme") || "dark"; - if (mql.matches) { - use(darkTheme, true); - } else { - // prefers a light theme, or has no preference - use(lightTheme, true); - } + // use light theme if user prefers it, or has no preference + switchTheme(mql.matches ? darkTheme : lightTheme, true); // note: we save the theme so that it doesn't suddenly change when // the user disables "use-system-theme" and reloads the page or // navigates to another page } else { - use(getSettingValue("theme"), false); + switchTheme(getSettingValue("theme"), false); } } From 5cad51c0c5a21c94bb119277131ac4b60576041a Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 6 Apr 2023 20:25:07 -0700 Subject: [PATCH 2/5] rustdoc: add test and bug fix for theme defaults --- src/librustdoc/html/static/js/storage.js | 1 + tests/rustdoc-gui/theme-defaults.goml | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/rustdoc-gui/theme-defaults.goml diff --git a/src/librustdoc/html/static/js/storage.js b/src/librustdoc/html/static/js/storage.js index 8d82b5b78edbb..67d3981ce07cd 100644 --- a/src/librustdoc/html/static/js/storage.js +++ b/src/librustdoc/html/static/js/storage.js @@ -158,6 +158,7 @@ const updateTheme = (function() { if (getSettingValue("use-system-theme") !== "false") { const lightTheme = getSettingValue("preferred-light-theme") || "light"; const darkTheme = getSettingValue("preferred-dark-theme") || "dark"; + updateLocalStorage("use-system-theme", "true"); if (mql.matches) { use(darkTheme, true); diff --git a/tests/rustdoc-gui/theme-defaults.goml b/tests/rustdoc-gui/theme-defaults.goml new file mode 100644 index 0000000000000..d5ed536b1a962 --- /dev/null +++ b/tests/rustdoc-gui/theme-defaults.goml @@ -0,0 +1,24 @@ +// Ensure that the theme picker always starts with the actual defaults. +goto: "file://" + |DOC_PATH| + "/test_docs/index.html" +click: "#settings-menu" +wait-for: "#theme-system-preference" +assert: "#theme-system-preference:checked" +assert: "#preferred-light-theme-light:checked" +assert: "#preferred-dark-theme-dark:checked" +assert-false: "#preferred-dark-theme-ayu:checked" + +// Test legacy migration from old theme setup without system-preference matching. +// See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732 +local-storage: { + "rustdoc-preferred-light-theme": null, + "rustdoc-preferred-dark-theme": null, + "rustdoc-use-system-theme": null, + "rustdoc-theme": "ayu" +} +goto: "file://" + |DOC_PATH| + "/test_docs/index.html" +click: "#settings-menu" +wait-for: "#theme-system-preference" +assert: "#theme-system-preference:checked" +assert: "#preferred-light-theme-light:checked" +assert-false: "#preferred-dark-theme-dark:checked" +assert: "#preferred-dark-theme-ayu:checked" From 12ad0d6686335e0ee830d85030f815905c2dac90 Mon Sep 17 00:00:00 2001 From: Alona Enraght-Moony Date: Fri, 7 Apr 2023 15:04:38 +0100 Subject: [PATCH 3/5] 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 | 2 -- src/librustdoc/Cargo.toml | 2 +- src/librustdoc/json/mod.rs | 5 +--- src/rustdoc-json-types/Cargo.toml | 4 ++- src/rustdoc-json-types/lib.rs | 17 ++++++++---- src/tools/jsondoclint/Cargo.toml | 1 - src/tools/jsondoclint/src/validator/tests.rs | 28 ++++++++++---------- 7 files changed, 31 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d4fca32d750f6..453efbe1828ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2908,7 +2908,6 @@ dependencies = [ "anyhow", "clap 4.2.1", "fs-err", - "rustc-hash", "rustdoc-json-types", "serde", "serde_json", @@ -5539,7 +5538,6 @@ dependencies = [ name = "rustdoc-json-types" version = "0.1.0" dependencies = [ - "rustc-hash", "serde", "serde_json", ] diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml index 29912b95703b2..2ffb1519db612 100644 --- a/src/librustdoc/Cargo.toml +++ b/src/librustdoc/Cargo.toml @@ -13,7 +13,7 @@ itertools = "0.10.1" minifier = "0.2.2" once_cell = "1.10.0" 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 d6da6e0993894..81f974fb4eaf8 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 d63caa7ad7010..fe61741f1da73 100644 --- a/src/rustdoc-json-types/Cargo.toml +++ b/src/rustdoc-json-types/Cargo.toml @@ -8,7 +8,9 @@ path = "lib.rs" [dependencies] serde = { version = "1.0", features = ["derive"] } -rustc-hash = "1.1.0" [dev-dependencies] serde_json = "1.0" + +[features] +rustc_hash = [] diff --git a/src/rustdoc-json-types/lib.rs b/src/rustdoc-json-types/lib.rs index 4c210291b113b..f13c6542c5583 100644 --- a/src/rustdoc-json-types/lib.rs +++ b/src/rustdoc-json-types/lib.rs @@ -3,7 +3,14 @@ //! 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_attr(feature = "rustc_hash", feature(rustc_private))] +#[cfg(feature = "rustc_hash")] +extern crate rustc_hash; +#[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 +30,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 +86,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 95a56a9dfac45..29563d7e0d250 100644 --- a/src/tools/jsondoclint/src/validator/tests.rs +++ b/src/tools/jsondoclint/src/validator/tests.rs @@ -1,5 +1,5 @@ -use rustc_hash::FxHashMap; use rustdoc_json_types::{Crate, Item, ItemKind, ItemSummary, Visibility, FORMAT_VERSION}; +use std::collections::HashMap; use crate::json_find::SelectorPart; @@ -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, &[]); From 7e9e91c3d395941b21dd1c791ff936ae44b4d45b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Bethune?= Date: Fri, 7 Apr 2023 23:49:20 +0200 Subject: [PATCH 4/5] Fix wrong type in docs: i16 -> u16 @rustbot label +A-docs r? docs --- library/core/src/num/shells/u16.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/num/shells/u16.rs b/library/core/src/num/shells/u16.rs index b203806f46005..7394977e5078a 100644 --- a/library/core/src/num/shells/u16.rs +++ b/library/core/src/num/shells/u16.rs @@ -1,4 +1,4 @@ -//! Redundant constants module for the [`i16` primitive type][i16]. +//! Redundant constants module for the [`u16` primitive type][u16]. //! //! New code should use the associated constants directly on the primitive type. From f8b62ff535ec1957e3a3b36bf7b0debc22ef447c Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Fri, 7 Apr 2023 15:24:08 -0700 Subject: [PATCH 5/5] Remove myself from reviewers list I'm going to be unable to review for the next few weeks, so I'm removing myself from the review queue. Once I'm back and able to review again, I'll add myself back to the list. --- triagebot.toml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/triagebot.toml b/triagebot.toml index 2d7be7d12734a..5f4de0562f89f 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -470,8 +470,8 @@ cc = ["@rust-lang/style"] [mentions."Cargo.lock"] message = """ -These commits modify the `Cargo.lock` file. Random changes to `Cargo.lock` can be introduced when switching branches and rebasing PRs. -This was probably unintentional and should be reverted before this PR is merged. +These commits modify the `Cargo.lock` file. Random changes to `Cargo.lock` can be introduced when switching branches and rebasing PRs. +This was probably unintentional and should be reverted before this PR is merged. If this was intentional then you can ignore this comment. """ @@ -499,7 +499,6 @@ compiler-team = [ ] compiler-team-contributors = [ "@compiler-errors", - "@eholk", "@jackh726", "@TaKO8Ki", "@WaffleLapkin",