Poor error message with type alias and external crate

adf2c01
Opened by Robert Clipsham at 2021-12-29 03:48:11

The following code: https://play.rust-lang.org/?gist=3a06bcc680135c042135ca45984379f7&version=stable

extern crate num;

use num::One;
use num::rational::BigRational;

fn main() {
    BigRational::pow(&BigRational::one(), 23);
}

Gives the following error:

error[E0599]: no function or associated item named `pow` found for type `num::rational::Ratio<num::BigInt>` in the current scope
 --> src/main.rs:7:5
  |
7 |     BigRational::pow(BigRational::one(), 23);
  |     ^^^^^^^^^^^^^^^^
  |
  = note: the method `pow` exists but the following trait bounds were not satisfied:
          `num::BigInt : num::PrimInt`

This is poor for several reasons:

  • The error message talks about num::rational::Ratio<num::BigInt> when the code uses BigRational - to a new user it isn't clear that this is a type alias
  • It says the function does not exist, but then also says it exists and isn't applicable. In addition to being more confusing than useful (debatably), there's nothing the end user can do about it:
    • They can't change the type parameter to satisfy the bound (since it's a type alias in an external crate)
    • They can't modify the implementation to use a different bound (again, because it's in the external crate)
  1. The error message is now different:

    error[E0308]: mismatched types
     --> src/main.rs:7:22
      |
    7 |     BigRational::pow(BigRational::one(), 23);
      |                      ^^^^^^^^^^^^^^^^^^
      |                      |
      |                      expected `&Ratio<BigInt>`, found struct `Ratio`
      |                      help: consider borrowing here: `&BigRational::one()`
      |
      = note: expected reference `&Ratio<BigInt>`
                    found struct `Ratio<BigInt>`
    

    While it does give a nice (and correct) suggestion, it still doesn't substitute the type alias for the actual type, which I think would be a nice touch.

    Ben Reeves at 2021-12-29 03:48:11