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
This commit is contained in:
bors 2020-09-02 06:46:21 +00:00
commit da897dfb6d
15 changed files with 56 additions and 71 deletions

View File

@ -3417,7 +3417,6 @@ dependencies = [
"ena",
"indexmap",
"jobserver",
"lazy_static",
"libc",
"measureme",
"parking_lot 0.10.2",
@ -3440,7 +3439,6 @@ dependencies = [
name = "rustc_driver"
version = "0.0.0"
dependencies = [
"lazy_static",
"libc",
"rustc_ast",
"rustc_ast_pretty",
@ -3514,7 +3512,6 @@ dependencies = [
name = "rustc_feature"
version = "0.0.0"
dependencies = [
"lazy_static",
"rustc_data_structures",
"rustc_span",
]
@ -3531,7 +3528,6 @@ version = "0.0.0"
name = "rustc_hir"
version = "0.0.0"
dependencies = [
"lazy_static",
"rustc_ast",
"rustc_data_structures",
"rustc_index",
@ -3606,7 +3602,6 @@ name = "rustc_interface"
version = "0.0.0"
dependencies = [
"libc",
"once_cell",
"rustc-rayon",
"rustc_ast",
"rustc_ast_lowering",

View File

@ -12,7 +12,6 @@ ena = "0.14"
indexmap = "1.5.1"
tracing = "0.1"
jobserver_crate = { version = "0.1.13", package = "jobserver" }
lazy_static = "1"
rustc_serialize = { path = "../rustc_serialize" }
rustc_macros = { path = "../rustc_macros" }
rustc_graphviz = { path = "../rustc_graphviz" }

View File

@ -1,33 +1,31 @@
pub use jobserver_crate::Client;
use lazy_static::lazy_static;
use std::lazy::SyncLazy;
lazy_static! {
// We can only call `from_env` once per process
// We can only call `from_env` once per process
// Note that this is unsafe because it may misinterpret file descriptors
// on Unix as jobserver file descriptors. We hopefully execute this near
// the beginning of the process though to ensure we don't get false
// positives, or in other words we try to execute this before we open
// any file descriptors ourselves.
//
// Pick a "reasonable maximum" if we don't otherwise have
// a jobserver in our environment, capping out at 32 so we
// don't take everything down by hogging the process run queue.
// The fixed number is used to have deterministic compilation
// across machines.
//
// Also note that we stick this in a global because there could be
// multiple rustc instances in this process, and the jobserver is
// per-process.
static ref GLOBAL_CLIENT: Client = unsafe {
Client::from_env().unwrap_or_else(|| {
let client = Client::new(32).expect("failed to create jobserver");
// Acquire a token for the main thread which we can release later
client.acquire_raw().ok();
client
})
};
}
// Note that this is unsafe because it may misinterpret file descriptors
// on Unix as jobserver file descriptors. We hopefully execute this near
// the beginning of the process though to ensure we don't get false
// positives, or in other words we try to execute this before we open
// any file descriptors ourselves.
//
// Pick a "reasonable maximum" if we don't otherwise have
// a jobserver in our environment, capping out at 32 so we
// don't take everything down by hogging the process run queue.
// The fixed number is used to have deterministic compilation
// across machines.
//
// Also note that we stick this in a global because there could be
// multiple rustc instances in this process, and the jobserver is
// per-process.
static GLOBAL_CLIENT: SyncLazy<Client> = SyncLazy::new(|| unsafe {
Client::from_env().unwrap_or_else(|| {
let client = Client::new(32).expect("failed to create jobserver");
// Acquire a token for the main thread which we can release later
client.acquire_raw().ok();
client
})
});
pub fn client() -> Client {
GLOBAL_CLIENT.clone()

View File

@ -8,7 +8,6 @@ edition = "2018"
crate-type = ["dylib"]
[dependencies]
lazy_static = "1.0"
libc = "0.2"
tracing = { version = "0.1.18", features = ["release_max_level_info"] }
tracing-subscriber = { version = "0.2.10", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] }

View File

@ -11,8 +11,6 @@
#[macro_use]
extern crate tracing;
#[macro_use]
extern crate lazy_static;
pub extern crate rustc_plugin_impl as plugin;
@ -49,6 +47,7 @@ use std::env;
use std::ffi::OsString;
use std::fs;
use std::io::{self, Read, Write};
use std::lazy::SyncLazy;
use std::mem;
use std::panic::{self, catch_unwind};
use std::path::PathBuf;
@ -1142,13 +1141,12 @@ pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
}
}
lazy_static! {
static ref DEFAULT_HOOK: Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static> = {
static DEFAULT_HOOK: SyncLazy<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
SyncLazy::new(|| {
let hook = panic::take_hook();
panic::set_hook(Box::new(|info| report_ice(info, BUG_REPORT_URL)));
hook
};
}
});
/// Prints the ICE message, including backtrace and query stack.
///
@ -1223,7 +1221,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
///
/// A custom rustc driver can skip calling this to set up a custom ICE hook.
pub fn install_ice_hook() {
lazy_static::initialize(&DEFAULT_HOOK);
SyncLazy::force(&DEFAULT_HOOK);
}
/// This allows tools to enable rust logging without having to magically match rustc's

View File

@ -9,5 +9,4 @@ doctest = false
[dependencies]
rustc_data_structures = { path = "../rustc_data_structures" }
lazy_static = "1.0.0"
rustc_span = { path = "../rustc_span" }

View File

@ -5,10 +5,11 @@ use AttributeType::*;
use crate::{Features, Stability};
use lazy_static::lazy_static;
use rustc_data_structures::fx::FxHashMap;
use rustc_span::symbol::{sym, Symbol};
use std::lazy::SyncLazy;
type GateFn = fn(&Features) -> bool;
macro_rules! cfg_fn {
@ -589,8 +590,8 @@ pub fn is_builtin_attr_name(name: Symbol) -> bool {
BUILTIN_ATTRIBUTE_MAP.get(&name).is_some()
}
lazy_static! {
pub static ref BUILTIN_ATTRIBUTE_MAP: FxHashMap<Symbol, &'static BuiltinAttribute> = {
pub static BUILTIN_ATTRIBUTE_MAP: SyncLazy<FxHashMap<Symbol, &'static BuiltinAttribute>> =
SyncLazy::new(|| {
let mut map = FxHashMap::default();
for attr in BUILTIN_ATTRIBUTES.iter() {
if map.insert(attr.0, attr).is_some() {
@ -598,5 +599,4 @@ lazy_static! {
}
}
map
};
}
});

View File

@ -11,6 +11,8 @@
//! even if it is stabilized or removed, *do not remove it*. Instead, move the
//! symbol to the `accepted` or `removed` modules respectively.
#![feature(once_cell)]
mod accepted;
mod active;
mod builtin_attrs;

View File

@ -15,6 +15,5 @@ rustc_index = { path = "../rustc_index" }
rustc_span = { path = "../rustc_span" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_ast = { path = "../rustc_ast" }
lazy_static = "1"
tracing = "0.1"
smallvec = { version = "1.0", features = ["union", "may_dangle"] }

View File

@ -17,7 +17,7 @@ use rustc_macros::HashStable_Generic;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;
use lazy_static::lazy_static;
use std::lazy::SyncLazy;
pub enum LangItemGroup {
Op,
@ -117,14 +117,12 @@ macro_rules! language_item_table {
)*
}
lazy_static! {
/// A mapping from the name of the lang item to its order and the form it must be of.
pub static ref ITEM_REFS: FxHashMap<Symbol, (usize, Target)> = {
let mut item_refs = FxHashMap::default();
$( item_refs.insert($name, (LangItem::$variant as usize, $target)); )*
item_refs
};
}
/// A mapping from the name of the lang item to its order and the form it must be of.
pub static ITEM_REFS: SyncLazy<FxHashMap<Symbol, (usize, Target)>> = SyncLazy::new(|| {
let mut item_refs = FxHashMap::default();
$( item_refs.insert($name, (LangItem::$variant as usize, $target)); )*
item_refs
});
// End of the macro
}

View File

@ -6,6 +6,7 @@
#![feature(const_fn)] // For the unsizing cast on `&[]`
#![feature(const_panic)]
#![feature(in_band_lifetimes)]
#![feature(once_cell)]
#![feature(or_patterns)]
#![recursion_limit = "256"]

View File

@ -7,18 +7,16 @@ use rustc_ast as ast;
use rustc_data_structures::fx::FxHashMap;
use rustc_span::symbol::{sym, Symbol};
use lazy_static::lazy_static;
use std::lazy::SyncLazy;
macro_rules! weak_lang_items {
($($name:ident, $item:ident, $sym:ident;)*) => (
lazy_static! {
pub static ref WEAK_ITEMS_REFS: FxHashMap<Symbol, LangItem> = {
let mut map = FxHashMap::default();
$(map.insert(sym::$name, LangItem::$item);)*
map
};
}
pub static WEAK_ITEMS_REFS: SyncLazy<FxHashMap<Symbol, LangItem>> = SyncLazy::new(|| {
let mut map = FxHashMap::default();
$(map.insert(sym::$name, LangItem::$item);)*
map
});
/// The `check_name` argument avoids the need for `librustc_hir` to depend on
/// `librustc_session`.

View File

@ -43,7 +43,6 @@ rustc_resolve = { path = "../rustc_resolve" }
rustc_trait_selection = { path = "../rustc_trait_selection" }
rustc_ty = { path = "../rustc_ty" }
tempfile = "3.0.5"
once_cell = "1"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["libloaderapi"] }

View File

@ -2,7 +2,6 @@ use crate::interface::{Compiler, Result};
use crate::proc_macro_decls;
use crate::util;
use once_cell::sync::Lazy;
use rustc_ast::mut_visit::MutVisitor;
use rustc_ast::{self as ast, visit};
use rustc_codegen_ssa::back::link::emit_metadata;
@ -46,6 +45,7 @@ use std::any::Any;
use std::cell::RefCell;
use std::ffi::OsString;
use std::io::{self, BufWriter, Write};
use std::lazy::SyncLazy;
use std::path::PathBuf;
use std::rc::Rc;
use std::{env, fs, iter, mem};
@ -681,7 +681,7 @@ pub fn prepare_outputs(
Ok(outputs)
}
pub static DEFAULT_QUERY_PROVIDERS: Lazy<Providers> = Lazy::new(|| {
pub static DEFAULT_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|| {
let providers = &mut Providers::default();
providers.analysis = analysis;
proc_macro_decls::provide(providers);
@ -704,7 +704,7 @@ pub static DEFAULT_QUERY_PROVIDERS: Lazy<Providers> = Lazy::new(|| {
*providers
});
pub static DEFAULT_EXTERN_QUERY_PROVIDERS: Lazy<Providers> = Lazy::new(|| {
pub static DEFAULT_EXTERN_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|| {
let mut extern_providers = *DEFAULT_QUERY_PROVIDERS;
rustc_metadata::provide_extern(&mut extern_providers);
rustc_codegen_ssa::provide_extern(&mut extern_providers);

View File

@ -25,6 +25,7 @@ use rustc_span::symbol::{sym, Symbol};
use smallvec::SmallVec;
use std::env;
use std::io::{self, Write};
use std::lazy::SyncOnceCell;
use std::mem;
use std::ops::DerefMut;
use std::path::{Path, PathBuf};
@ -243,8 +244,7 @@ pub fn get_codegen_backend(sess: &Session) -> Box<dyn CodegenBackend> {
// loading, so we leave the code here. It is potentially useful for other tools
// that want to invoke the rustc binary while linking to rustc as well.
pub fn rustc_path<'a>() -> Option<&'a Path> {
static RUSTC_PATH: once_cell::sync::OnceCell<Option<PathBuf>> =
once_cell::sync::OnceCell::new();
static RUSTC_PATH: SyncOnceCell<Option<PathBuf>> = SyncOnceCell::new();
const BIN_PATH: &str = env!("RUSTC_INSTALL_BINDIR");