Skip to content

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

Closed

Description

@jendrikw

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 #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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 3, 2026
  2. changed the title [-]E0434 suggests changing the fn of a trait impl to a closure[/-] [+]E0434 suggests changing the fn item of a trait impl to a closure[/+] on Mar 3, 2026
  3. mvanhorn commented on Mar 9, 2026

    @mvanhorn
  4. mvanhorn commented on Mar 9, 2026

    @mvanhorn
  5. added a commit that references this issue on Sep 21, 2026
    0c365b0
  6. added a commit that references this issue on Sep 21, 2026
    c2f9d25
  7. added a commit that references this issue on Sep 21, 2026
    6e0ed21
  8. added a commit that references this issue on Sep 22, 2026
    d6b8d28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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