Implement method suggestions for associated functions

e627419
Opened by gnzlbg at 2023-01-13 17:12:58

This doesn't suggest bar but IMO should (see it live):

struct A { }

impl A {
    fn foo() -> Self {
      /*Self::*/bar()
    }
    
    fn bar() -> Self { A { } }
}

fn main() {
    let v = A::foo();
}

produces

error[E0425]: cannot find function `bar` in this scope
 --> src/main.rs:5:17
  |
5 |       /*Self::*/bar()
  |                 ^^^ not found in this scope

error: aborting due to previous error
error: Could not compile `playground`.

I'd like a help/note saying: "Maybe you meant to call Self::bar()?" in this case, but if for example bar` has a typo, I'd like to see more suggestions, like here:

impl A {
    fn foo() -> Self {
      /*Self::*/bar()
    }
    
    fn bar0() -> Self { A { } }
    fn bar1() -> u32 { 0 }  // mismatching-types
    fn bar2() -> Self { A { } }
}

It should suggest whether I meant Self::bar0 or Self::bar2 (Self::bar1 produces a different type). If I remove bar0 and bar2, I'd expect to see a suggestion for Self::bar1 though.

  1. As of the current stable Rust release (1.66.1), the primary suggestion of this issue for a better error message is implemented:

    Compiling playground v0.0.1 (/playground)
    error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find function `bar` in this scope
     --> src/main.rs:5:17
      |
    5 |       /*Self::*/bar()
      |                 ^^^ not found in this scope
      |
    help: consider using the associated function
      |
    5 |       /*Self::*/Self::bar()
      |                 ~~~~~~~~~
    
    For more information about this error, try `rustc --explain E0425`.
    error: could not compile `playground` due to previous error
    

    Later typos example

    The later example where typos are identified, here:

    impl A {
        fn foo() -> Self {
          /*Self::*/bar()
        }
        
        fn bar0() -> Self { A { } }
        fn bar1() -> u32 { 0 }  // mismatching-types
        fn bar2() -> Self { A { } }
    }
    

    is not improved as of the latest stable release (1.66.1):

    Compiling playground v0.0.1 (/playground)
    error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find function `bar` in this scope
     --> src/main.rs:5:17
      |
    5 |       /*Self::*/bar()
      |                 ^^^ not found in this scope
    
    For more information about this error, try `rustc --explain E0425`.
    error: could not compile `playground` due to previous error
    

    Zoe at 2023-01-13 17:12:58