Files
rust/tests/ui/impl-trait/equality.stderr
Usman Akinyemi 8b6f7fbd8d Improve E0308 error message for impl Trait return mismatches
When a function returns `impl Trait`, all branches must return the same
concrete type. Previously, the compiler showed:

    expected `First` because of return type

This was misleading, as it suggested the return type was `First`, rather
than any single type implementing the trait.

Update the diagnostic to:

    expected a single type implementing `Value` because of return type

Also highlight the first return expression to make it clearer why
subsequent returns do not match.

Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
2026-04-20 13:59:39 +05:30

48 lines
1.5 KiB
Plaintext

error[E0308]: mismatched types
--> $DIR/equality.rs:17:5
|
LL | fn two(x: bool) -> impl Foo {
| -------- expected a single type implementing `Foo` because of return type
LL | if x {
LL | return 1_i32;
| ----- return type resolved to be `i32`
LL | }
LL | 0_u32
| ^^^^^ expected `i32`, found `u32`
|
help: change the type of the numeric literal from `u32` to `i32`
|
LL - 0_u32
LL + 0_i32
|
error[E0277]: cannot add `impl Foo` to `u32`
--> $DIR/equality.rs:26:11
|
LL | n + sum_to(n - 1)
| ^ no implementation for `u32 + impl Foo`
|
= help: the trait `Add<impl Foo>` is not implemented for `u32`
help: the following other types implement trait `Add<Rhs>`
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `u32` implements `Add`
::: $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: in this macro invocation
--> $SRC_DIR/core/src/internal_macros.rs:LL:COL
|
= note: `&u32` implements `Add<u32>`
::: $SRC_DIR/core/src/internal_macros.rs:LL:COL
|
= note: `u32` implements `Add<&u32>`
::: $SRC_DIR/core/src/internal_macros.rs:LL:COL
|
= note: `&u32` implements `Add`
= note: this error originates in the macro `add_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.