Skip to content

Add boxing suggestions for impl Trait return type mismatches#155682

Merged
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
Unique-Usman:ua/box-impl
Apr 27, 2026
Merged

Add boxing suggestions for impl Trait return type mismatches#155682
rust-bors[bot] merged 2 commits 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

@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.

@Unique-Usman
Copy link
Copy Markdown
Contributor Author

r? me

We already have a SuggestBoxingForReturnImplTrait suggestion.

That is was used in the pr.

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

Those two are different errors and are emitted at different locations. While we could make the second case suggest Box::new() for the return type, it is cleaner to let the compiler guide the user (if the user adds Box<dyn std::fmt::Display>, the compiler will then suggest Box::new() in the return expressions). The location where the error is emitted makes it a bit harder to implement, and it could become messy or overly verbose if there are multiple return points, such as in an if-else chain.

@Unique-Usman Unique-Usman requested a review from mejrs April 23, 2026 13:02
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 23, 2026
@mejrs
Copy link
Copy Markdown
Contributor

mejrs commented Apr 23, 2026

Right, sorry. I meant, rather than emit that custom note, use SuggestBoxingForReturnImplTrait::BoxReturnExpr to provide the Box::new suggestion.

it is cleaner to let the compiler guide the user (if the user adds Box, the compiler will then suggest Box::new() in the return expressions).

No? Sometimes it's unavoidable but I'm not a fan of suggesting fixes that create new errors suggesting more fixes etc. This case may be obvious to you and me but remember it's sometimes hard to tell if the compiler is actually being helpful or if it's guiding you into a dead end.

@Unique-Usman
Copy link
Copy Markdown
Contributor Author

Right, sorry. I meant, rather than emit that custom note, use SuggestBoxingForReturnImplTrait::BoxReturnExpr to provide the Box::new suggestion.

it is cleaner to let the compiler guide the user (if the user adds Box, the compiler will then suggest Box::new() in the return expressions).

No? Sometimes it's unavoidable but I'm not a fan of suggesting fixes that create new errors suggesting more fixes etc. This case may be obvious to you and me but remember it's sometimes hard to tell if the compiler is actually being helpful or if it's guiding you into a dead end.

Valid. I will update the pr.

@mejrs mejrs 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
@mejrs mejrs changed the title Add boxing suggestions for impl Trait return type mismatchesAdd boxing suggestions for impl Trait return type mismatches Add boxing suggestions for impl Trait return type mismatches Apr 23, 2026
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 26, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

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.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 26, 2026

📌 Commit a677828 has been approved by mejrs

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 26, 2026
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
rust-bors Bot pushed a commit that referenced this pull request Apr 26, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - #149624 (Fix requires_lto targets needing lto set in cargo)
 - #152443 (NVPTX: Drop support for old architectures and old ISAs)
 - #155317 (`std::io::Take`: Clarify & optimize `BorrowedBuf::set_init` usage.)
 - #155588 (Implement more traits for FRTs)
 - #155682 (Add boxing suggestions for `impl Trait` return type mismatches)
 - #155770 (Avoid misleading closure return type note)
 - #155818 (Convert attribute `FinalizeFn` to fn pointer)
 - #155829 (rustc_attr_parsing: use a `try {}` in `or_malformed`)
 - #155835 (couple of `crate_name` cleanups)
rust-bors Bot pushed a commit that referenced this pull request Apr 27, 2026
Rollup of 12 pull requests

Successful merges:

 - #149624 (Fix requires_lto targets needing lto set in cargo)
 - #155317 (`std::io::Take`: Clarify & optimize `BorrowedBuf::set_init` usage.)
 - #155579 (Make Rcs and Arcs use pointer comparison for unsized types)
 - #155588 (Implement more traits for FRTs)
 - #155708 (Fix heap overflow in slice::join caused by misbehaving Borrow)
 - #155778 (Avoid Vec allocation in TyCtxt::mk_place_elem)
 - #151014 (std: sys: process: uefi: Add program searching)
 - #155682 (Add boxing suggestions for `impl Trait` return type mismatches)
 - #155770 (Avoid misleading closure return type note)
 - #155818 (Convert attribute `FinalizeFn` to fn pointer)
 - #155829 (rustc_attr_parsing: use a `try {}` in `or_malformed`)
 - #155835 (couple of `crate_name` cleanups)
@rust-bors rust-bors Bot merged commit 9562f3d into rust-lang:main Apr 27, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 27, 2026
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.

4 participants