Skip to content

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

Closed
@ghost

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.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

    Issue actions