Skip to content

Commit dbe7a4e

Browse files
committed
Improve diagnostic for global_allocator + thread_local
1 parent 9614f28 commit dbe7a4e

4 files changed

Lines changed: 17 additions & 8 deletions

File tree

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ pub(crate) struct AllocMustStatics {
163163
pub(crate) struct AllocCannotThreadLocal {
164164
#[primary_span]
165165
pub(crate) span: Span,
166+
#[label("marked `#[thread_local]` here")]
167+
#[suggestion("remove this attribute", code = "", applicability = "maybe-incorrect")]
168+
pub(crate) attr: Span,
166169
}
167170

168171
pub(crate) use autodiff::*;

compiler/rustc_builtin_macros/src/global_allocator.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ pub(crate) fn expand(
3939
};
4040

4141
// Forbid `#[thread_local]` attributes on the item
42-
if let Some(_) = item.attrs.iter().find(|x| { x.has_name(sym::thread_local) }) {
43-
ecx.dcx().emit_err(errors::AllocCannotThreadLocal { span: item.span_with_attributes() });
42+
if let Some(attr) = item.attrs.iter().find(|x| { x.has_name(sym::thread_local) }) {
43+
ecx.dcx().emit_err(errors::AllocCannotThreadLocal {
44+
span: item.span,
45+
attr: attr.span
46+
});
4447
return vec![orig_item];
4548
}
4649

tests/ui/allocator/no-thread-local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::alloc::System;
44

55
#[global_allocator]
66
#[thread_local]
7-
//~^ ERROR: allocators cannot be `#[thread_local]`
87
static A: System = System;
8+
//~^ ERROR: allocators cannot be `#[thread_local]`
99

1010
fn main() {}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
error: allocators cannot be `#[thread_local]`
2-
--> $DIR/no-thread-local.rs:6:1
2+
--> $DIR/no-thread-local.rs:7:1
33
|
4-
LL | / #[thread_local]
5-
LL | |
6-
LL | | static A: System = System;
7-
| |__________________________^
4+
LL | #[thread_local]
5+
| ---------------
6+
| |
7+
| marked `#[thread_local]` here
8+
| help: remove this attribute
9+
LL | static A: System = System;
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
811

912
error: aborting due to 1 previous error
1013

0 commit comments

Comments
 (0)