Skip to content

Misleading diagnostic when using the thread_local macro #139247

@jjl9807

Description

@jjl9807

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler 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