Skip to content

Commit da897df

Browse files
committed
Auto merge of #76216 - marmeladema:use-once-cell-from-std, r=matklad
compiler: use `OnceCell` from std Fixes #76192 The only remaining direct use of `lazy_static` crate is in `src/bootstrap` but I am not sure how I can remove that dependency for now. r? @matklad
2 parents b4acb11 + 99c96c5 commit da897df

File tree

15 files changed

+56
-71
lines changed

15 files changed

+56
-71
lines changed

Cargo.lock

-5
Original file line numberDiff line numberDiff line change
@@ -3417,7 +3417,6 @@ dependencies = [
34173417
"ena",
34183418
"indexmap",
34193419
"jobserver",
3420-
"lazy_static",
34213420
"libc",
34223421
"measureme",
34233422
"parking_lot 0.10.2",
@@ -3440,7 +3439,6 @@ dependencies = [
34403439
name = "rustc_driver"
34413440
version = "0.0.0"
34423441
dependencies = [
3443-
"lazy_static",
34443442
"libc",
34453443
"rustc_ast",
34463444
"rustc_ast_pretty",
@@ -3514,7 +3512,6 @@ dependencies = [
35143512
name = "rustc_feature"
35153513
version = "0.0.0"
35163514
dependencies = [
3517-
"lazy_static",
35183515
"rustc_data_structures",
35193516
"rustc_span",
35203517
]
@@ -3531,7 +3528,6 @@ version = "0.0.0"
35313528
name = "rustc_hir"
35323529
version = "0.0.0"
35333530
dependencies = [
3534-
"lazy_static",
35353531
"rustc_ast",
35363532
"rustc_data_structures",
35373533
"rustc_index",
@@ -3606,7 +3602,6 @@ name = "rustc_interface"
36063602
version = "0.0.0"
36073603
dependencies = [
36083604
"libc",
3609-
"once_cell",
36103605
"rustc-rayon",
36113606
"rustc_ast",
36123607
"rustc_ast_lowering",

compiler/rustc_data_structures/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ ena = "0.14"
1212
indexmap = "1.5.1"
1313
tracing = "0.1"
1414
jobserver_crate = { version = "0.1.13", package = "jobserver" }
15-
lazy_static = "1"
1615
rustc_serialize = { path = "../rustc_serialize" }
1716
rustc_macros = { path = "../rustc_macros" }
1817
rustc_graphviz = { path = "../rustc_graphviz" }

compiler/rustc_data_structures/src/jobserver.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
pub use jobserver_crate::Client;
2-
use lazy_static::lazy_static;
2+
use std::lazy::SyncLazy;
33

4-
lazy_static! {
5-
// We can only call `from_env` once per process
4+
// We can only call `from_env` once per process
65

7-
// Note that this is unsafe because it may misinterpret file descriptors
8-
// on Unix as jobserver file descriptors. We hopefully execute this near
9-
// the beginning of the process though to ensure we don't get false
10-
// positives, or in other words we try to execute this before we open
11-
// any file descriptors ourselves.
12-
//
13-
// Pick a "reasonable maximum" if we don't otherwise have
14-
// a jobserver in our environment, capping out at 32 so we
15-
// don't take everything down by hogging the process run queue.
16-
// The fixed number is used to have deterministic compilation
17-
// across machines.
18-
//
19-
// Also note that we stick this in a global because there could be
20-
// multiple rustc instances in this process, and the jobserver is
21-
// per-process.
22-
static ref GLOBAL_CLIENT: Client = unsafe {
23-
Client::from_env().unwrap_or_else(|| {
24-
let client = Client::new(32).expect("failed to create jobserver");
25-
// Acquire a token for the main thread which we can release later
26-
client.acquire_raw().ok();
27-
client
28-
})
29-
};
30-
}
6+
// Note that this is unsafe because it may misinterpret file descriptors
7+
// on Unix as jobserver file descriptors. We hopefully execute this near
8+
// the beginning of the process though to ensure we don't get false
9+
// positives, or in other words we try to execute this before we open
10+
// any file descriptors ourselves.
11+
//
12+
// Pick a "reasonable maximum" if we don't otherwise have
13+
// a jobserver in our environment, capping out at 32 so we
14+
// don't take everything down by hogging the process run queue.
15+
// The fixed number is used to have deterministic compilation
16+
// across machines.
17+
//
18+
// Also note that we stick this in a global because there could be
19+
// multiple rustc instances in this process, and the jobserver is
20+
// per-process.
21+
static GLOBAL_CLIENT: SyncLazy<Client> = SyncLazy::new(|| unsafe {
22+
Client::from_env().unwrap_or_else(|| {
23+
let client = Client::new(32).expect("failed to create jobserver");
24+
// Acquire a token for the main thread which we can release later
25+
client.acquire_raw().ok();
26+
client
27+
})
28+
});
3129

3230
pub fn client() -> Client {
3331
GLOBAL_CLIENT.clone()

compiler/rustc_driver/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ edition = "2018"
88
crate-type = ["dylib"]
99

1010
[dependencies]
11-
lazy_static = "1.0"
1211
libc = "0.2"
1312
tracing = { version = "0.1.18", features = ["release_max_level_info"] }
1413
tracing-subscriber = { version = "0.2.10", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] }

compiler/rustc_driver/src/lib.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
#[macro_use]
1313
extern crate tracing;
14-
#[macro_use]
15-
extern crate lazy_static;
1614

1715
pub extern crate rustc_plugin_impl as plugin;
1816

@@ -49,6 +47,7 @@ use std::env;
4947
use std::ffi::OsString;
5048
use std::fs;
5149
use std::io::{self, Read, Write};
50+
use std::lazy::SyncLazy;
5251
use std::mem;
5352
use std::panic::{self, catch_unwind};
5453
use std::path::PathBuf;
@@ -1142,13 +1141,12 @@ pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
11421141
}
11431142
}
11441143

1145-
lazy_static! {
1146-
static ref DEFAULT_HOOK: Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static> = {
1144+
static DEFAULT_HOOK: SyncLazy<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
1145+
SyncLazy::new(|| {
11471146
let hook = panic::take_hook();
11481147
panic::set_hook(Box::new(|info| report_ice(info, BUG_REPORT_URL)));
11491148
hook
1150-
};
1151-
}
1149+
});
11521150

11531151
/// Prints the ICE message, including backtrace and query stack.
11541152
///
@@ -1223,7 +1221,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
12231221
///
12241222
/// A custom rustc driver can skip calling this to set up a custom ICE hook.
12251223
pub fn install_ice_hook() {
1226-
lazy_static::initialize(&DEFAULT_HOOK);
1224+
SyncLazy::force(&DEFAULT_HOOK);
12271225
}
12281226

12291227
/// This allows tools to enable rust logging without having to magically match rustc's

compiler/rustc_feature/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ doctest = false
99

1010
[dependencies]
1111
rustc_data_structures = { path = "../rustc_data_structures" }
12-
lazy_static = "1.0.0"
1312
rustc_span = { path = "../rustc_span" }

compiler/rustc_feature/src/builtin_attrs.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use AttributeType::*;
55

66
use crate::{Features, Stability};
77

8-
use lazy_static::lazy_static;
98
use rustc_data_structures::fx::FxHashMap;
109
use rustc_span::symbol::{sym, Symbol};
1110

11+
use std::lazy::SyncLazy;
12+
1213
type GateFn = fn(&Features) -> bool;
1314

1415
macro_rules! cfg_fn {
@@ -589,14 +590,13 @@ pub fn is_builtin_attr_name(name: Symbol) -> bool {
589590
BUILTIN_ATTRIBUTE_MAP.get(&name).is_some()
590591
}
591592

592-
lazy_static! {
593-
pub static ref BUILTIN_ATTRIBUTE_MAP: FxHashMap<Symbol, &'static BuiltinAttribute> = {
593+
pub static BUILTIN_ATTRIBUTE_MAP: SyncLazy<FxHashMap<Symbol, &'static BuiltinAttribute>> =
594+
SyncLazy::new(|| {
594595
let mut map = FxHashMap::default();
595596
for attr in BUILTIN_ATTRIBUTES.iter() {
596597
if map.insert(attr.0, attr).is_some() {
597598
panic!("duplicate builtin attribute `{}`", attr.0);
598599
}
599600
}
600601
map
601-
};
602-
}
602+
});

compiler/rustc_feature/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//! even if it is stabilized or removed, *do not remove it*. Instead, move the
1212
//! symbol to the `accepted` or `removed` modules respectively.
1313
14+
#![feature(once_cell)]
15+
1416
mod accepted;
1517
mod active;
1618
mod builtin_attrs;

compiler/rustc_hir/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ rustc_index = { path = "../rustc_index" }
1515
rustc_span = { path = "../rustc_span" }
1616
rustc_serialize = { path = "../rustc_serialize" }
1717
rustc_ast = { path = "../rustc_ast" }
18-
lazy_static = "1"
1918
tracing = "0.1"
2019
smallvec = { version = "1.0", features = ["union", "may_dangle"] }

compiler/rustc_hir/src/lang_items.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_macros::HashStable_Generic;
1717
use rustc_span::symbol::{kw, sym, Symbol};
1818
use rustc_span::Span;
1919

20-
use lazy_static::lazy_static;
20+
use std::lazy::SyncLazy;
2121

2222
pub enum LangItemGroup {
2323
Op,
@@ -117,14 +117,12 @@ macro_rules! language_item_table {
117117
)*
118118
}
119119

120-
lazy_static! {
121-
/// A mapping from the name of the lang item to its order and the form it must be of.
122-
pub static ref ITEM_REFS: FxHashMap<Symbol, (usize, Target)> = {
123-
let mut item_refs = FxHashMap::default();
124-
$( item_refs.insert($name, (LangItem::$variant as usize, $target)); )*
125-
item_refs
126-
};
127-
}
120+
/// A mapping from the name of the lang item to its order and the form it must be of.
121+
pub static ITEM_REFS: SyncLazy<FxHashMap<Symbol, (usize, Target)>> = SyncLazy::new(|| {
122+
let mut item_refs = FxHashMap::default();
123+
$( item_refs.insert($name, (LangItem::$variant as usize, $target)); )*
124+
item_refs
125+
});
128126

129127
// End of the macro
130128
}

compiler/rustc_hir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![feature(const_fn)] // For the unsizing cast on `&[]`
77
#![feature(const_panic)]
88
#![feature(in_band_lifetimes)]
9+
#![feature(once_cell)]
910
#![feature(or_patterns)]
1011
#![recursion_limit = "256"]
1112

compiler/rustc_hir/src/weak_lang_items.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ use rustc_ast as ast;
77
use rustc_data_structures::fx::FxHashMap;
88
use rustc_span::symbol::{sym, Symbol};
99

10-
use lazy_static::lazy_static;
10+
use std::lazy::SyncLazy;
1111

1212
macro_rules! weak_lang_items {
1313
($($name:ident, $item:ident, $sym:ident;)*) => (
1414

15-
lazy_static! {
16-
pub static ref WEAK_ITEMS_REFS: FxHashMap<Symbol, LangItem> = {
17-
let mut map = FxHashMap::default();
18-
$(map.insert(sym::$name, LangItem::$item);)*
19-
map
20-
};
21-
}
15+
pub static WEAK_ITEMS_REFS: SyncLazy<FxHashMap<Symbol, LangItem>> = SyncLazy::new(|| {
16+
let mut map = FxHashMap::default();
17+
$(map.insert(sym::$name, LangItem::$item);)*
18+
map
19+
});
2220

2321
/// The `check_name` argument avoids the need for `librustc_hir` to depend on
2422
/// `librustc_session`.

compiler/rustc_interface/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ rustc_resolve = { path = "../rustc_resolve" }
4343
rustc_trait_selection = { path = "../rustc_trait_selection" }
4444
rustc_ty = { path = "../rustc_ty" }
4545
tempfile = "3.0.5"
46-
once_cell = "1"
4746

4847
[target.'cfg(windows)'.dependencies]
4948
winapi = { version = "0.3", features = ["libloaderapi"] }

compiler/rustc_interface/src/passes.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::interface::{Compiler, Result};
22
use crate::proc_macro_decls;
33
use crate::util;
44

5-
use once_cell::sync::Lazy;
65
use rustc_ast::mut_visit::MutVisitor;
76
use rustc_ast::{self as ast, visit};
87
use rustc_codegen_ssa::back::link::emit_metadata;
@@ -46,6 +45,7 @@ use std::any::Any;
4645
use std::cell::RefCell;
4746
use std::ffi::OsString;
4847
use std::io::{self, BufWriter, Write};
48+
use std::lazy::SyncLazy;
4949
use std::path::PathBuf;
5050
use std::rc::Rc;
5151
use std::{env, fs, iter, mem};
@@ -681,7 +681,7 @@ pub fn prepare_outputs(
681681
Ok(outputs)
682682
}
683683

684-
pub static DEFAULT_QUERY_PROVIDERS: Lazy<Providers> = Lazy::new(|| {
684+
pub static DEFAULT_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|| {
685685
let providers = &mut Providers::default();
686686
providers.analysis = analysis;
687687
proc_macro_decls::provide(providers);
@@ -704,7 +704,7 @@ pub static DEFAULT_QUERY_PROVIDERS: Lazy<Providers> = Lazy::new(|| {
704704
*providers
705705
});
706706

707-
pub static DEFAULT_EXTERN_QUERY_PROVIDERS: Lazy<Providers> = Lazy::new(|| {
707+
pub static DEFAULT_EXTERN_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|| {
708708
let mut extern_providers = *DEFAULT_QUERY_PROVIDERS;
709709
rustc_metadata::provide_extern(&mut extern_providers);
710710
rustc_codegen_ssa::provide_extern(&mut extern_providers);

compiler/rustc_interface/src/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_span::symbol::{sym, Symbol};
2525
use smallvec::SmallVec;
2626
use std::env;
2727
use std::io::{self, Write};
28+
use std::lazy::SyncOnceCell;
2829
use std::mem;
2930
use std::ops::DerefMut;
3031
use std::path::{Path, PathBuf};
@@ -243,8 +244,7 @@ pub fn get_codegen_backend(sess: &Session) -> Box<dyn CodegenBackend> {
243244
// loading, so we leave the code here. It is potentially useful for other tools
244245
// that want to invoke the rustc binary while linking to rustc as well.
245246
pub fn rustc_path<'a>() -> Option<&'a Path> {
246-
static RUSTC_PATH: once_cell::sync::OnceCell<Option<PathBuf>> =
247-
once_cell::sync::OnceCell::new();
247+
static RUSTC_PATH: SyncOnceCell<Option<PathBuf>> = SyncOnceCell::new();
248248

249249
const BIN_PATH: &str = env!("RUSTC_INSTALL_BINDIR");
250250

0 commit comments

Comments
 (0)