rust-lang / rust-lang/rust

“attributes on expressions are experimental” error should be more accurate and helpful

Open
#147,853 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-attributes A-diagnostics D-lack-of-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn example(x: bool) {
    #[rustfmt::skip]
    if x { print!("yea"); } else { print!("nay"); }
    
    println!(".");
}
Current output
error[E0658]: attributes on expressions are experimental
 --> src/lib.rs:2:5
  |
2 |     #[rustfmt::skip]
  |     ^^^^^^^^^^^^^^^^
Desired output
error[E0658]: attributes on some kinds of expressions are experimental
 --> src/lib.rs:2:5
  |
2 |     #[rustfmt::skip]
  |     ^^^^^^^^^^^^^^^^
  = help: this attribute will be allowed if the expression statement ends with a semicolon:
  |     if x { print!("yea"); } else { print!("nay"); };
  |                                                    +
Rationale and extra context

There are two problems with this diagnostic:

  • The claim is significantly false; it is not the case that all attributes on all expressions are unstable. (For example, the above code would compile if rustfmt::skip was replaced by allow(deprecated), or if the expression was a function call.)
  • There are simple but unintuitive workarounds available which do not require substantially refactoring the code, such as:
    • Adding a semicolon to an expression-with-block statement
    • Parenthesizing an assignment (surprisingly only works for some attributes, such as allow)
Rust Version
rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: aarch64-apple-darwin
release: 1.90.0
LLVM version: 20.1.8

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 by reproducing the example in src/lib.rs with rustc 1.90.0 and compare the current diagnostic with the desired output. Trace the compiler diagnostic for attributes on expression statements; done means the wording is accurate and the semicolon workaround is shown as help without affecting other cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.