rust-lang / rust-lang/rust

E0434 suggests changing the fn item of a trait impl to a closure

Open
#153,363 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
trait T {
    fn t() -> impl FnOnce() -> ();
}

fn f(x: String) {
    struct S;
    impl T for S {
        fn t() -> impl FnOnce() -> () {
            move || {let _ = x;}
        }
    }
}
Current output
error[E0434]: can't capture dynamic environment in a fn item
 --> src/lib.rs:9:30
  |
9 |             move || {let _ = x;}
  |                              ^
  |
  = help: use the `|| { ... }` closure form instead
Desired output
error[E0434]: can't capture dynamic environment in a fn item
 --> src/lib.rs:9:30
  |
9 |             move || {let _ = x;}
  |                              ^
  |
Rationale and extra context

I don't think it's possible to implement a trait without using a fn item. A more detailed hint that refers to the exect fn keyword would also be nice (see https://github.com/rust-lang/rust/issues/112845).

The suggestion is wrong in this case.

Other cases

Rust Version
rustc 1.93.1 (01f6ddf75 2026-02-11)
binary: rustc
commit-hash: 01f6ddf7588f42ae2d7eb0a2f21d44e8e96674cf
commit-date: 2026-02-11
host: x86_64-unknown-linux-gnu
release: 1.93.1
LLVM version: 21.1.8
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

Reproduce E0434 with the Rust snippet in src/lib.rs and inspect the compiler diagnostic handling for the suggestion to use a closure. The work is done when this trait-impl case no longer emits the incorrect suggestion while the diagnostic remains correct for applicable 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.