Rollup merge of #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/rust#85517.
This commit is contained in:
Jacob Pratt
2026-04-10 00:00:03 -04:00
committed by GitHub
4 changed files with 39 additions and 0 deletions
@@ -158,6 +158,16 @@ pub(crate) struct AllocMustStatics {
pub(crate) span: Span,
}
#[derive(Diagnostic)]
#[diag("allocators cannot be `#[thread_local]`")]
pub(crate) struct AllocCannotThreadLocal {
#[primary_span]
pub(crate) span: Span,
#[label("marked `#[thread_local]` here")]
#[suggestion("remove this attribute", code = "", applicability = "maybe-incorrect")]
pub(crate) attr: Span,
}
pub(crate) use autodiff::*;
mod autodiff {
@@ -38,6 +38,12 @@ pub(crate) fn expand(
return vec![orig_item];
};
// Forbid `#[thread_local]` attributes on the item
if let Some(attr) = item.attrs.iter().find(|x| x.has_name(sym::thread_local)) {
ecx.dcx().emit_err(errors::AllocCannotThreadLocal { span: item.span, attr: attr.span });
return vec![orig_item];
}
// Generate a bunch of new items using the AllocFnFactory
let span = ecx.with_def_site_ctxt(item.span);
let f = AllocFnFactory { span, ty_span, global: ident, cx: ecx };
+10
View File
@@ -0,0 +1,10 @@
#![feature(thread_local)]
use std::alloc::System;
#[global_allocator]
#[thread_local]
static A: System = System;
//~^ ERROR: allocators cannot be `#[thread_local]`
fn main() {}
+13
View File
@@ -0,0 +1,13 @@
error: allocators cannot be `#[thread_local]`
--> $DIR/no-thread-local.rs:7:1
|
LL | #[thread_local]
| ---------------
| |
| marked `#[thread_local]` here
| help: remove this attribute
LL | static A: System = System;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error