Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compiler/rustc_builtin_macros/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_builtin_macros/src/global_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/allocator/no-thread-local.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions tests/ui/allocator/no-thread-local.stderr
Original file line number Diff line number Diff line change
@@ -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

Loading