Skip to content

E0530 when using std::thread_local and a static named init #99018

Open
@CGamesPlay

Description

@CGamesPlay

I tried this code (Playground):

thread_local!(static FOO: usize = 0);
static init: usize = 0;

I expected to see this happen: code compile. Instead, this happened:

error[[E0530]](https://doc.rust-lang.org/stable/error-index.html#E0530): function parameters cannot shadow statics
 --> src/lib.rs:1:1
  |
1 | thread_local!(static FOO: usize = 0);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot be named the same as a static
2 | 
3 | static init: usize = 0;
  | ----------------------- the static `init` is defined here
  |
  = note: this error originates in the macro `$crate::__thread_local_inner` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0530`.

The workaround is obvious, simply don't have a static named init. However, the broader issue is that this appears to violate macro hygienics: any change to the implementation to thread_local could break my code in the future.

Meta

This happens on the playground for all channels (1.62.0, 1.63.0-beta.4, 1.64.0-nightly).

Metadata

Metadata

Assignees

Labels

A-thread-localsArea: Thread local storage (TLS)C-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions