Skip to content

Commit 71dafca

Browse files
committed
Auto merge of #12616 - Eh2406:lazy_static, r=epage
stop using lazy_static ### What does this PR try to resolve? Remove the dependency on `lazy_static` and replace with `std::sync::OnceLock`. ### How should we test and review this PR? This was an internal re-factor, and the tests still pass. I did not test that the intended caching was still working.
2 parents d619310 + 16b330b commit 71dafca

File tree

6 files changed

+37
-37
lines changed

6 files changed

+37
-37
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ im-rc = "15.1.0"
5454
indexmap = "2"
5555
itertools = "0.10.0"
5656
jobserver = "0.1.26"
57-
lazy_static = "1.4.0"
5857
lazycell = "1.3.0"
5958
libc = "0.2.147"
6059
libgit2-sys = "0.16.1"

crates/cargo-test-support/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ flate2.workspace = true
1919
git2.workspace = true
2020
glob.workspace = true
2121
itertools.workspace = true
22-
lazy_static.workspace = true
2322
pasetors.workspace = true
2423
serde = { workspace = true, features = ["derive"] }
2524
serde_json.workspace = true

crates/cargo-test-support/src/lib.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::os;
1212
use std::path::{Path, PathBuf};
1313
use std::process::{Command, Output};
1414
use std::str;
15+
use std::sync::OnceLock;
1516
use std::time::{self, Duration};
1617

1718
use anyhow::{bail, Result};
@@ -1157,13 +1158,14 @@ impl RustcInfo {
11571158
}
11581159
}
11591160

1160-
lazy_static::lazy_static! {
1161-
static ref RUSTC_INFO: RustcInfo = RustcInfo::new();
1161+
fn rustc_info() -> &'static RustcInfo {
1162+
static RUSTC_INFO: OnceLock<RustcInfo> = OnceLock::new();
1163+
RUSTC_INFO.get_or_init(RustcInfo::new)
11621164
}
11631165

11641166
/// The rustc host such as `x86_64-unknown-linux-gnu`.
11651167
pub fn rustc_host() -> &'static str {
1166-
&RUSTC_INFO.host
1168+
&rustc_info().host
11671169
}
11681170

11691171
/// The host triple suitable for use in a cargo environment variable (uppercased).
@@ -1172,7 +1174,7 @@ pub fn rustc_host_env() -> String {
11721174
}
11731175

11741176
pub fn is_nightly() -> bool {
1175-
let vv = &RUSTC_INFO.verbose_version;
1177+
let vv = &rustc_info().verbose_version;
11761178
// CARGO_TEST_DISABLE_NIGHTLY is set in rust-lang/rust's CI so that all
11771179
// nightly-only tests are disabled there. Otherwise, it could make it
11781180
// difficult to land changes which would need to be made simultaneously in
@@ -1225,28 +1227,27 @@ pub trait TestEnv: Sized {
12251227
if env::var_os("RUSTUP_TOOLCHAIN").is_some() {
12261228
// Override the PATH to avoid executing the rustup wrapper thousands
12271229
// of times. This makes the testsuite run substantially faster.
1228-
lazy_static::lazy_static! {
1229-
static ref RUSTC_DIR: PathBuf = {
1230-
match ProcessBuilder::new("rustup")
1231-
.args(&["which", "rustc"])
1232-
.exec_with_output()
1233-
{
1234-
Ok(output) => {
1235-
let s = str::from_utf8(&output.stdout).expect("utf8").trim();
1236-
let mut p = PathBuf::from(s);
1237-
p.pop();
1238-
p
1239-
}
1240-
Err(e) => {
1241-
panic!("RUSTUP_TOOLCHAIN was set, but could not run rustup: {}", e);
1242-
}
1230+
static RUSTC_DIR: OnceLock<PathBuf> = OnceLock::new();
1231+
let rustc_dir = RUSTC_DIR.get_or_init(|| {
1232+
match ProcessBuilder::new("rustup")
1233+
.args(&["which", "rustc"])
1234+
.exec_with_output()
1235+
{
1236+
Ok(output) => {
1237+
let s = str::from_utf8(&output.stdout).expect("utf8").trim();
1238+
let mut p = PathBuf::from(s);
1239+
p.pop();
1240+
p
12431241
}
1244-
};
1245-
}
1242+
Err(e) => {
1243+
panic!("RUSTUP_TOOLCHAIN was set, but could not run rustup: {}", e);
1244+
}
1245+
}
1246+
});
12461247
let path = env::var_os("PATH").unwrap_or_default();
12471248
let paths = env::split_paths(&path);
12481249
let new_path =
1249-
env::join_paths(std::iter::once(RUSTC_DIR.clone()).chain(paths)).unwrap();
1250+
env::join_paths(std::iter::once(rustc_dir.clone()).chain(paths)).unwrap();
12501251
self = self.env("PATH", new_path);
12511252
}
12521253

@@ -1408,11 +1409,14 @@ pub fn is_coarse_mtime() -> bool {
14081409
/// Architectures that do not have a modern processor, hardware emulation, etc.
14091410
/// This provides a way for those setups to increase the cut off for all the time based test.
14101411
pub fn slow_cpu_multiplier(main: u64) -> Duration {
1411-
lazy_static::lazy_static! {
1412-
static ref SLOW_CPU_MULTIPLIER: u64 =
1413-
env::var("CARGO_TEST_SLOW_CPU_MULTIPLIER").ok().and_then(|m| m.parse().ok()).unwrap_or(1);
1414-
}
1415-
Duration::from_secs(*SLOW_CPU_MULTIPLIER * main)
1412+
static SLOW_CPU_MULTIPLIER: OnceLock<u64> = OnceLock::new();
1413+
let slow_cpu_multiplier = SLOW_CPU_MULTIPLIER.get_or_init(|| {
1414+
env::var("CARGO_TEST_SLOW_CPU_MULTIPLIER")
1415+
.ok()
1416+
.and_then(|m| m.parse().ok())
1417+
.unwrap_or(1)
1418+
});
1419+
Duration::from_secs(slow_cpu_multiplier * main)
14161420
}
14171421

14181422
#[cfg(windows)]

crates/resolver-tests/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ publish = false
88
[dependencies]
99
cargo.workspace = true
1010
cargo-util.workspace = true
11-
lazy_static.workspace = true
1211
proptest.workspace = true
1312
varisat.workspace = true

crates/resolver-tests/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
77
use std::fmt;
88
use std::fmt::Write;
99
use std::rc::Rc;
10+
use std::sync::OnceLock;
1011
use std::task::Poll;
1112
use std::time::Instant;
1213

@@ -563,11 +564,11 @@ macro_rules! pkg {
563564
}
564565

565566
fn registry_loc() -> SourceId {
566-
lazy_static::lazy_static! {
567-
static ref EXAMPLE_DOT_COM: SourceId =
568-
SourceId::for_registry(&"https://example.com".into_url().unwrap()).unwrap();
569-
}
570-
*EXAMPLE_DOT_COM
567+
static EXAMPLE_DOT_COM: OnceLock<SourceId> = OnceLock::new();
568+
let example_dot = EXAMPLE_DOT_COM.get_or_init(|| {
569+
SourceId::for_registry(&"https://example.com".into_url().unwrap()).unwrap()
570+
});
571+
*example_dot
571572
}
572573

573574
pub fn pkg<T: ToPkgId>(name: T) -> Summary {

0 commit comments

Comments
 (0)