forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#100019 - TaKO8Ki:suggest-boxed-trait-object…
…s-instead-of-impl-trait, r=compiler-errors Revive suggestions for boxed trait objects instead of impl Trait The suggestion implemented in rust-lang#75608 was not working properly, so I fixed it.
- Loading branch information
Showing
8 changed files
with
217 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/test/ui/mismatched_types/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
struct S; | ||
struct Y; | ||
|
||
trait Trait {} | ||
|
||
impl Trait for Y {} | ||
|
||
fn foo() -> impl Trait { | ||
if true { | ||
S | ||
} else { | ||
Y //~ ERROR `if` and `else` have incompatible types | ||
} | ||
} | ||
|
||
fn bar() -> impl Trait { | ||
match true { | ||
true => S, | ||
false => Y, //~ ERROR `match` arms have incompatible types | ||
} | ||
} | ||
|
||
fn main() {} |
26 changes: 26 additions & 0 deletions
26
src/test/ui/mismatched_types/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error[E0308]: `if` and `else` have incompatible types | ||
--> $DIR/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.rs:12:9 | ||
| | ||
LL | / if true { | ||
LL | | S | ||
| | - expected because of this | ||
LL | | } else { | ||
LL | | Y | ||
| | ^ expected struct `S`, found struct `Y` | ||
LL | | } | ||
| |_____- `if` and `else` have incompatible types | ||
|
||
error[E0308]: `match` arms have incompatible types | ||
--> $DIR/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.rs:19:18 | ||
| | ||
LL | / match true { | ||
LL | | true => S, | ||
| | - this is found to be of type `S` | ||
LL | | false => Y, | ||
| | ^ expected struct `S`, found struct `Y` | ||
LL | | } | ||
| |_____- `match` arms have incompatible types | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
28 changes: 28 additions & 0 deletions
28
src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// run-rustfix | ||
|
||
#![allow(dead_code)] | ||
|
||
struct S; | ||
struct Y; | ||
|
||
trait Trait {} | ||
|
||
impl Trait for S {} | ||
impl Trait for Y {} | ||
|
||
fn foo() -> Box<dyn Trait> { | ||
if true { | ||
Box::new(S) | ||
} else { | ||
Box::new(Y) //~ ERROR `if` and `else` have incompatible types | ||
} | ||
} | ||
|
||
fn bar() -> Box<dyn Trait> { | ||
match true { | ||
true => Box::new(S), | ||
false => Box::new(Y), //~ ERROR `match` arms have incompatible types | ||
} | ||
} | ||
|
||
fn main() {} |
28 changes: 28 additions & 0 deletions
28
src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// run-rustfix | ||
|
||
#![allow(dead_code)] | ||
|
||
struct S; | ||
struct Y; | ||
|
||
trait Trait {} | ||
|
||
impl Trait for S {} | ||
impl Trait for Y {} | ||
|
||
fn foo() -> impl Trait { | ||
if true { | ||
S | ||
} else { | ||
Y //~ ERROR `if` and `else` have incompatible types | ||
} | ||
} | ||
|
||
fn bar() -> impl Trait { | ||
match true { | ||
true => S, | ||
false => Y, //~ ERROR `match` arms have incompatible types | ||
} | ||
} | ||
|
||
fn main() {} |
47 changes: 47 additions & 0 deletions
47
src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
error[E0308]: `if` and `else` have incompatible types | ||
--> $DIR/suggest-boxed-trait-objects-instead-of-impl-trait.rs:17:9 | ||
| | ||
LL | / if true { | ||
LL | | S | ||
| | - expected because of this | ||
LL | | } else { | ||
LL | | Y | ||
| | ^ expected struct `S`, found struct `Y` | ||
LL | | } | ||
| |_____- `if` and `else` have incompatible types | ||
| | ||
help: you could change the return type to be a boxed trait object | ||
| | ||
LL | fn foo() -> Box<dyn Trait> { | ||
| ~~~~~~~ + | ||
help: if you change the return type to expect trait objects, box the returned expressions | ||
| | ||
LL ~ Box::new(S) | ||
LL | } else { | ||
LL ~ Box::new(Y) | ||
| | ||
|
||
error[E0308]: `match` arms have incompatible types | ||
--> $DIR/suggest-boxed-trait-objects-instead-of-impl-trait.rs:24:18 | ||
| | ||
LL | / match true { | ||
LL | | true => S, | ||
| | - this is found to be of type `S` | ||
LL | | false => Y, | ||
| | ^ expected struct `S`, found struct `Y` | ||
LL | | } | ||
| |_____- `match` arms have incompatible types | ||
| | ||
help: you could change the return type to be a boxed trait object | ||
| | ||
LL | fn bar() -> Box<dyn Trait> { | ||
| ~~~~~~~ + | ||
help: if you change the return type to expect trait objects, box the returned expressions | ||
| | ||
LL ~ true => Box::new(S), | ||
LL ~ false => Box::new(Y), | ||
| | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |