rust-lang / rust-lang/rust

thread_local!{static mut x: y = 0;} produces very bad error messages

Open
#151,564 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
thread_local! {
    /// hello
    /// world
    pub static mut tables: i32 = 0;
}
Current output
error: no rules expected `vis` metavariable
   --> src/lib.rs:3:1
    |
  3 | / thread_local! {
  4 | |     /// hello
  5 | |     /// world
  6 | |     pub static mut tables: i32 = 0;
  7 | | }
    | |_^ no rules expected this token in macro call
    |
note: while trying to match `@`
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:152:9
    |
152 |         @processing_cfg_attr { pred: ($($predicate:tt)*), rhs: [] };
    |         ^
    = note: captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens
    = note: see <https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment> for more information
    = note: this error originates in the macro `$crate::thread::local_impl::thread_local_process_attrs` which comes from the expansion of the macro `thread_local` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `playground` (lib) due to 1 previous error
Desired output
error: no rules expected `mut`
   --> src/lib.rs:3:1
    |
  6 |    pub static mut tables: i32 = 0;
    |               ^^^ no rules expected this token in macro call
    |
note: [...]
Rationale and extra context

It took me way too long to find out that, oh, right, I'm supposed to use Cell here, not mut. :)

Other cases

Rust Version
rust playground
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.

Research direction

Start with the thread_local! macro definitions in std/src/thread/local.rs, especially the attribute-processing entry point shown in the diagnostic, and reproduce the supplied static mut example. Trace why the macro reports the forwarded vis metavariable instead of mut; done means the diagnostic points at mut and matches the desired output, with coverage for this example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.