Add boxing suggestions for impl Trait return type mismatches#155682
Add boxing suggestions for impl Trait return type mismatches#155682rust-bors[bot] merged 2 commits intorust-lang:mainfrom
impl Trait return type mismatches#155682Conversation
|
r? @nnethercote rustbot has assigned @nnethercote. Use Why was this reviewer chosen?The reviewer was selected based on:
|
There was a problem hiding this comment.
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`.
|
Reminder, once the PR becomes ready for a review, use |
That is was used in the pr.
Those two are different errors and are emitted at different locations. While we could make the second case suggest |
|
Right, sorry. I meant, rather than emit that custom note, use
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. |
impl Trait return type mismatchesAdd boxing suggestions for impl Trait return type mismatchesimpl Trait return type mismatches
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
360e5d1 to
a677828
Compare
|
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. |
Add boxing suggestions for `impl Trait` return type mismatches A sort of a follow up pr to this -> rust-lang#155546
Add boxing suggestions for `impl Trait` return type mismatches A sort of a follow up pr to this -> rust-lang#155546
…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)
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)
A sort of a follow up pr to this -> #155546