Skip to content

Commit c282c1c

Browse files
Use OnceCell instead of Once
1 parent 9f82785 commit c282c1c

File tree

39 files changed

+118
-102
lines changed

39 files changed

+118
-102
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,6 +2327,9 @@ name = "once_cell"
23272327
version = "1.1.0"
23282328
source = "registry+https://github.com/rust-lang/crates.io-index"
23292329
checksum = "d6a04cb71e910d0034815600180f62a95bf6e67942d7ab52a166a68c7d7e9cd0"
2330+
dependencies = [
2331+
"parking_lot 0.9.0",
2332+
]
23302333

23312334
[[package]]
23322335
name = "opaque-debug"

src/librustc_ast_lowering/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ pub fn lower_crate<'a, 'hir>(
269269
let _prof_timer = sess.prof.verbose_generic_activity("hir_lowering");
270270

271271
LoweringContext {
272-
crate_root: sess.parse_sess.injected_crate_name.try_get().copied(),
272+
crate_root: sess.parse_sess.injected_crate_name.get().copied(),
273273
sess,
274274
resolver,
275275
nt_to_tokenstream,

src/librustc_codegen_llvm/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub unsafe fn create_module(
174174
llvm::LLVMRustSetModulePICLevel(llmod);
175175
// PIE is potentially more effective than PIC, but can only be used in executables.
176176
// If all our outputs are executables, then we can relax PIC to PIE.
177-
if sess.crate_types.get().iter().all(|ty| *ty == CrateType::Executable) {
177+
if sess.crate_types().iter().all(|ty| *ty == CrateType::Executable) {
178178
llvm::LLVMRustSetModulePIELevel(llmod);
179179
}
180180
}

src/librustc_codegen_ssa/back/link.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
5353
) {
5454
let _timer = sess.timer("link_binary");
5555
let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
56-
for &crate_type in sess.crate_types.borrow().iter() {
56+
for &crate_type in sess.crate_types().iter() {
5757
// Ignore executable crates if we have -Z no-codegen, as they will error.
5858
if (sess.opts.debugging_opts.no_codegen || !sess.opts.output_types.should_codegen())
5959
&& !output_metadata
@@ -875,11 +875,8 @@ fn preserve_objects_for_their_debuginfo(sess: &Session) -> bool {
875875

876876
// If we're only producing artifacts that are archives, no need to preserve
877877
// the objects as they're losslessly contained inside the archives.
878-
let output_linked = sess
879-
.crate_types
880-
.borrow()
881-
.iter()
882-
.any(|&x| x != CrateType::Rlib && x != CrateType::Staticlib);
878+
let output_linked =
879+
sess.crate_types().iter().any(|&x| x != CrateType::Rlib && x != CrateType::Staticlib);
883880
if !output_linked {
884881
return false;
885882
}

src/librustc_codegen_ssa/back/linker.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ impl LinkerInfo {
4444
LinkerInfo {
4545
exports: tcx
4646
.sess
47-
.crate_types
48-
.borrow()
47+
.crate_types()
4948
.iter()
5049
.map(|&c| (c, exported_symbols(tcx, c)))
5150
.collect(),

src/librustc_codegen_ssa/back/symbol_export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_middle::ty::{SymbolName, TyCtxt};
1818
use rustc_session::config::{CrateType, Sanitizer};
1919

2020
pub fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
21-
crates_export_threshold(&tcx.sess.crate_types.borrow())
21+
crates_export_threshold(&tcx.sess.crate_types())
2222
}
2323

2424
fn crate_export_threshold(crate_type: CrateType) -> SymbolExportLevel {
@@ -212,7 +212,7 @@ fn exported_symbols_provider_local(
212212
}));
213213
}
214214

215-
if tcx.sess.crate_types.borrow().contains(&CrateType::Dylib) {
215+
if tcx.sess.crate_types().contains(&CrateType::Dylib) {
216216
let symbol_name = metadata_symbol_name(tcx);
217217
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(&symbol_name));
218218

src/librustc_codegen_ssa/back/write.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub struct CompiledModules {
382382

383383
fn need_bitcode_in_object(sess: &Session) -> bool {
384384
let requested_for_rlib = sess.opts.cg.embed_bitcode
385-
&& sess.crate_types.borrow().contains(&CrateType::Rlib)
385+
&& sess.crate_types().contains(&CrateType::Rlib)
386386
&& sess.opts.output_types.contains_key(&OutputType::Exe);
387387
let forced_by_target = sess.target.target.options.forces_embed_bitcode;
388388
requested_for_rlib || forced_by_target
@@ -991,7 +991,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
991991
};
992992
let cgcx = CodegenContext::<B> {
993993
backend: backend.clone(),
994-
crate_types: sess.crate_types.borrow().clone(),
994+
crate_types: sess.crate_types().to_vec(),
995995
each_linked_rlib_for_lto,
996996
lto: sess.lto(),
997997
no_landing_pads: sess.panic_strategy() == PanicStrategy::Abort,
@@ -1812,7 +1812,7 @@ fn msvc_imps_needed(tcx: TyCtxt<'_>) -> bool {
18121812
);
18131813

18141814
tcx.sess.target.target.options.is_like_msvc &&
1815-
tcx.sess.crate_types.borrow().iter().any(|ct| *ct == CrateType::Rlib) &&
1815+
tcx.sess.crate_types().iter().any(|ct| *ct == CrateType::Rlib) &&
18161816
// ThinLTO can't handle this workaround in all cases, so we don't
18171817
// emit the `__imp_` symbols. Instead we make them unnecessary by disallowing
18181818
// dynamic linking when linker plugin LTO is enabled.

src/librustc_codegen_ssa/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguR
948948
match compute_per_cgu_lto_type(
949949
&tcx.sess.lto(),
950950
&tcx.sess.opts,
951-
&tcx.sess.crate_types.borrow(),
951+
&tcx.sess.crate_types(),
952952
ModuleKind::Regular,
953953
) {
954954
ComputedLtoType::No => CguReuse::PostLto,

src/librustc_driver/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ impl RustcDefaultCalls {
586586
if let Input::File(file) = compiler.input() {
587587
// FIXME: #![crate_type] and #![crate_name] support not implemented yet
588588
let attrs = vec![];
589-
sess.crate_types.set(collect_crate_types(sess, &attrs));
589+
sess.init_crate_types(collect_crate_types(sess, &attrs));
590590
let outputs = compiler.build_output_filenames(&sess, &attrs);
591591
let rlink_data = fs::read_to_string(file).unwrap_or_else(|err| {
592592
sess.fatal(&format!("failed to read rlink file: {}", err));

src/librustc_driver/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ pub fn print_after_parsing(
396396
annotation.pp_ann(),
397397
false,
398398
parse.edition,
399-
parse.injected_crate_name.try_get().is_some(),
399+
parse.injected_crate_name.get().is_some(),
400400
)
401401
})
402402
} else {
@@ -438,7 +438,7 @@ pub fn print_after_hir_lowering<'tcx>(
438438
annotation.pp_ann(),
439439
true,
440440
parse.edition,
441-
parse.injected_crate_name.try_get().is_some(),
441+
parse.injected_crate_name.get().is_some(),
442442
)
443443
})
444444
}

src/librustc_interface/passes.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::mut_visit::MutVisitor;
77
use rustc_ast::{self, ast, visit};
88
use rustc_codegen_ssa::back::link::emit_metadata;
99
use rustc_codegen_ssa::traits::CodegenBackend;
10-
use rustc_data_structures::sync::{par_iter, Lrc, Once, ParallelIterator, WorkerLocal};
10+
use rustc_data_structures::sync::{par_iter, Lrc, OnceCell, ParallelIterator, WorkerLocal};
1111
use rustc_data_structures::{box_region_allow_access, declare_box_region_type, parallel};
1212
use rustc_errors::{ErrorReported, PResult};
1313
use rustc_expand::base::ExtCtxt;
@@ -169,10 +169,10 @@ pub fn register_plugins<'a>(
169169
sess.init_features(features);
170170

171171
let crate_types = util::collect_crate_types(sess, &krate.attrs);
172-
sess.crate_types.set(crate_types);
172+
sess.init_crate_types(crate_types);
173173

174174
let disambiguator = util::compute_crate_disambiguator(sess);
175-
sess.crate_disambiguator.set(disambiguator);
175+
sess.crate_disambiguator.set(disambiguator).expect("not yet initialized");
176176
rustc_incremental::prepare_session_directory(sess, &crate_name, disambiguator);
177177

178178
if sess.opts.incremental.is_some() {
@@ -244,7 +244,7 @@ fn configure_and_expand_inner<'a>(
244244
alt_std_name,
245245
);
246246
if let Some(name) = name {
247-
sess.parse_sess.injected_crate_name.set(name);
247+
sess.parse_sess.injected_crate_name.set(name).expect("not yet initialized");
248248
}
249249
krate
250250
});
@@ -288,7 +288,7 @@ fn configure_and_expand_inner<'a>(
288288
let features = sess.features_untracked();
289289
let cfg = rustc_expand::expand::ExpansionConfig {
290290
features: Some(&features),
291-
recursion_limit: *sess.recursion_limit.get(),
291+
recursion_limit: sess.recursion_limit(),
292292
trace_mac: sess.opts.debugging_opts.trace_macros,
293293
should_test: sess.opts.test,
294294
..rustc_expand::expand::ExpansionConfig::default(crate_name.to_string())
@@ -358,7 +358,7 @@ fn configure_and_expand_inner<'a>(
358358
rustc_ast_passes::ast_validation::check_crate(sess, &krate, &mut resolver.lint_buffer())
359359
});
360360

361-
let crate_types = sess.crate_types.borrow();
361+
let crate_types = sess.crate_types();
362362
let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
363363

364364
// For backwards compatibility, we don't try to run proc macro injection
@@ -488,7 +488,7 @@ fn generated_output_paths(
488488
// If the filename has been overridden using `-o`, it will not be modified
489489
// by appending `.rlib`, `.exe`, etc., so we can skip this transformation.
490490
OutputType::Exe if !exact_name => {
491-
for crate_type in sess.crate_types.borrow().iter() {
491+
for crate_type in sess.crate_types().iter() {
492492
let p = filename_for_input(sess, *crate_type, crate_name, outputs);
493493
out_filenames.push(p);
494494
}
@@ -721,7 +721,7 @@ pub fn create_global_ctxt<'tcx>(
721721
mut resolver_outputs: ResolverOutputs,
722722
outputs: OutputFilenames,
723723
crate_name: &str,
724-
global_ctxt: &'tcx Once<GlobalCtxt<'tcx>>,
724+
global_ctxt: &'tcx OnceCell<GlobalCtxt<'tcx>>,
725725
arena: &'tcx WorkerLocal<Arena<'tcx>>,
726726
) -> QueryContext<'tcx> {
727727
let sess = &compiler.session();
@@ -743,7 +743,7 @@ pub fn create_global_ctxt<'tcx>(
743743
}
744744

745745
let gcx = sess.time("setup_global_ctxt", || {
746-
global_ctxt.init_locking(|| {
746+
global_ctxt.get_or_init(|| {
747747
TyCtxt::create_global_ctxt(
748748
sess,
749749
lint_store,
@@ -905,8 +905,7 @@ fn encode_and_write_metadata(
905905

906906
let metadata_kind = tcx
907907
.sess
908-
.crate_types
909-
.borrow()
908+
.crate_types()
910909
.iter()
911910
.map(|ty| match *ty {
912911
CrateType::Executable | CrateType::Staticlib | CrateType::Cdylib => MetadataKind::None,

src/librustc_interface/queries.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::passes::{self, BoxedResolver, QueryContext};
33

44
use rustc_ast::{self, ast};
55
use rustc_codegen_ssa::traits::CodegenBackend;
6-
use rustc_data_structures::sync::{Lrc, Once, WorkerLocal};
6+
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
77
use rustc_errors::ErrorReported;
88
use rustc_hir::def_id::LOCAL_CRATE;
99
use rustc_hir::Crate;
@@ -65,7 +65,7 @@ impl<T> Default for Query<T> {
6565

6666
pub struct Queries<'tcx> {
6767
compiler: &'tcx Compiler,
68-
gcx: Once<GlobalCtxt<'tcx>>,
68+
gcx: OnceCell<GlobalCtxt<'tcx>>,
6969

7070
arena: WorkerLocal<Arena<'tcx>>,
7171
hir_arena: WorkerLocal<rustc_ast_lowering::Arena<'tcx>>,
@@ -86,7 +86,7 @@ impl<'tcx> Queries<'tcx> {
8686
pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
8787
Queries {
8888
compiler,
89-
gcx: Once::new(),
89+
gcx: OnceCell::new(),
9090
arena: WorkerLocal::new(|_| Arena::default()),
9191
hir_arena: WorkerLocal::new(|_| rustc_ast_lowering::Arena::default()),
9292
dep_graph_future: Default::default(),

src/librustc_interface/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ pub(crate) fn compute_crate_disambiguator(session: &Session) -> CrateDisambiguat
406406

407407
// Also incorporate crate type, so that we don't get symbol conflicts when
408408
// linking against a library of the same name, if this is an executable.
409-
let is_exe = session.crate_types.borrow().contains(&CrateType::Executable);
409+
let is_exe = session.crate_types().contains(&CrateType::Executable);
410410
hasher.write(if is_exe { b"exe" } else { b"lib" });
411411

412412
CrateDisambiguator::from(hasher.finish::<Fingerprint>())

src/librustc_metadata/creader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ impl<'a> CrateLoader<'a> {
615615
fn inject_panic_runtime(&mut self, krate: &ast::Crate) {
616616
// If we're only compiling an rlib, then there's no need to select a
617617
// panic runtime, so we just skip this section entirely.
618-
let any_non_rlib = self.sess.crate_types.borrow().iter().any(|ct| *ct != CrateType::Rlib);
618+
let any_non_rlib = self.sess.crate_types().iter().any(|ct| *ct != CrateType::Rlib);
619619
if !any_non_rlib {
620620
info!("panic runtime injection skipped, only generating rlib");
621621
return;
@@ -734,7 +734,7 @@ impl<'a> CrateLoader<'a> {
734734
// At this point we've determined that we need an allocator. Let's see
735735
// if our compilation session actually needs an allocator based on what
736736
// we're emitting.
737-
let all_rlib = self.sess.crate_types.borrow().iter().all(|ct| match *ct {
737+
let all_rlib = self.sess.crate_types().iter().all(|ct| match *ct {
738738
CrateType::Rlib => true,
739739
_ => false,
740740
});

src/librustc_metadata/dependency_format.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ use rustc_target::spec::PanicStrategy;
6464

6565
crate fn calculate(tcx: TyCtxt<'_>) -> Dependencies {
6666
tcx.sess
67-
.crate_types
68-
.borrow()
67+
.crate_types()
6968
.iter()
7069
.map(|&ty| {
7170
let linkage = calculate_type(tcx, ty);

src/librustc_metadata/locator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ impl<'a> CrateLocator<'a> {
670670

671671
// The all loop is because `--crate-type=rlib --crate-type=rlib` is
672672
// legal and produces both inside this type.
673-
let is_rlib = self.sess.crate_types.borrow().iter().all(|c| *c == CrateType::Rlib);
673+
let is_rlib = self.sess.crate_types().iter().all(|c| *c == CrateType::Rlib);
674674
let needs_object_code = self.sess.opts.output_types.should_codegen();
675675
// If we're producing an rlib, then we don't need object code.
676676
// Or, if we're not producing object code, then we don't need it either

src/librustc_metadata/rmeta/decoder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_data_structures::captures::Captures;
1010
use rustc_data_structures::fingerprint::Fingerprint;
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_data_structures::svh::Svh;
13-
use rustc_data_structures::sync::{AtomicCell, Lock, LockGuard, Lrc, Once};
13+
use rustc_data_structures::sync::{AtomicCell, Lock, LockGuard, Lrc, OnceCell};
1414
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
1515
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive};
1616
use rustc_hir as hir;
@@ -79,7 +79,7 @@ crate struct CrateMetadata {
7979
/// Proc macro descriptions for this crate, if it's a proc macro crate.
8080
raw_proc_macros: Option<&'static [ProcMacro]>,
8181
/// Source maps for code from the crate.
82-
source_map_import_info: Once<Vec<ImportedSourceFile>>,
82+
source_map_import_info: OnceCell<Vec<ImportedSourceFile>>,
8383
/// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
8484
alloc_decoding_state: AllocDecodingState,
8585
/// The `DepNodeIndex` of the `DepNode` representing this upstream crate.
@@ -1486,7 +1486,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
14861486
}
14871487
};
14881488

1489-
self.cdata.source_map_import_info.init_locking(|| {
1489+
self.cdata.source_map_import_info.get_or_init(|| {
14901490
let external_source_map = self.root.source_map.decode(self);
14911491

14921492
external_source_map
@@ -1600,7 +1600,7 @@ impl CrateMetadata {
16001600
def_path_table,
16011601
trait_impls,
16021602
raw_proc_macros,
1603-
source_map_import_info: Once::new(),
1603+
source_map_import_info: OnceCell::new(),
16041604
alloc_decoding_state,
16051605
dep_node_index: AtomicCell::new(DepNodeIndex::INVALID),
16061606
cnum,

src/librustc_metadata/rmeta/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<'tcx> EncodeContext<'tcx> {
416416
}
417417

418418
fn encode_crate_root(&mut self) -> Lazy<CrateRoot<'tcx>> {
419-
let is_proc_macro = self.tcx.sess.crate_types.borrow().contains(&CrateType::ProcMacro);
419+
let is_proc_macro = self.tcx.sess.crate_types().contains(&CrateType::ProcMacro);
420420

421421
let mut i = self.position();
422422

@@ -1364,7 +1364,7 @@ impl EncodeContext<'tcx> {
13641364
}
13651365

13661366
fn encode_proc_macros(&mut self) -> Option<Lazy<[DefIndex]>> {
1367-
let is_proc_macro = self.tcx.sess.crate_types.borrow().contains(&CrateType::ProcMacro);
1367+
let is_proc_macro = self.tcx.sess.crate_types().contains(&CrateType::ProcMacro);
13681368
if is_proc_macro {
13691369
let tcx = self.tcx;
13701370
Some(self.lazy(tcx.hir().krate().proc_macros.iter().map(|p| p.owner.local_def_index)))

src/librustc_middle/middle/limits.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
use crate::bug;
99
use rustc_ast::ast;
10-
use rustc_data_structures::sync::Once;
10+
use rustc_data_structures::sync::OnceCell;
1111
use rustc_session::Session;
1212
use rustc_span::symbol::{sym, Symbol};
1313

@@ -22,7 +22,7 @@ pub fn update_limits(sess: &Session, krate: &ast::Crate) {
2222
fn update_limit(
2323
sess: &Session,
2424
krate: &ast::Crate,
25-
limit: &Once<usize>,
25+
limit: &OnceCell<usize>,
2626
name: Symbol,
2727
default: usize,
2828
) {
@@ -34,7 +34,7 @@ fn update_limit(
3434
if let Some(s) = attr.value_str() {
3535
match s.as_str().parse() {
3636
Ok(n) => {
37-
limit.set(n);
37+
limit.set(n).unwrap();
3838
return;
3939
}
4040
Err(e) => {
@@ -62,5 +62,5 @@ fn update_limit(
6262
}
6363
}
6464
}
65-
limit.set(default);
65+
limit.set(default).unwrap();
6666
}

src/librustc_middle/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,7 @@ impl<'tcx> TyCtxt<'tcx> {
13801380
pub fn local_crate_exports_generics(self) -> bool {
13811381
debug_assert!(self.sess.opts.share_generics());
13821382

1383-
self.sess.crate_types.borrow().iter().any(|crate_type| {
1383+
self.sess.crate_types().iter().any(|crate_type| {
13841384
match crate_type {
13851385
CrateType::Executable
13861386
| CrateType::Staticlib

0 commit comments

Comments
 (0)