Skip to content

Conversation

@zhi-ping
Copy link

@zhi-ping zhi-ping commented Nov 25, 2025

Summary

Disallow simultaneous use of #[global_allocator] and #[thread_local] on the same static item. This fixes a logical incompatibility where the global allocator (a single program-wide instance) cannot coexist with thread-local storage.

Test Plan

  • Added a test case in src/test/ui/allocator/global-allocator-thread-local-conflict.rs that verifies the error is emitted when both attributes are present.
  • Tested that existing valid uses of #[global_allocator] (without #[thread_local]) still compile successfully.

Related Issues

Fixes #85517.

Rationale

The global allocator is designed to be a single instance shared across all threads. Using #[thread_local] would create a separate instance per thread, which contradicts the purpose of the global allocator and could lead to unexpected behavior. Adding this check prevents such invalid code from compiling.

This change adds a check in the #[global_allocator] macro to prevent
simultaneous use with #[thread_local], which is logically incompatible.
The global allocator must be a single instance across all threads.

Fixes rust-lang#85517
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 25, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 25, 2025

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented Nov 25, 2025

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@rust-log-analyzer
Copy link
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[TIMING:end] tool::ToolBuild { build_compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu, tool: "tidy", path: "src/tools/tidy", mode: ToolBootstrap, source_type: InTree, extra_features: [], allow_features: "", cargo_args: [], artifact_kind: Binary } -- 11.338
[TIMING:end] tool::Tidy { compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu } -- 0.000
fmt check
Diff in /checkout/compiler/rustc_builtin_macros/src/global_allocator.rs:37:
         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,
-        });
+        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 };
Diff in /checkout/compiler/rustc_builtin_macros/src/errors.rs:152:
 
 /// 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] 
+#[diag(builtin_macros_global_allocator_thread_local_conflict)]
+pub(crate) struct GlobalAllocatorThreadLocalConflict {
+    #[primary_span]
     pub(crate) span: Span,
 }
 
fmt: checked 6570 files
Bootstrap failed while executing `test src/tools/tidy tidyselftest --extra-checks=py,cpp,js,spellcheck`
Build completed unsuccessfully in 0:00:51

@chenyukang
Copy link
Member

Per our policy at rust-lang/compiler-team#893, I'm closing this PR.

@chenyukang chenyukang closed this Nov 25, 2025
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#[global_allocator] and #[thread_local] should not be usable together on the same static

4 participants