Skip to content

E0525 should point at where a closure trait obligation was introduced #101157

@estebank

Description

@estebank
Contributor

Given a closure that doesn't fulfill a trait bound, E0525 is emitted but there's no span pointing at where the obligation was introduced:

error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
  --> src/lib.rs:6:9
   |
6  |         move || {
   |         ^^^^^^^ this closure implements `FnMut`, not `Fn`
7  |             i += 1;
   |             - closure is `FnMut` because it mutates the variable `i` here
...
11 |     is_fn(f);
   |     ----- the requirement to implement `Fn` derives from here

We should point at it, like we already do for E0277:

error[E0277]: expected a `Fn<()>` closure, found `[closure@src/lib.rs:7:31: 7:36]`
 --> src/lib.rs:9:11
  |
9 |     is_fn(f);
  |     ----- ^ expected an `Fn<()>` closure, found `[closure@src/lib.rs:7:31: 7:36]`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the trait `Fn<()>` is not implemented for `[closure@src/lib.rs:7:31: 7:36]`
  = note: wrap the `[closure@src/lib.rs:7:31: 7:36]` in a closure with no arguments: `|| { /* code */ }`
  = note: `[closure@src/lib.rs:7:31: 7:36]` implements `FnOnce`, but it must implement `Fn`, which is more general
note: required by a bound in `is_fn`
 --> src/lib.rs:2:13
  |
2 | fn is_fn<F: Fn()>(_: F) {}
  |             ^^^^ required by this bound in `is_fn`

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
D-papercutDiagnostics: An error or lint that needs small tweaks.
D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
on Aug 29, 2022
compiler-errors

compiler-errors commented on Aug 29, 2022

@compiler-errors
Member

@estebank on nightly:

error[[E0525]](https://doc.rust-lang.org/nightly/error-index.html#E0525): expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
  --> src/lib.rs:6:9
   |
6  |         move || {
   |         ^^^^^^^ this closure implements `FnMut`, not `Fn`
7  |             i += 1;
   |             - closure is `FnMut` because it mutates the variable `i` here
...
11 |     is_fn(f);
   |     ----- - the requirement to implement `Fn` derives from here
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `is_fn`
  --> src/lib.rs:1:13
   |
1  | fn is_fn<F: Fn()>(_: F) {}
   |             ^^^^ required by this bound in `is_fn`

For more information about this error, try `rustc --explain E0525`.

Am I missing something? I think the "required by a bound in" was added in #100654.

estebank

estebank commented on Aug 29, 2022

@estebank
ContributorAuthor

@compiler-errors I could have sworn that I tried it on nightly in the playground 🤦‍♂️

I knew that you'd done something "similar" recently (very similar) which is why this was in my head.

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 lintsD-papercutDiagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-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

        Participants

        @estebank@compiler-errors

        Issue actions

          E0525 should point at where a closure trait obligation was introduced · Issue #101157 · rust-lang/rust