From 82f2c08200dabfecf82ad7eecd3bc6ff246bda5c Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Mon, 1 Aug 2022 20:22:50 +0900 Subject: fix wrong suggestions for boxed trait objects instead of impl trait --- ...st-boxed-trait-objects-instead-of-impl-trait.rs | 23 +++++++++++++++++++ ...oxed-trait-objects-instead-of-impl-trait.stderr | 26 ++++++++++++++++++++++ ...boxed-trait-objects-instead-of-impl-trait.fixed | 9 +++++++- ...st-boxed-trait-objects-instead-of-impl-trait.rs | 9 +++++++- ...oxed-trait-objects-instead-of-impl-trait.stderr | 25 +++++++++++++++++++-- 5 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/mismatched_types/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.rs create mode 100644 src/test/ui/mismatched_types/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.stderr (limited to 'src/test') diff --git a/src/test/ui/mismatched_types/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.rs b/src/test/ui/mismatched_types/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.rs new file mode 100644 index 00000000000..d302dc99bd8 --- /dev/null +++ b/src/test/ui/mismatched_types/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.rs @@ -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() {} diff --git a/src/test/ui/mismatched_types/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.stderr b/src/test/ui/mismatched_types/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.stderr new file mode 100644 index 00000000000..2f814445bba --- /dev/null +++ b/src/test/ui/mismatched_types/do-not-suggest-boxed-trait-objects-instead-of-impl-trait.stderr @@ -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`. diff --git a/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.fixed b/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.fixed index bc4ccbf503c..f30feaed055 100644 --- a/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.fixed +++ b/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.fixed @@ -10,7 +10,7 @@ trait Trait {} impl Trait for S {} impl Trait for Y {} -fn baz() -> Box { +fn foo() -> Box { if true { Box::new(S) } else { @@ -18,4 +18,11 @@ fn baz() -> Box { } } +fn bar() -> Box { + match true { + true => Box::new(S), + false => Box::new(Y), //~ ERROR `match` arms have incompatible types + } +} + fn main() {} diff --git a/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.rs b/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.rs index 0f609665005..2bd8146e289 100644 --- a/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.rs +++ b/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.rs @@ -10,7 +10,7 @@ trait Trait {} impl Trait for S {} impl Trait for Y {} -fn baz() -> impl Trait { +fn foo() -> impl Trait { if true { S } else { @@ -18,4 +18,11 @@ fn baz() -> impl Trait { } } +fn bar() -> impl Trait { + match true { + true => S, + false => Y, //~ ERROR `match` arms have incompatible types + } +} + fn main() {} diff --git a/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.stderr b/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.stderr index 72b5e25ea46..f58b9c3ec16 100644 --- a/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.stderr +++ b/src/test/ui/mismatched_types/suggest-boxed-trait-objects-instead-of-impl-trait.stderr @@ -12,7 +12,7 @@ LL | | } | help: you could change the return type to be a boxed trait object | -LL | fn baz() -> Box { +LL | fn foo() -> Box { | ~~~~~~~ + help: if you change the return type to expect trait objects, box the returned expressions | @@ -21,6 +21,27 @@ LL | } else { LL ~ Box::new(Y) | -error: aborting due to previous error +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 { + | ~~~~~~~ + +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`. -- cgit 1.4.1-3-g733a5