Improve error messages involving type aliases

c7d372e
Opened by NODA Kai at 2024-11-05 21:16:09
extern crate libc;
fn main() {
    let a: libc::ssize_t = 0;
    let () = a;
}
$ rustc show-typedef.rs
show-typedef.rs:4:17: 4:19 error: mismatched types: expected `i64`, found `()` (expected i64, found ())
show-typedef.rs:4             let () = a;
                                  ^~
error: aborting due to previous error

It would be great if rustc could show libc::ssize_t as well as i64 in the error message. Compare it with GCC 4.9

$ g++49 show-typedef.cpp
show-typedef.cpp: In function 'int main()':
show-typedef.cpp:3:28: error: invalid operands of types 'void' and 'const size_type {aka const long unsigned int}' to binary 'operator+'
     (void)0 + std::string::npos;
                            ^
#include <string>
int main() {
    (void)0 + std::string::npos;
}
  1. This doesn't need libc to reproduce, it happens with any type synonym:

    type Lol = i32;
    fn main() {
        let a: Lol = 0;
        let () = a;
    }
    

    Steve Klabnik at 2016-02-02 22:13:25

  2. Right, libc was just for presenting a motivating example.

    NODA Kai at 2016-02-02 23:30:15

  3. Triage: no change

    Steve Klabnik at 2018-09-24 17:49:14

  4. This is fundamentally the same as #21934, only more specific.

    Esteban Kuber at 2019-03-22 23:11:34

  5. Rustdoc would benefit from work on this; see https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/To.20alias.20or.20not.20to.20alias.

    Noah Lev at 2022-03-02 19:03:52

  6. Current output with 1.65.0-nightly (2022-08-10 29e4a9ee0253cd39e552):

    type Lol = i32;
    fn main() {
        let a: Lol = 0;
        let () = a;
    }
    
    error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
     --> src/main.rs:4:9
      |
    4 |     let () = a;
      |         ^^   - this expression has type `i32`
      |         |
      |         expected `i32`, found `()`
    
    For more information about this error, try `rustc --explain E0308`.
    

    TimJentzsch at 2022-08-11 14:02:00

  7. CC https://github.com/rust-lang/rust/pull/97974

    Esteban Kuber at 2022-08-11 14:50:33