Skip to content

Commit

Permalink
Auto merge of rust-lang#104658 - thomcc:rand-update-and-usable-no_std…
Browse files Browse the repository at this point in the history
…, r=Mark-Simulacrum

Update `rand` in the stdlib tests, and remove the `getrandom` feature from it.

The main goal is actually removing `getrandom`, so that eventually we can allow running the stdlib test suite on tier3 targets which don't have `getrandom` support. Currently those targets can only run the subset of stdlib tests that exist in uitests, and (generally speaking), we prefer not to test libstd functionality in uitests, which came up recently in rust-lang#104095 and rust-lang#104185. Additionally, the fact that we can't update `rand`/`getrandom` means we're stuck with the old set of tier3 targets, so can't test new ones.

~~Anyway, I haven't checked that this actually does allow use on tier3 targets (I think it does not, as some work is needed in stdlib submodules) but it moves us slightly closer to this, and seems to allow at least finally updating our `rand` dep, which definitely improves the status quo.~~ Checked and works now.

For the most part, our tests and benchmarks are fine using hard-coded seeds. A couple tests seem to fail with this (stuff manipulating the environment expecting no collisions, for example), or become pointless (all inputs to a function become equivalent). In these cases I've done a (gross) dance (ab)using `RandomState` and `Location::caller()` for some extra "entropy".

Trying to share that code seems *way* more painful than it's worth given that the duplication is a 7-line function, even if the lines are quite gross. (Keeping in mind that sharing it would require adding `rand` as a non-dev dep to std, and exposing a type from it publicly, all of which sounds truly awful, even if done behind a perma-unstable feature).

See also some previous attempts:
- rust-lang#86963 (in particular rust-lang#86963 (comment) which explains why this is non-trivial)
- rust-lang#89131
- rust-lang#96626 (comment) (I tried in that PR at the same time, but settled for just removing the usage of `thread_rng()` from the benchmarks, since that was the main goal).
- rust-lang#104185
- Probably more. It's very tempting of a thing to "just update".

r? `@Mark-Simulacrum`
  • Loading branch information
bors committed Jan 8, 2023
2 parents e5d46a5 + a4bf36e commit 2afe585
Show file tree
Hide file tree
Showing 22 changed files with 508 additions and 480 deletions.
125 changes: 34 additions & 91 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43bb833f0bf979d8475d38fbf09ed3b8a55e1885fe93ad3f93239fc6a4f17b98"
dependencies = [
"getrandom 0.2.8",
"getrandom",
"once_cell",
"version_check",
]
Expand All @@ -50,7 +50,7 @@ version = "0.0.0"
dependencies = [
"compiler_builtins",
"core",
"rand 0.7.3",
"rand",
"rand_xorshift",
]

Expand Down Expand Up @@ -951,7 +951,7 @@ checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
name = "core"
version = "0.0.0"
dependencies = [
"rand 0.7.3",
"rand",
"rand_xorshift",
]

Expand Down Expand Up @@ -1055,7 +1055,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef2b4b23cddf68b89b8f8069890e8c270d54e2d5fe1b143820234805e4cb17ef"
dependencies = [
"generic-array",
"rand_core 0.6.4",
"rand_core",
"subtle",
"zeroize",
]
Expand Down Expand Up @@ -1314,7 +1314,7 @@ version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a3d382e8464107391c8706b4c14b087808ecb909f6c15c34114bc42e53a9e4c"
dependencies = [
"getrandom 0.2.8",
"getrandom",
]

[[package]]
Expand Down Expand Up @@ -1351,7 +1351,7 @@ dependencies = [
"hkdf",
"pem-rfc7468",
"pkcs8",
"rand_core 0.6.4",
"rand_core",
"sec1",
"subtle",
"zeroize",
Expand Down Expand Up @@ -1482,7 +1482,7 @@ version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d013fc25338cc558c5c2cfbad646908fb23591e2404481826742b651c9af7160"
dependencies = [
"rand_core 0.6.4",
"rand_core",
"subtle",
]

Expand Down Expand Up @@ -1749,17 +1749,6 @@ dependencies = [
"unicode-width",
]

[[package]]
name = "getrandom"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]

[[package]]
name = "getrandom"
version = "0.2.8"
Expand All @@ -1769,7 +1758,7 @@ dependencies = [
"cfg-if",
"js-sys",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
"wasi",
"wasm-bindgen",
]

Expand Down Expand Up @@ -1840,7 +1829,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dfbfb3a6cfbd390d5c9564ab283a0349b9b9fcd46a706c1eb10e0db70bfbac7"
dependencies = [
"ff",
"rand_core 0.6.4",
"rand_core",
"subtle",
]

Expand Down Expand Up @@ -2115,7 +2104,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af1955a75fa080c677d3972822ec4bad316169ab1cfc6c257a942c2265dbe5fe"
dependencies = [
"bitmaps",
"rand_core 0.6.4",
"rand_core",
"rand_xoshiro",
"sized-chunks",
"typenum",
Expand Down Expand Up @@ -2660,14 +2649,14 @@ version = "0.1.0"
dependencies = [
"colored",
"env_logger 0.9.0",
"getrandom 0.2.8",
"getrandom",
"lazy_static",
"libc",
"libffi",
"libloading",
"log",
"measureme",
"rand 0.8.5",
"rand",
"regex",
"rustc-workspace-hack",
"rustc_version",
Expand Down Expand Up @@ -2970,10 +2959,10 @@ checksum = "ed20c4c21d893414f42e0cbfebe8a8036b5ae9b0264611fb6504e395eda6ceec"
dependencies = [
"ct-codecs",
"ed25519-compact",
"getrandom 0.2.8",
"getrandom",
"orion",
"p384",
"rand_core 0.6.4",
"rand_core",
"regex",
"serde",
"serde_json",
Expand Down Expand Up @@ -3093,7 +3082,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6"
dependencies = [
"phf_shared",
"rand 0.8.5",
"rand",
]

[[package]]
Expand Down Expand Up @@ -3298,38 +3287,15 @@ dependencies = [
"proc-macro2",
]

[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom 0.1.16",
"libc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc",
]

[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha 0.3.0",
"rand_core 0.6.4",
]

[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core 0.5.1",
"rand_chacha",
"rand_core",
]

[[package]]
Expand All @@ -3339,16 +3305,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core 0.6.4",
]

[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom 0.1.16",
"rand_core",
]

[[package]]
Expand All @@ -3357,25 +3314,16 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom 0.2.8",
]

[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core 0.5.1",
"getrandom",
]

[[package]]
name = "rand_xorshift"
version = "0.2.0"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77d416b86801d23dde1aa643023b775c3a462efc0ed96443add11546cdf1dca8"
checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f"
dependencies = [
"rand_core 0.5.1",
"rand_core",
]

[[package]]
Expand All @@ -3384,7 +3332,7 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa"
dependencies = [
"rand_core 0.6.4",
"rand_core",
]

[[package]]
Expand Down Expand Up @@ -3426,7 +3374,7 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
dependencies = [
"getrandom 0.2.8",
"getrandom",
"redox_syscall",
]

Expand Down Expand Up @@ -3640,10 +3588,10 @@ version = "1.0.0"
dependencies = [
"bstr 0.2.17",
"clap 3.2.20",
"getrandom 0.2.8",
"getrandom",
"libc",
"libz-sys",
"rand 0.8.5",
"rand",
"regex",
"serde_json",
"syn",
Expand All @@ -3656,7 +3604,7 @@ name = "rustc_abi"
version = "0.0.0"
dependencies = [
"bitflags",
"rand 0.8.5",
"rand",
"rand_xoshiro",
"rustc_data_structures",
"rustc_index",
Expand Down Expand Up @@ -4162,7 +4110,7 @@ dependencies = [
name = "rustc_incremental"
version = "0.0.0"
dependencies = [
"rand 0.8.5",
"rand",
"rustc_ast",
"rustc_data_structures",
"rustc_errors",
Expand Down Expand Up @@ -5185,7 +5133,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c"
dependencies = [
"digest",
"rand_core 0.6.4",
"rand_core",
]

[[package]]
Expand Down Expand Up @@ -5345,11 +5293,12 @@ dependencies = [
"panic_abort",
"panic_unwind",
"profiler_builtins",
"rand 0.7.3",
"rand",
"rand_xorshift",
"rustc-demangle",
"std_detect",
"unwind",
"wasi 0.11.0+wasi-snapshot-preview1",
"wasi",
]

[[package]]
Expand Down Expand Up @@ -5821,7 +5770,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675"
dependencies = [
"cfg-if",
"rand 0.8.5",
"rand",
"static_assertions",
]

Expand Down Expand Up @@ -6104,7 +6053,7 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [
"getrandom 0.2.8",
"getrandom",
]

[[package]]
Expand Down Expand Up @@ -6145,12 +6094,6 @@ dependencies = [
"winapi-util",
]

[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"

[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ core = { path = "../core" }
compiler_builtins = { version = "0.1.40", features = ['rustc-dep-of-std'] }

[dev-dependencies]
rand = "0.7"
rand_xorshift = "0.2"
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
rand_xorshift = "0.3.0"

[[test]]
name = "collectionstests"
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/benches/slice.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{mem, ptr};

use rand::distributions::{Alphanumeric, Standard};
use rand::distributions::{Alphanumeric, DistString, Standard};
use rand::Rng;
use test::{black_box, Bencher};

Expand Down Expand Up @@ -218,7 +218,7 @@ fn gen_strings(len: usize) -> Vec<String> {
let mut v = vec![];
for _ in 0..len {
let n = rng.gen::<usize>() % 20 + 1;
v.push((&mut rng).sample_iter(&Alphanumeric).take(n).collect());
v.push(Alphanumeric.sample_string(&mut rng, n));
}
v
}
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/collections/binary_heap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ fn test_retain() {
#[test]
#[cfg(not(target_os = "emscripten"))]
fn panic_safe() {
use rand::{seq::SliceRandom, thread_rng};
use rand::seq::SliceRandom;
use std::cmp;
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::{AtomicUsize, Ordering};
Expand All @@ -490,7 +490,7 @@ fn panic_safe() {
self.0.partial_cmp(&other.0)
}
}
let mut rng = thread_rng();
let mut rng = crate::test_helpers::test_rng();
const DATASZ: usize = 32;
// Miri is too slow
let ntest = if cfg!(miri) { 1 } else { 10 };
Expand Down
Loading

0 comments on commit 2afe585

Please sign in to comment.