Repository: rust-analyzer/rust-analyzer
Pull request #5574 (2020-07-29 17:49:31)

Replace rand with oorandom


bors r+
🤖


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index b7a9516e3d..c656f846d8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -400,17 +400,6 @@ version = "0.3.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
 
-[[package]]
-name = "getrandom"
-version = "0.1.14"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
-dependencies = [
- "cfg-if",
- "libc",
- "wasi",
-]
-
 [[package]]
 name = "gimli"
 version = "0.22.0"
@@ -810,6 +799,12 @@ version = "1.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d"
 
+[[package]]
+name = "oorandom"
+version = "11.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a170cebd8021a008ea92e4db85a72f80b35df514ec664b296fdcbb654eac0b2c"
+
 [[package]]
 name = "parking_lot"
 version = "0.11.0"
@@ -868,12 +863,6 @@ version = "0.2.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
 
-[[package]]
-name = "ppv-lite86"
-version = "0.2.8"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
-
 [[package]]
 name = "proc-macro2"
 version = "1.0.19"
@@ -1046,6 +1035,7 @@ dependencies = [
  "indexmap",
  "itertools",
  "log",
+ "oorandom",
  "ra_assists",
  "ra_cfg",
  "ra_db",
@@ -1056,7 +1046,6 @@ dependencies = [
  "ra_ssr",
  "ra_syntax",
  "ra_text_edit",
- "rand",
  "rustc-hash",
  "stdx",
  "test_utils",
@@ -1217,57 +1206,6 @@ dependencies = [
  "stdx",
 ]
 
-[[package]]
-name = "rand"
-version = "0.7.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
-dependencies = [
- "getrandom",
- "libc",
- "rand_chacha",
- "rand_core",
- "rand_hc",
- "rand_pcg",
-]
-
-[[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",
-]
-
-[[package]]
-name = "rand_core"
-version = "0.5.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
-dependencies = [
- "getrandom",
-]
-
-[[package]]
-name = "rand_hc"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
-dependencies = [
- "rand_core",
-]
-
-[[package]]
-name = "rand_pcg"
-version = "0.2.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429"
-dependencies = [
- "rand_core",
-]
-
 [[package]]
 name = "rayon"
 version = "1.3.1"
@@ -1354,6 +1292,7 @@ dependencies = [
  "lsp-server",
  "lsp-types",
  "mimalloc",
+ "oorandom",
  "parking_lot",
  "pico-args",
  "ra_cfg",
@@ -1372,7 +1311,6 @@ dependencies = [
  "ra_text_edit",
  "ra_toolchain",
  "ra_tt",
- "rand",
  "rayon",
  "rustc-hash",
  "serde",
@@ -1414,16 +1352,16 @@ checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
 
 [[package]]
 name = "salsa"
-version = "0.15.0"
+version = "0.15.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "885b4b99dde959decc84e85dd943bd140b4aabd62db2f8206ef5270f77ec20b9"
+checksum = "d4cdc109fcc9e9450c7ef47fb7474e99bffd51799da03ed0a6c7f0e2cb3848a6"
 dependencies = [
  "crossbeam-utils",
  "indexmap",
  "lock_api",
  "log",
+ "oorandom",
  "parking_lot",
- "rand",
  "rustc-hash",
  "salsa-macros",
  "smallvec",
@@ -1831,12 +1769,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 = "winapi"
 version = "0.2.8"
diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml
index 6f81074912..f4181c4eb8 100644
--- a/crates/ra_ide/Cargo.toml
+++ b/crates/ra_ide/Cargo.toml
@@ -17,7 +17,7 @@ indexmap = "1.3.2"
 itertools = "0.9.0"
 log = "0.4.8"
 rustc-hash = "1.1.0"
-rand = { version = "0.7.3", features = ["small_rng"] }
+oorandom = "11.1.2"
 
 stdx = { path = "../stdx" }
 
diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs
index 0be55bca98..a5e7d28676 100644
--- a/crates/ra_ide/src/syntax_highlighting/html.rs
+++ b/crates/ra_ide/src/syntax_highlighting/html.rs
@@ -1,5 +1,6 @@
 //! Renders a bit of code as HTML.
 
+use oorandom::Rand32;
 use ra_db::SourceDatabase;
 use ra_syntax::{AstNode, TextRange, TextSize};
 
@@ -9,13 +10,12 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
     let parse = db.parse(file_id);
 
     fn rainbowify(seed: u64) -> String {
-        use rand::prelude::*;
-        let mut rng = SmallRng::seed_from_u64(seed);
+        let mut rng = Rand32::new(seed);
         format!(
             "hsl({h},{s}%,{l}%)",
-            h = rng.gen_range::<u16, _, _>(0, 361),
-            s = rng.gen_range::<u16, _, _>(42, 99),
-            l = rng.gen_range::<u16, _, _>(40, 91),
+            h = rng.rand_range(0..361),
+            s = rng.rand_range(42..99),
+            l = rng.rand_range(40..91),
         )
     }
 
diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml
index 3f9c820c5d..931fc61edd 100644
--- a/crates/rust-analyzer/Cargo.toml
+++ b/crates/rust-analyzer/Cargo.toml
@@ -23,7 +23,7 @@ log = "0.4.8"
 lsp-types = { version = "0.78.0", features = ["proposed"] }
 parking_lot = "0.11.0"
 pico-args = "0.3.1"
-rand = { version = "0.7.3", features = ["small_rng"] }
+oorandom = "11.1.2"
 rustc-hash = "1.1.0"
 serde = { version = "1.0.106", features = ["derive"] }
 serde_json = "1.0.48"
diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs
index ccc058682e..66d201ba66 100644
--- a/crates/rust-analyzer/src/cli/analysis_stats.rs
+++ b/crates/rust-analyzer/src/cli/analysis_stats.rs
@@ -1,7 +1,10 @@
 //! Fully type-check project and print various stats, like the number of type
 //! errors.
 
-use std::{path::Path, time::Instant};
+use std::{
+    path::Path,
+    time::{Instant, SystemTime, UNIX_EPOCH},
+};
 
 use hir::{
     db::{AstDatabase, DefDatabase, HirDatabase},
@@ -10,12 +13,12 @@ use hir::{
 use hir_def::FunctionId;
 use hir_ty::{Ty, TypeWalk};
 use itertools::Itertools;
+use oorandom::Rand32;
 use ra_db::{
     salsa::{self, ParallelDatabase},
     SourceDatabaseExt,
 };
 use ra_syntax::AstNode;
-use rand::{seq::SliceRandom, thread_rng};
 use rayon::prelude::*;
 use rustc_hash::FxHashSet;
 use stdx::format_to;
@@ -46,6 +49,11 @@ pub fn analysis_stats(
     load_output_dirs: bool,
     with_proc_macro: bool,
 ) -> Result<()> {
+    let mut rng = {
+        let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
+        Rand32::new(seed)
+    };
+
     let db_load_time = Instant::now();
     let (host, vfs) = load_cargo(path, load_output_dirs, with_proc_macro)?;
     let db = host.raw_database();
@@ -57,7 +65,7 @@ pub fn analysis_stats(
 
     let mut krates = Crate::all(db);
     if randomize {
-        krates.shuffle(&mut thread_rng());
+        shuffle(&mut rng, &mut krates);
     }
     for krate in krates {
         let module = krate.root_module(db).expect("crate without root module");
@@ -72,7 +80,7 @@ pub fn analysis_stats(
     }
 
     if randomize {
-        visit_queue.shuffle(&mut thread_rng());
+        shuffle(&mut rng, &mut visit_queue);
     }
 
     eprintln!("Crates in this dir: {}", num_crates);
@@ -110,7 +118,7 @@ pub fn analysis_stats(
     );
 
     if randomize {
-        funcs.shuffle(&mut thread_rng());
+        shuffle(&mut rng, &mut funcs);
     }
 
     let mut bar = match verbosity {
@@ -306,3 +314,10 @@ pub fn analysis_stats(
 
     Ok(())
 }
+
+fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) {
+    for i in (1..slice.len()).rev() {
+        let idx = rng.rand_range(0..i as u32) as usize;
+        slice.swap(idx, i)
+    }
+}


Test patch:
diff --git a/crates/ra_ide/test_data/rainbow_highlighting.html b/crates/ra_ide/test_data/rainbow_highlighting.html
index 08d83302ce..401e87a73d 100644
--- a/crates/ra_ide/test_data/rainbow_highlighting.html
+++ b/crates/ra_ide/test_data/rainbow_highlighting.html
@@ -36,14 +36,14 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
 </style>
 <pre><code><span class="keyword">fn</span> <span class="function declaration">main</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span>
-    <span class="keyword">let</span> <span class="variable declaration" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span> <span class="operator">=</span> <span class="string_literal">"hello"</span><span class="punctuation">;</span>
-    <span class="keyword">let</span> <span class="variable declaration" data-binding-hash="2705725358298919760" style="color: hsl(17,51%,74%);">x</span> <span class="operator">=</span> <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
-    <span class="keyword">let</span> <span class="variable declaration" data-binding-hash="3365759661443752373" style="color: hsl(127,76%,66%);">y</span> <span class="operator">=</span> <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
+    <span class="keyword">let</span> <span class="variable declaration" data-binding-hash="8121853618659664005" style="color: hsl(273,88%,88%);">hello</span> <span class="operator">=</span> <span class="string_literal">"hello"</span><span class="punctuation">;</span>
+    <span class="keyword">let</span> <span class="variable declaration" data-binding-hash="2705725358298919760" style="color: hsl(76,47%,83%);">x</span> <span class="operator">=</span> <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(273,88%,88%);">hello</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
+    <span class="keyword">let</span> <span class="variable declaration" data-binding-hash="3365759661443752373" style="color: hsl(15,86%,51%);">y</span> <span class="operator">=</span> <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(273,88%,88%);">hello</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
 
-    <span class="keyword">let</span> <span class="variable declaration" data-binding-hash="794745962933817518" style="color: hsl(19,74%,76%);">x</span> <span class="operator">=</span> <span class="string_literal">"other color please!"</span><span class="punctuation">;</span>
-    <span class="keyword">let</span> <span class="variable declaration" data-binding-hash="6717528807933952652" style="color: hsl(85,49%,84%);">y</span> <span class="operator">=</span> <span class="variable" data-binding-hash="794745962933817518" style="color: hsl(19,74%,76%);">x</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
+    <span class="keyword">let</span> <span class="variable declaration" data-binding-hash="794745962933817518" style="color: hsl(127,71%,87%);">x</span> <span class="operator">=</span> <span class="string_literal">"other color please!"</span><span class="punctuation">;</span>
+    <span class="keyword">let</span> <span class="variable declaration" data-binding-hash="6717528807933952652" style="color: hsl(90,74%,79%);">y</span> <span class="operator">=</span> <span class="variable" data-binding-hash="794745962933817518" style="color: hsl(127,71%,87%);">x</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
 <span class="punctuation">}</span>
 
 <span class="keyword">fn</span> <span class="function declaration">bar</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span>
-    <span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span> <span class="operator">=</span> <span class="string_literal">"hello"</span><span class="punctuation">;</span>
+    <span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable" data-binding-hash="8121853618659664005" style="color: hsl(273,88%,88%);">hello</span> <span class="operator">=</span> <span class="string_literal">"hello"</span><span class="punctuation">;</span>
 <span class="punctuation">}</span></code></pre>
\ No newline at end of file
diff --git a/xtask/tests/tidy.rs b/xtask/tests/tidy.rs
index adadffc53a..d65a2acbca 100644
--- a/xtask/tests/tidy.rs
+++ b/xtask/tests/tidy.rs
@@ -55,7 +55,6 @@ fn check_licenses() {
 0BSD OR MIT OR Apache-2.0
 Apache-2.0 OR BSL-1.0
 Apache-2.0 OR MIT
-Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT
 Apache-2.0/MIT
 BSD-2-Clause
 BSD-3-Clause