Skip to content

Commit c305a1a

Browse files
authored
Rollup merge of rust-lang#155071 - Fayti1703:alloc/no-thread-local, r=Kivooeo
Deny `#[global_allocator]` + `#[thread_local]` This forbids using `#[thread_local]` on `static` items that are also `#[global_allocator]`s. Fixes rust-lang#85517.
2 parents 5d8f9c8 + b537c45 commit c305a1a

4 files changed

Lines changed: 39 additions & 0 deletions

File tree

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ pub(crate) struct AllocMustStatics {
158158
pub(crate) span: Span,
159159
}
160160

161+
#[derive(Diagnostic)]
162+
#[diag("allocators cannot be `#[thread_local]`")]
163+
pub(crate) struct AllocCannotThreadLocal {
164+
#[primary_span]
165+
pub(crate) span: Span,
166+
#[label("marked `#[thread_local]` here")]
167+
#[suggestion("remove this attribute", code = "", applicability = "maybe-incorrect")]
168+
pub(crate) attr: Span,
169+
}
170+
161171
pub(crate) use autodiff::*;
162172

163173
mod autodiff {

compiler/rustc_builtin_macros/src/global_allocator.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ pub(crate) fn expand(
3838
return vec![orig_item];
3939
};
4040

41+
// Forbid `#[thread_local]` attributes on the item
42+
if let Some(attr) = item.attrs.iter().find(|x| x.has_name(sym::thread_local)) {
43+
ecx.dcx().emit_err(errors::AllocCannotThreadLocal { span: item.span, attr: attr.span });
44+
return vec![orig_item];
45+
}
46+
4147
// Generate a bunch of new items using the AllocFnFactory
4248
let span = ecx.with_def_site_ctxt(item.span);
4349
let f = AllocFnFactory { span, ty_span, global: ident, cx: ecx };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(thread_local)]
2+
3+
use std::alloc::System;
4+
5+
#[global_allocator]
6+
#[thread_local]
7+
static A: System = System;
8+
//~^ ERROR: allocators cannot be `#[thread_local]`
9+
10+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: allocators cannot be `#[thread_local]`
2+
--> $DIR/no-thread-local.rs:7:1
3+
|
4+
LL | #[thread_local]
5+
| ---------------
6+
| |
7+
| marked `#[thread_local]` here
8+
| help: remove this attribute
9+
LL | static A: System = System;
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
12+
error: aborting due to 1 previous error
13+

0 commit comments

Comments
 (0)