Skip to content

Can this error be a little better? "E0576: cannot find method or associated constant ..." #61729

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
3 tasks
ghost opened this issue Jun 11, 2019 · 3 comments
Closed
3 tasks
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ghost
Copy link

ghost commented Jun 11, 2019

playground:

// src: https://doc.rust-lang.org/stable/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name
trait Pilot {
    fn fly(&self);
}

trait Wizard {
    fn fly(&self);
}

struct Human;

impl Pilot for Human {
    fn fly(&self) {
        println!("This is your captain speaking.");
    }
}

impl Wizard for Human {
    fn fly(&self) {
        println!("Up!");
    }
}

impl Human {
    fn fly(&self) {
        println!("*waving arms furiously*");
    }
}

struct Human2;
impl Human2 {
    fn fly(&self) {
        println!("*waving arms furiously 2*");
    }
}

fn main() {
    let person = Human;
    Pilot::fly(&person);
    <Human as Pilot>::fly(&person);
    Wizard::fly(&person);
    <Human as Wizard>::fly(&person);
    person.fly();
    Human::fly(&person);
    <Human as Human>::fly(&person); // E0576: cannot find method or associated constant `fly` in `Human`  not found in `Human`
    <Human as Human2>::fly(&person); // E0576: cannot find method or associated constant `fly` in `Human2`  not found in `Human2`
}
    Checking l_19_16 v0.1.0 (/home/user/build/2nonpkgs/rust.stuff/reflo/book/l_19_16)
error[E0576]: cannot find method or associated constant `fly` in `Human`
  --> book/l_19_16/src/main.rs:45:23
   |
45 |     <Human as Human>::fly(&person); // E0576: cannot find method or associated constant `fly` in `Human`  not found in `Human`
   |                       ^^^ not found in `Human`

error[E0576]: cannot find method or associated constant `fly` in `Human2`
  --> book/l_19_16/src/main.rs:46:24
   |
46 |     <Human as Human2>::fly(&person); // E0576: cannot find method or associated constant `fly` in `Human2`  not found in `Human2`
   |                        ^^^ not found in `Human2`

error: aborting due to 2 previous errors

error: Could not compile `l_19_16`.

The problem(s):

  • can't use <Human as Human> probably because the latter Human isn't a trait, (so Type as Type is not valid?), as per:

In general, fully qualified syntax is defined as follows:

<Type as Trait>::function(receiver_if_method, next_arg, ...);

For associated functions, there would not be a receiver: there would only be the list of other arguments. You could use fully qualified syntax everywhere that you call functions or methods. However, you’re allowed to omit any part of this syntax that Rust can figure out from other information in the program. You only need to use this more verbose syntax in cases where there are multiple implementations that use the same name and Rust needs help to identify which implementation you want to call.

  • the error says it can't find the method(or associated constant - this is true) in Human but this is not true, the method is there, for both Human and Human2 - can the error be improved in this regard?
  • maybe the error should say that Human(or Human2) isn't a trait instead? (if <Type as Trait>::function(...) is the only allowed syntax.
rustc 1.37.0-dev (5c45343f1 2019-06-08)
binary: rustc
commit-hash: 5c45343f11fbf93cf4e15568aee3ff3f2f287466
commit-date: 2019-06-08
host: x86_64-unknown-linux-gnu
release: 1.37.0-dev
LLVM version: 8.0

cargo 1.37.0-dev (65e3885c 2019-06-06)
release: 1.37.0
commit-hash: 65e3885ce3a60e61aaede458233616c177a1f701
commit-date: 2019-06-06
@jonas-schievink jonas-schievink added 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. labels Jun 11, 2019
@estebank estebank added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Aug 11, 2020
@krtab
Copy link
Contributor

krtab commented Jan 11, 2023

A minimal reproducing example:

struct Foo {}

impl Foo {
    fn bar() {}
}

fn main() {
    <Foo as Foo>::bar()
}

leads to

error[E0576]: cannot find method or associated constant `bar` in `Foo`
  --> example.rs:11:19
   |
11 |     <Foo as Foo>::bar()
   |                   ^^^ not found in `Foo`

error: aborting due to previous error

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

@krtab
Copy link
Contributor

krtab commented Jan 11, 2023

@rustbot claim

@krtab
Copy link
Contributor

krtab commented Oct 20, 2023

Fixed by #109788

@lqd lqd closed this as completed Oct 20, 2023
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. D-papercut Diagnostics: An error or lint that needs small tweaks. 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