From 7d33d1a84eadec1f1e54bf5e39575103c04b03bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Sun, 1 Apr 2018 08:24:46 +0200 Subject: [PATCH] Make Session.has_global_allocator thread-safe --- src/librustc/session/mod.rs | 4 ++-- src/librustc_metadata/creader.rs | 4 +--- src/librustc_metadata/encoder.rs | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 731e4feefa0f..1c11c52357d1 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -161,7 +161,7 @@ pub struct Session { pub jobserver_from_env: Option, /// Metadata about the allocators for the current crate being compiled - pub has_global_allocator: Cell, + pub has_global_allocator: Once, } pub struct PerfStats { @@ -1142,7 +1142,7 @@ pub fn build_session_( }); (*GLOBAL_JOBSERVER).clone() }, - has_global_allocator: Cell::new(false), + has_global_allocator: Once::new(), }; sess diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 86f495c5fac3..5b54994b9cea 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -812,9 +812,7 @@ impl<'a> CrateLoader<'a> { fn inject_allocator_crate(&mut self, krate: &ast::Crate) { let has_global_allocator = has_global_allocator(krate); - if has_global_allocator { - self.sess.has_global_allocator.set(true); - } + self.sess.has_global_allocator.set(has_global_allocator); // Check to see if we actually need an allocator. This desire comes // about through the `#![needs_allocator]` attribute and is typically diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index 1b208a512e2a..66071f242fb9 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -459,7 +459,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateTypeProcMacro); let has_default_lib_allocator = attr::contains_name(tcx.hir.krate_attrs(), "default_lib_allocator"); - let has_global_allocator = tcx.sess.has_global_allocator.get(); + let has_global_allocator = *tcx.sess.has_global_allocator.get(); let root = self.lazy(&CrateRoot { name: tcx.crate_name(LOCAL_CRATE), extra_filename: tcx.sess.opts.cg.extra_filename.clone(),