Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading error message when passing a reference to an FnOnce and a non-reference is expected #47648

Open
illicitonion opened this issue Jan 21, 2018 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@illicitonion
Copy link

illicitonion commented Jan 21, 2018

Compiling this code:

struct Foo {}

impl Foo {
    pub fn foo(&self) -> String {
        "foo".to_owned()
    }
}

fn main() {
    let foo = Foo{};
    let closure = move || foo;

    call(&closure);
}

fn call<F: FnOnce() -> Foo>(f: F) {
    println!("{}", f().foo())
}

gives me the following error:

error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
  --> src/main.rs:11:19
   |
11 |     let closure = move || foo;
   |                   ^^^^^^^^^^^
12 | 
13 |     call(&closure);
   |     ---- the requirement to implement `Fn` derives from here
   |
note: closure is `FnOnce` because it moves the variable `foo` out of its environment
  --> src/main.rs:11:27
   |
11 |     let closure = move || foo;
   |                           ^^^

call does expect a closure which implements FnOnce, not one which implements Fn as the error message suggests. Accordingly, the line "expected a closure that implements the Fn trait, but this closure only implements FnOnce" is very misleading.

The actual problem here is that a reference to an FnOnce is being passed, rather than the FnOnce itself. It would be great if the error message reflected this fact.

@TimNN TimNN added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels Jan 23, 2018
@estebank
Copy link
Contributor

Current output, still incorrect:

error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
  --> src/main.rs:11:19
   |
11 |     let closure = move || foo;
   |                   ^^^^^^^^---
   |                   |       |
   |                   |       closure is `FnOnce` because it moves the variable `foo` out of its environment
   |                   this closure implements `FnOnce`, not `Fn`
12 | 
13 |     call(&closure);
   |     ---- the requirement to implement `Fn` derives from here

@crlf0710 crlf0710 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants