Skip to content

Improve E0308 error message for impl Trait return mismatches#155546

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Unique-Usman:ua/impl_traits
Apr 22, 2026
Merged

Improve E0308 error message for impl Trait return mismatches#155546
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Unique-Usman:ua/impl_traits

Conversation

@Unique-Usman
Copy link
Copy Markdown
Contributor

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.

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>
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 20, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 20, 2026

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 72 candidates
  • Random selection from 16 candidates

@Unique-Usman
Copy link
Copy Markdown
Contributor Author

Fix: #155366

@Unique-Usman
Copy link
Copy Markdown
Contributor Author

cc: @DanielEScherzer

Copy link
Copy Markdown
Contributor

@DanielEScherzer DanielEScherzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much, the updates to the existing tests show that this results in exactly what I was looking for. I don't have r+ rights, but this looks good to me

View changes since this review

@estebank
Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 21, 2026

📌 Commit 8b6f7fb has been approved by estebank

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 21, 2026
rust-bors Bot pushed a commit that referenced this pull request Apr 21, 2026
…uwer

Rollup of 5 pull requests

Successful merges:

 - #155546 (Improve E0308 error message for `impl Trait` return mismatches)
 - #152834 (Fix incorrect `let` to `const` suggestion for pattern bindings)
 - #155425 (Remove duplicated `Flags` methods.)
 - #155547 (Use per-parent disambiguators everywhere)
 - #155590 (Remove AttributeLintKind variants - part 5)
@rust-bors rust-bors Bot merged commit 5abe5ea into rust-lang:main Apr 22, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 22, 2026
rust-timer added a commit that referenced this pull request Apr 22, 2026
Rollup merge of #155546 - Unique-Usman:ua/impl_traits, r=estebank

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.
@reddevilmidzy
Copy link
Copy Markdown
Member

Fix: #155366

nit: @Unique-Usman The issue is automatically closed only when this phrase is in the PR description, not comment.

@Unique-Usman
Copy link
Copy Markdown
Contributor Author

noted.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 26, 2026
Add boxing suggestions for `impl Trait` return type mismatches

A sort of a follow up pr to this -> rust-lang#155546
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 26, 2026
Add boxing suggestions for `impl Trait` return type mismatches

A sort of a follow up pr to this -> rust-lang#155546
jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 27, 2026
Add boxing suggestions for `impl Trait` return type mismatches

A sort of a follow up pr to this -> rust-lang#155546
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants