Skip to content

Commit dacf9ba

Browse files
committed
Make Session.injected_allocator and Session.allocator_kind thread-safe
1 parent 66488a5 commit dacf9ba

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/librustc/session/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ pub struct Session {
118118
/// The metadata::creader module may inject an allocator/panic_runtime
119119
/// dependency if it didn't already find one, and this tracks what was
120120
/// injected.
121-
pub injected_allocator: Cell<Option<CrateNum>>,
122-
pub allocator_kind: Cell<Option<AllocatorKind>>,
121+
pub injected_allocator: Once<Option<CrateNum>>,
122+
pub allocator_kind: Once<Option<AllocatorKind>>,
123123
pub injected_panic_runtime: Cell<Option<CrateNum>>,
124124

125125
/// Map from imported macro spans (which consist of
@@ -1105,8 +1105,8 @@ pub fn build_session_(
11051105
const_eval_stack_frame_limit: 100,
11061106
const_eval_step_limit: 1_000_000,
11071107
next_node_id: OneThread::new(Cell::new(NodeId::new(1))),
1108-
injected_allocator: Cell::new(None),
1109-
allocator_kind: Cell::new(None),
1108+
injected_allocator: Once::new(),
1109+
allocator_kind: Once::new(),
11101110
injected_panic_runtime: Cell::new(None),
11111111
imported_macro_spans: OneThread::new(RefCell::new(HashMap::new())),
11121112
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),

src/librustc_metadata/creader.rs

+8
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,8 @@ impl<'a> CrateLoader<'a> {
823823
needs_allocator = needs_allocator || data.needs_allocator(self.sess);
824824
});
825825
if !needs_allocator {
826+
self.sess.injected_allocator.set(None);
827+
self.sess.allocator_kind.set(None);
826828
return
827829
}
828830

@@ -842,6 +844,8 @@ impl<'a> CrateLoader<'a> {
842844
}
843845
}
844846
if !need_lib_alloc && !need_exe_alloc {
847+
self.sess.injected_allocator.set(None);
848+
self.sess.allocator_kind.set(None);
845849
return
846850
}
847851

@@ -879,6 +883,7 @@ impl<'a> CrateLoader<'a> {
879883
});
880884
if global_allocator.is_some() {
881885
self.sess.allocator_kind.set(Some(AllocatorKind::Global));
886+
self.sess.injected_allocator.set(None);
882887
return
883888
}
884889

@@ -922,6 +927,9 @@ impl<'a> CrateLoader<'a> {
922927
};
923928

924929
let allocation_crate_data = exe_allocation_crate_data.or_else(|| {
930+
// No allocator was injected
931+
self.sess.injected_allocator.set(None);
932+
925933
if attr::contains_name(&krate.attrs, "default_lib_allocator") {
926934
// Prefer self as the allocator if there's a collision
927935
return None;

src/librustc_trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
795795
codegen_units.len());
796796

797797
// Translate an allocator shim, if any
798-
let allocator_module = if let Some(kind) = tcx.sess.allocator_kind.get() {
798+
let allocator_module = if let Some(kind) = *tcx.sess.allocator_kind.get() {
799799
unsafe {
800800
let llmod_id = "allocator";
801801
let (llcx, llmod) =

0 commit comments

Comments
 (0)