rust-lang / rust-lang/rust

Misleading diagnostic when using the `thread_local` macro

Open
#139,247 1 comment 0 reactions 1 assignee View on GitHub

@mysma-9403 is already working on this.

Since Apr 2, 2025.

A-diagnostics A-macros T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn main () {
    thread_local!(static FOO: () = ())
}
Current output
error: expected expression, found `vis` metavariable
 --> test.rs:3:5
  |
3 |     thread_local!(static FOO: () = ())
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |     |
  |     expected expression
  |     in this macro invocation
  |
  = note: the macro call doesn't expand to an expression, but it can expand to a statement
  = note: this error originates in the macro `$crate::thread::local_impl::thread_local_inner` which comes from the expansion of the macro `thread_local` (in Nightly builds, run with -Z macro-backtrace for more info)
help: add `;` to interpret the expansion as a statement
 --> /home/jl-jiang/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:201:92
  |
20|         $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, $init);;
  |                                                                                            +

error: aborting due to 1 previous error
Desired output
error: expected expression, found `vis` metavariable
 --> test.rs:3:5
  |
3 |     thread_local!(static FOO: () = ())
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |     |
  |     expected expression
  |     in this macro invocation
  |
  = note: the macro call doesn't expand to an expression, but it can expand to a statement
  = note: this error originates in the macro `$crate::thread::local_impl::thread_local_inner` which comes from the expansion of the macro `thread_local` (in Nightly builds, run with -Z macro-backtrace for more info)
help: add `;` to interpret the expansion as a statement
 --> test.rs:3:39
  |
3 |    thread_local!(static FOO: () = ());
  |                                      +

error: aborting due to 1 previous error
Rationale and extra context

In this test case, the error can be resolved and the code can be successfully compiled by simply adding a ; at the end of the problematic line.

Other cases

Rust Version
rustc 1.88.0-nightly (e2014e876 2025-04-01)
binary: rustc
commit-hash: e2014e876e3efaa69bf51c19579adb16c3df5f81
commit-date: 2025-04-01
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.1
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.