Skip to content

Commit 9be526e

Browse files
committed
rustc_metadata: Move has_global_allocator from session to cstore
1 parent 4c8105e commit 9be526e

File tree

8 files changed

+29
-28
lines changed

8 files changed

+29
-28
lines changed

src/librustc/middle/cstore.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ pub trait CrateStore {
237237
fn metadata_encoding_version(&self) -> &[u8];
238238
fn injected_panic_runtime(&self) -> Option<CrateNum>;
239239
fn allocator_kind(&self) -> Option<AllocatorKind>;
240+
fn has_global_allocator(&self) -> bool;
240241
}
241242

242243
pub type CrateStoreDyn = dyn CrateStore + sync::Sync;

src/librustc/session/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ pub struct Session {
133133
/// false positives about a job server in our environment.
134134
pub jobserver: Client,
135135

136-
/// Metadata about the allocators for the current crate being compiled.
137-
pub has_global_allocator: Once<bool>,
138-
139136
/// Cap lint level specified by a driver specifically.
140137
pub driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
141138

@@ -1180,7 +1177,6 @@ fn build_session_(
11801177
print_fuel_crate,
11811178
print_fuel,
11821179
jobserver: jobserver::client(),
1183-
has_global_allocator: Once::new(),
11841180
driver_lint_caps,
11851181
trait_methods_not_found: Lock::new(Default::default()),
11861182
confused_type_with_std_module: Lock::new(Default::default()),

src/librustc/ty/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,4 +3028,8 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
30283028
// We want to check if the panic handler was defined in this crate
30293029
tcx.lang_items().panic_impl().map_or(false, |did| did.is_local())
30303030
};
3031+
providers.has_global_allocator = |tcx, cnum| {
3032+
assert_eq!(cnum, LOCAL_CRATE);
3033+
tcx.cstore.has_global_allocator()
3034+
};
30313035
}

src/librustc_metadata/creader.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ use rustc_error_codes::*;
3737
pub struct CStore {
3838
metas: IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>,
3939
injected_panic_runtime: Option<CrateNum>,
40+
/// This crate needs an allocator and either provides it itself, or finds it in a dependency.
41+
/// If the above is true, then this field denotes the kind of the found allocator.
4042
allocator_kind: Option<AllocatorKind>,
43+
/// This crate has a `#[global_allocator]` item.
44+
has_global_allocator: bool,
4145
}
4246

4347
pub struct CrateLoader<'a> {
@@ -150,6 +154,10 @@ impl CStore {
150154
crate fn allocator_kind(&self) -> Option<AllocatorKind> {
151155
self.allocator_kind
152156
}
157+
158+
crate fn has_global_allocator(&self) -> bool {
159+
self.has_global_allocator
160+
}
153161
}
154162

155163
impl<'a> CrateLoader<'a> {
@@ -170,6 +178,7 @@ impl<'a> CrateLoader<'a> {
170178
metas: IndexVec::from_elem_n(None, 1),
171179
injected_panic_runtime: None,
172180
allocator_kind: None,
181+
has_global_allocator: false,
173182
}
174183
}
175184
}
@@ -562,7 +571,6 @@ impl<'a> CrateLoader<'a> {
562571
});
563572
if !any_non_rlib {
564573
info!("panic runtime injection skipped, only generating rlib");
565-
self.cstore.injected_panic_runtime = None;
566574
return
567575
}
568576

@@ -593,7 +601,6 @@ impl<'a> CrateLoader<'a> {
593601
// we just don't need one at all, then we're done here and there's
594602
// nothing else to do.
595603
if !needs_panic_runtime || runtime_found {
596-
self.cstore.injected_panic_runtime = None;
597604
return
598605
}
599606

@@ -753,7 +760,7 @@ impl<'a> CrateLoader<'a> {
753760
}
754761

755762
fn inject_allocator_crate(&mut self, krate: &ast::Crate) {
756-
let has_global_allocator = match &*global_allocator_spans(krate) {
763+
self.cstore.has_global_allocator = match &*global_allocator_spans(krate) {
757764
[span1, span2, ..] => {
758765
self.sess.struct_span_err(*span2, "cannot define multiple global allocators")
759766
.span_label(*span2, "cannot define a new global allocator")
@@ -763,7 +770,6 @@ impl<'a> CrateLoader<'a> {
763770
}
764771
spans => !spans.is_empty()
765772
};
766-
self.sess.has_global_allocator.set(has_global_allocator);
767773

768774
// Check to see if we actually need an allocator. This desire comes
769775
// about through the `#![needs_allocator]` attribute and is typically
@@ -774,7 +780,6 @@ impl<'a> CrateLoader<'a> {
774780
needs_allocator = needs_allocator || data.needs_allocator();
775781
});
776782
if !needs_allocator {
777-
self.cstore.allocator_kind = None;
778783
return
779784
}
780785

@@ -790,7 +795,6 @@ impl<'a> CrateLoader<'a> {
790795
}
791796
});
792797
if all_rlib {
793-
self.cstore.allocator_kind = None;
794798
return
795799
}
796800

@@ -801,8 +805,8 @@ impl<'a> CrateLoader<'a> {
801805
// First up we check for global allocators. Look at the crate graph here
802806
// and see what's a global allocator, including if we ourselves are a
803807
// global allocator.
804-
let mut global_allocator = if has_global_allocator {
805-
Some(None)
808+
let mut global_allocator = if self.cstore.has_global_allocator {
809+
Some(Symbol::intern("this crate"))
806810
} else {
807811
None
808812
};
@@ -811,19 +815,14 @@ impl<'a> CrateLoader<'a> {
811815
return
812816
}
813817
match global_allocator {
814-
Some(Some(other_crate)) => {
818+
Some(other_crate) => {
815819
self.sess.err(&format!("the `#[global_allocator]` in {} \
816-
conflicts with this global \
820+
conflicts with global \
817821
allocator in: {}",
818822
other_crate,
819823
data.name()));
820824
}
821-
Some(None) => {
822-
self.sess.err(&format!("the `#[global_allocator]` in this \
823-
crate conflicts with global \
824-
allocator in: {}", data.name()));
825-
}
826-
None => global_allocator = Some(Some(data.name())),
825+
None => global_allocator = Some(data.name()),
827826
}
828827
});
829828
if global_allocator.is_some() {
@@ -848,7 +847,7 @@ impl<'a> CrateLoader<'a> {
848847
add `#[global_allocator]` to a static item \
849848
that implements the GlobalAlloc trait.");
850849
}
851-
self.cstore.allocator_kind = Some(AllocatorKind::DefaultLib);
850+
self.cstore.allocator_kind = Some(AllocatorKind::Default);
852851
}
853852

854853
fn inject_dependency_if(&self,

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,4 +536,8 @@ impl CrateStore for CStore {
536536
fn allocator_kind(&self) -> Option<AllocatorKind> {
537537
self.allocator_kind()
538538
}
539+
540+
fn has_global_allocator(&self) -> bool {
541+
self.has_global_allocator()
542+
}
539543
}

src/librustc_metadata/rmeta/encoder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,6 @@ impl<'tcx> EncodeContext<'tcx> {
496496

497497
let attrs = tcx.hir().krate_attrs();
498498
let has_default_lib_allocator = attr::contains_name(&attrs, sym::default_lib_allocator);
499-
let has_global_allocator = *tcx.sess.has_global_allocator.get();
500499

501500
let root = self.lazy(CrateRoot {
502501
name: tcx.crate_name(LOCAL_CRATE),
@@ -506,7 +505,7 @@ impl<'tcx> EncodeContext<'tcx> {
506505
disambiguator: tcx.sess.local_crate_disambiguator(),
507506
panic_strategy: tcx.sess.panic_strategy(),
508507
edition: tcx.sess.edition(),
509-
has_global_allocator: has_global_allocator,
508+
has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE),
510509
has_panic_handler: tcx.has_panic_handler(LOCAL_CRATE),
511510
has_default_lib_allocator: has_default_lib_allocator,
512511
plugin_registrar_fn: tcx.plugin_registrar_fn(LOCAL_CRATE).map(|id| id.index),

src/libsyntax/expand/allocator.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ use syntax_pos::Span;
55
#[derive(Clone, Copy)]
66
pub enum AllocatorKind {
77
Global,
8-
DefaultLib,
9-
DefaultExe,
8+
Default,
109
}
1110

1211
impl AllocatorKind {
1312
pub fn fn_name(&self, base: &str) -> String {
1413
match *self {
1514
AllocatorKind::Global => format!("__rg_{}", base),
16-
AllocatorKind::DefaultLib => format!("__rdl_{}", base),
17-
AllocatorKind::DefaultExe => format!("__rde_{}", base),
15+
AllocatorKind::Default => format!("__rdl_{}", base),
1816
}
1917
}
2018
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: the `#[global_allocator]` in system_allocator conflicts with this global allocator in: system_allocator2
1+
error: the `#[global_allocator]` in system_allocator conflicts with global allocator in: system_allocator2
22

33
error: aborting due to previous error
44

0 commit comments

Comments
 (0)