@@ -12,6 +12,7 @@ use std::os;
12
12
use std:: path:: { Path , PathBuf } ;
13
13
use std:: process:: { Command , Output } ;
14
14
use std:: str;
15
+ use std:: sync:: OnceLock ;
15
16
use std:: time:: { self , Duration } ;
16
17
17
18
use anyhow:: { bail, Result } ;
@@ -1157,13 +1158,14 @@ impl RustcInfo {
1157
1158
}
1158
1159
}
1159
1160
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)
1162
1164
}
1163
1165
1164
1166
/// The rustc host such as `x86_64-unknown-linux-gnu`.
1165
1167
pub fn rustc_host ( ) -> & ' static str {
1166
- & RUSTC_INFO . host
1168
+ & rustc_info ( ) . host
1167
1169
}
1168
1170
1169
1171
/// The host triple suitable for use in a cargo environment variable (uppercased).
@@ -1172,7 +1174,7 @@ pub fn rustc_host_env() -> String {
1172
1174
}
1173
1175
1174
1176
pub fn is_nightly ( ) -> bool {
1175
- let vv = & RUSTC_INFO . verbose_version ;
1177
+ let vv = & rustc_info ( ) . verbose_version ;
1176
1178
// CARGO_TEST_DISABLE_NIGHTLY is set in rust-lang/rust's CI so that all
1177
1179
// nightly-only tests are disabled there. Otherwise, it could make it
1178
1180
// difficult to land changes which would need to be made simultaneously in
@@ -1225,28 +1227,27 @@ pub trait TestEnv: Sized {
1225
1227
if env:: var_os ( "RUSTUP_TOOLCHAIN" ) . is_some ( ) {
1226
1228
// Override the PATH to avoid executing the rustup wrapper thousands
1227
1229
// 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
1243
1241
}
1244
- } ;
1245
- }
1242
+ Err ( e) => {
1243
+ panic ! ( "RUSTUP_TOOLCHAIN was set, but could not run rustup: {}" , e) ;
1244
+ }
1245
+ }
1246
+ } ) ;
1246
1247
let path = env:: var_os ( "PATH" ) . unwrap_or_default ( ) ;
1247
1248
let paths = env:: split_paths ( & path) ;
1248
1249
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 ( ) ;
1250
1251
self = self . env ( "PATH" , new_path) ;
1251
1252
}
1252
1253
@@ -1408,11 +1409,14 @@ pub fn is_coarse_mtime() -> bool {
1408
1409
/// Architectures that do not have a modern processor, hardware emulation, etc.
1409
1410
/// This provides a way for those setups to increase the cut off for all the time based test.
1410
1411
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)
1416
1420
}
1417
1421
1418
1422
#[ cfg( windows) ]
0 commit comments