Skip to content

Explain type mismatch cause pointing to return type when it is impl Trait #57743

Closed
@estebank

Description

@estebank

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions