Skip to content
Closed
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
6 changes: 6 additions & 0 deletions compiler/rustc_builtin_macros/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,9 @@ builtin_macros_unexpected_lit = expected path to a trait, found literal
.other = for example, write `#[derive(Debug)]` for `Debug`
builtin_macros_unnameable_test_items = cannot test inner items
# New: Disallow simultaneous use of #[global_allocator] and #[thread_local]
builtin_macros_global_allocator_thread_local_conflict =
`#[global_allocator]` cannot be used with `#[thread_local]` on the same static
.note = The global allocator must be a single, program-wide instance.
.help = Remove either the `#[global_allocator]` or `#[thread_local]` attribute.
8 changes: 8 additions & 0 deletions compiler/rustc_builtin_macros/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ pub(crate) struct AllocMustStatics {
pub(crate) span: Span,
}

/// The `#[global_allocator]` attribute cannot be used with `#[thread_local]`.
#[derive(Diagnostic)]
#[diag(builtin_macros_global_allocator_thread_local_conflict)]
pub(crate) struct GlobalAllocatorThreadLocalConflict {
#[primary_span]
pub(crate) span: Span,
}

pub(crate) use autodiff::*;

mod autodiff {
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_builtin_macros/src/global_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ pub(crate) fn expand(
ecx.dcx().emit_err(errors::AllocMustStatics { span: item.span() });
return vec![orig_item];
};


// Check if the static item also has the `#[thread_local]` attribute, which is incompatible with `#[global_allocator]`.
if item.attrs.iter().any(|a| a.path().is_ident(sym::thread_local)) {
ecx.dcx().emit_err(errors::GlobalAllocatorThreadLocalConflict {
span: item.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
Loading