Skip to content

Confusing error when trying to call a function member of a struct #36528

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

Closed
glandium opened this issue Sep 16, 2016 · 1 comment
Closed

Confusing error when trying to call a function member of a struct #36528

glandium opened this issue Sep 16, 2016 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@glandium
Copy link
Contributor

This is essentially the same as issue #2392, but in a slightly different case:

struct Cat<F> where F: FnMut() -> u32 {
    func: F,
}

trait Meow {
    fn mew(&mut self) -> i32;
}

impl<F> Meow for Cat<F> where F: FnMut() -> u32 {
    fn mew(&mut self) -> i32 {
        self.func()
    }
}

The compiler outputs:

error: no method named `func` found for type `&mut Cat<F>` in the current scope
  --> <anon>:11:14
   |>
11 |>         self.func()
   |>              ^^^^

It should also add:

note: use `(self.func)(...)` if you meant to call the function stored in the `func` field
  --> <anon>:17:19
   |>
11 |>     let x = self.func();
   |>                  ^^^^

which is the message that the fix for issue #2392 added.

This is not a regression, because adding:

fn main() {
    let kitty = Cat { func: || 5 };
    let x = kitty.func();
}

does give this output:

error: no method named `func` found for type `Cat<[closure@<anon>:16:29: 16:33]>` in the current scope
  --> <anon>:17:19
   |>
17 |>     let x = kitty.func();
   |>                   ^^^^
note: use `(kitty.func)(...)` if you meant to call the function stored in the `func` field
  --> <anon>:17:19
   |>
17 |>     let x = kitty.func();
   |>                   ^^^^
@TimNN TimNN added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 16, 2016
@glandium
Copy link
Contributor Author

Turns out this is already fixed in beta.

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
Projects
None yet
Development

No branches or pull requests

2 participants