Rust: Explain type mismatch cause pointing to return type when it is `impl Trait`

Created on 18 Jan 2019  路  3Comments  路  Source: rust-lang/rust

Point to the return type with an appropriate label on

fn foo() -> impl std::fmt::Display {
    if false {
        return 0i32;
    }
    1u64
}

An appropriate wording should be figured out, but it can be along the lines of

error[E0308]: mismatched types
  --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:5:5
   |
LL | fn foo() -> impl std::fmt::Display {
   |             ---------------------- expected because this `impl Trait` return type...
LL |     if false {
LL |         return 0i32;
   |                ---- ...gets coerced to `i32` here
LL |     }
LL |     1u32
   |     ^^^^ expected i32, found u64
   |
   = note: expected type `i32`
              found type `u64`

CC @nikomatsakis @zackmdavis @oli-obk @varkor who might have thoughts on wording.

A-diagnostics E-easy

Most helpful comment

This would be awesome. I don't love the use of the word "coerced" in the label, perhaps something like "...is found to be i32 here" -- just because coercion I would prefer to reserve for a specific set of operations in Rust involving changing the value in some way (e.g., coercing from &Vec<T> to &[T])

All 3 comments

This would be awesome. I don't love the use of the word "coerced" in the label, perhaps something like "...is found to be i32 here" -- just because coercion I would prefer to reserve for a specific set of operations in Rust involving changing the value in some way (e.g., coercing from &Vec<T> to &[T])

Quick nit that was initially confusing for me: in @estebank's OP example, the error says expected i32, found u64, but I'm pretty sure that what it's supposed to be is expected i32, foundu32, since the second literal in the example is 1u32 and not 1u64.

Also, Esteban: You're awesome for putting so much hard work into Rust's diagnostics. Rock on!

Was this page helpful?
0 / 5 - 0 ratings