Skip to content

Add boxing suggestions for impl Trait return type mismatchesAdd boxing suggestions for impl Trait return type mismatches#155682

Open
Unique-Usman wants to merge 1 commit intorust-lang:mainfrom
Unique-Usman:ua/box-impl
Open

Add boxing suggestions for impl Trait return type mismatchesAdd boxing suggestions for impl Trait return type mismatches#155682
Unique-Usman wants to merge 1 commit intorust-lang:mainfrom
Unique-Usman:ua/box-impl

Conversation

@Unique-Usman
Copy link
Copy Markdown
Contributor

@Unique-Usman Unique-Usman commented Apr 23, 2026

A sort of a follow up pr to this -> #155546

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 23, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 23, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
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 73 candidates
  • Random selection from 19 candidates

Copy link
Copy Markdown
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

r? me

We already have a SuggestBoxingForReturnImplTrait suggestion.

Please use it instead. Or ideally, figure out why it's not being emitted in some cases, like here

// suggestion emitted
fn works() -> impl std::fmt::Display {
    match 13 {
        0 => 0i32,
        _ => 1u32,
    }
}

// suggestion not given
fn oh_no() -> impl std::fmt::Display {
    match 13 {
        0 => return 0i32,
        _ => 1u32,
    }
}
error[E0308]: `match` arms have incompatible types
 --> src/lib.rs:5:14
  |
3 | /     match 13 {
4 | |         0 => 0i32,
  | |              ---- this is found to be of type `i32`
5 | |         _ => 1u32,
  | |              ^^^^ expected `i32`, found `u32`
6 | |     }
  | |_____- `match` arms have incompatible types
  |
help: you could change the return type to be a boxed trait object
  |
2 - fn works() -> impl std::fmt::Display {
2 + fn works() -> Box<dyn std::fmt::Display> {
  |
help: if you change the return type to expect trait objects, box the returned expressions
  |
4 ~         0 => Box::new(0i32),
5 ~         _ => Box::new(1u32),
  |
help: change the type of the numeric literal from `u32` to `i32`
  |
5 -         _ => 1u32,
5 +         _ => 1i32,
  |

error[E0308]: mismatched types
  --> src/lib.rs:13:14
   |
10 | fn oh_no() -> impl std::fmt::Display {
   |               ---------------------- expected a single type implementing `std::fmt::Display` because of return type
11 |     match 13 {
12 |         0 => return 0i32,
   |                     ---- return type resolved to be `i32`
13 |         _ => 1u32,
   |              ^^^^ expected `i32`, found `u32`
   |
help: you can convert a `u32` to an `i32` and panic if the converted value doesn't fit
   |
14 |     }.try_into().unwrap()
   |      ++++++++++++++++++++

For more information about this error, try `rustc --explain E0308`.

View changes since this review

@rustbot rustbot assigned mejrs and unassigned nnethercote Apr 23, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 23, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 23, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

4 participants