Skip to content

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

Closed
@glandium

Description

@glandium

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();
   |>                   ^^^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions