diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2023-05-23 16:44:27 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-23 16:44:27 +0530 |
| commit | 4b26b80dd56f03affae07eeaffae5c284247a40b (patch) | |
| tree | c16966b08c2f071298a914fbae738feace29521f /tests | |
| parent | 00185bec7ce88eae94adbc3185b802bfec8cad9a (diff) | |
| parent | 3db2bcf4ebb08f7fbe3de7fded45e96639ca3e68 (diff) | |
| download | rust-4b26b80dd56f03affae07eeaffae5c284247a40b.tar.gz rust-4b26b80dd56f03affae07eeaffae5c284247a40b.zip | |
Rollup merge of #111704 - compiler-errors:sized-return-cleanup, r=oli-obk
Remove return type sized check hack from hir typeck Remove a bunch of special-cased suggestions when someone returns `-> dyn Trait` that checks for type equality, etc. This was a pretty complex piece of code that also relied on a hack in hir typeck (see changes to `compiler/rustc_hir_typeck/src/check.rs`), and I'm not convinced that it's necessary to maintain, when all we really need to tell the user is that they should return `-> impl Trait` or `-> Box<dyn Trait>`, depending on their specific use-case. This is necessary because we may need to move the "return type is sized" check from hir typeck to wfcheck, which does not have access to typeck results. This is a prerequisite for that, and I'm fairly confident that the diagnostics "regressions" here are not a big deal.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/error-codes/E0746.fixed | 18 | ||||
| -rw-r--r-- | tests/ui/error-codes/E0746.rs | 2 | ||||
| -rw-r--r-- | tests/ui/error-codes/E0746.stderr | 22 | ||||
| -rw-r--r-- | tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs | 2 | ||||
| -rw-r--r-- | tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr | 110 | ||||
| -rw-r--r-- | tests/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.rs | 4 | ||||
| -rw-r--r-- | tests/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.stderr | 108 | ||||
| -rw-r--r-- | tests/ui/issues/issue-18107.stderr | 12 | ||||
| -rw-r--r-- | tests/ui/unsized/box-instead-of-dyn-fn.rs | 1 | ||||
| -rw-r--r-- | tests/ui/unsized/box-instead-of-dyn-fn.stderr | 43 | ||||
| -rw-r--r-- | tests/ui/unsized/issue-91801.stderr | 7 | ||||
| -rw-r--r-- | tests/ui/unsized/issue-91803.stderr | 9 |
12 files changed, 131 insertions, 207 deletions
diff --git a/tests/ui/error-codes/E0746.fixed b/tests/ui/error-codes/E0746.fixed deleted file mode 100644 index ca8319aa020..00000000000 --- a/tests/ui/error-codes/E0746.fixed +++ /dev/null @@ -1,18 +0,0 @@ -// run-rustfix -#![allow(dead_code)] -struct Struct; -trait Trait {} -impl Trait for Struct {} -impl Trait for u32 {} - -fn foo() -> impl Trait { Struct } -//~^ ERROR E0746 - -fn bar() -> impl Trait { //~ ERROR E0746 - if true { - return 0; - } - 42 -} - -fn main() {} diff --git a/tests/ui/error-codes/E0746.rs b/tests/ui/error-codes/E0746.rs index bf5ba8fff56..86b5b7444d1 100644 --- a/tests/ui/error-codes/E0746.rs +++ b/tests/ui/error-codes/E0746.rs @@ -1,5 +1,5 @@ -// run-rustfix #![allow(dead_code)] + struct Struct; trait Trait {} impl Trait for Struct {} diff --git a/tests/ui/error-codes/E0746.stderr b/tests/ui/error-codes/E0746.stderr index 2153b59ad18..9fe90ab7bec 100644 --- a/tests/ui/error-codes/E0746.stderr +++ b/tests/ui/error-codes/E0746.stderr @@ -4,11 +4,14 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn foo() -> dyn Trait { Struct } | ^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> -help: use `impl Trait` as the return type, as all return paths are of type `Struct`, which implements `Trait` +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | LL | fn foo() -> impl Trait { Struct } - | ~~~~~~~~~~ + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` + | +LL | fn foo() -> Box<dyn Trait> { Box::new(Struct) } + | ++++ + +++++++++ + error[E0746]: return type cannot have an unboxed trait object --> $DIR/E0746.rs:11:13 @@ -16,11 +19,18 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn bar() -> dyn Trait { | ^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> -help: use `impl Trait` as the return type, as all return paths are of type `{integer}`, which implements `Trait` +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | LL | fn bar() -> impl Trait { - | ~~~~~~~~~~ + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` + | +LL ~ fn bar() -> Box<dyn Trait> { +LL | if true { +LL ~ return Box::new(0); +LL | } +LL ~ Box::new(42) + | error: aborting due to 2 previous errors diff --git a/tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs b/tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs index cbf1daabe2b..af368203de0 100644 --- a/tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs +++ b/tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs @@ -26,7 +26,7 @@ fn bax() -> dyn Trait { //~ ERROR E0746 if true { Struct } else { - 42 //~ ERROR `if` and `else` have incompatible types + 42 } } fn bam() -> Box<dyn Trait> { diff --git a/tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr b/tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr index dc1e40ea560..ed9261d0de5 100644 --- a/tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr +++ b/tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr @@ -46,11 +46,10 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn bap() -> Trait { Struct } | ^^^^^ doesn't have a size known at compile-time | - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> -help: use `impl Trait` as the return type, as all return paths are of type `Struct`, which implements `Trait` +help: box the return type, and wrap all of the returned values in `Box::new` | -LL | fn bap() -> impl Trait { Struct } - | ~~~~~~~~~~ +LL | fn bap() -> Box<Trait> { Box::new(Struct) } + | ++++ + +++++++++ + error[E0746]: return type cannot have an unboxed trait object --> $DIR/dyn-trait-return-should-be-impl-trait.rs:15:13 @@ -58,11 +57,14 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn ban() -> dyn Trait { Struct } | ^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> -help: use `impl Trait` as the return type, as all return paths are of type `Struct`, which implements `Trait` +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | LL | fn ban() -> impl Trait { Struct } - | ~~~~~~~~~~ + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` + | +LL | fn ban() -> Box<dyn Trait> { Box::new(Struct) } + | ++++ + +++++++++ + error[E0746]: return type cannot have an unboxed trait object --> $DIR/dyn-trait-return-should-be-impl-trait.rs:17:13 @@ -70,14 +72,14 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn bak() -> dyn Trait { unimplemented!() } | ^^^^^^^^^ doesn't have a size known at compile-time | -help: use `impl Trait` as the return type if all return paths have the same type but you want to expose only the trait in the signature +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | LL | fn bak() -> impl Trait { unimplemented!() } - | ~~~~~~~~~~ -help: use a boxed trait object if all return paths implement trait `Trait` + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` | -LL | fn bak() -> Box<dyn Trait> { unimplemented!() } - | ++++ + +LL | fn bak() -> Box<dyn Trait> { Box::new(unimplemented!()) } + | ++++ + +++++++++ + error[E0746]: return type cannot have an unboxed trait object --> $DIR/dyn-trait-return-should-be-impl-trait.rs:19:13 @@ -85,34 +87,18 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn bal() -> dyn Trait { | ^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types> - = note: if all the returned values were of the same type you could use `impl Trait` as the return type - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> - = note: you can create a new `enum` with a variant for each returned type -help: return a boxed trait object instead +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | -LL | fn bal() -> Box<dyn Trait> { - | ++++ + -help: ... and box this value +LL | fn bal() -> impl Trait { + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` | -LL | return Box::new(Struct); - | +++++++++ + -help: ... and box this value +LL ~ fn bal() -> Box<dyn Trait> { +LL | if true { +LL ~ return Box::new(Struct); +LL | } +LL ~ Box::new(42) | -LL | Box::new(42) - | +++++++++ + - -error[E0308]: `if` and `else` have incompatible types - --> $DIR/dyn-trait-return-should-be-impl-trait.rs:29:9 - | -LL | / if true { -LL | | Struct - | | ------ expected because of this -LL | | } else { -LL | | 42 - | | ^^ expected `Struct`, found integer -LL | | } - | |_____- `if` and `else` have incompatible types error[E0746]: return type cannot have an unboxed trait object --> $DIR/dyn-trait-return-should-be-impl-trait.rs:25:13 @@ -120,22 +106,18 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn bax() -> dyn Trait { | ^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types> - = note: if all the returned values were of the same type you could use `impl Trait` as the return type - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> - = note: you can create a new `enum` with a variant for each returned type -help: return a boxed trait object instead +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | -LL | fn bax() -> Box<dyn Trait> { - | ++++ + -help: ... and box this value +LL | fn bax() -> impl Trait { + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` | -LL | Box::new(Struct) - | +++++++++ + -help: ... and box this value +LL ~ fn bax() -> Box<dyn Trait> { +LL | if true { +LL ~ Box::new(Struct) +LL | } else { +LL ~ Box::new(42) | -LL | Box::new(42) - | +++++++++ + error[E0308]: mismatched types --> $DIR/dyn-trait-return-should-be-impl-trait.rs:34:16 @@ -279,11 +261,18 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn bat() -> dyn Trait { | ^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> -help: use `impl Trait` as the return type, as all return paths are of type `{integer}`, which implements `Trait` +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | LL | fn bat() -> impl Trait { - | ~~~~~~~~~~ + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` + | +LL ~ fn bat() -> Box<dyn Trait> { +LL | if true { +LL ~ return Box::new(0); +LL | } +LL ~ Box::new(42) + | error[E0746]: return type cannot have an unboxed trait object --> $DIR/dyn-trait-return-should-be-impl-trait.rs:66:13 @@ -291,13 +280,20 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn bay() -> dyn Trait { | ^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> -help: use `impl Trait` as the return type, as all return paths are of type `{integer}`, which implements `Trait` +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | LL | fn bay() -> impl Trait { - | ~~~~~~~~~~ + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` + | +LL ~ fn bay() -> Box<dyn Trait> { +LL | if true { +LL ~ Box::new(0) +LL | } else { +LL ~ Box::new(42) + | -error: aborting due to 20 previous errors +error: aborting due to 19 previous errors Some errors have detailed explanations: E0277, E0308, E0746. For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.rs b/tests/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.rs index fa7664a83ee..a8a6288eb56 100644 --- a/tests/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.rs +++ b/tests/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.rs @@ -77,7 +77,7 @@ fn hat() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed fn pug() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed trait object match 13 { 0 => 0i32, - 1 => 1u32, //~ ERROR `match` arms have incompatible types + 1 => 1u32, _ => 2u32, } } @@ -86,7 +86,7 @@ fn man() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed if false { 0i32 } else { - 1u32 //~ ERROR `if` and `else` have incompatible types + 1u32 } } diff --git a/tests/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.stderr b/tests/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.stderr index 3c65fd998c5..9205d74504f 100644 --- a/tests/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.stderr +++ b/tests/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.stderr @@ -171,39 +171,20 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn hat() -> dyn std::fmt::Display { | ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types> - = note: if all the returned values were of the same type you could use `impl std::fmt::Display` as the return type - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> - = note: you can create a new `enum` with a variant for each returned type -help: return a boxed trait object instead - | -LL | fn hat() -> Box<dyn std::fmt::Display> { - | ++++ + -help: ... and box this value - | -LL | return Box::new(0i32); - | +++++++++ + -help: ... and box this value - | -LL | Box::new(1u32) - | +++++++++ + - -error[E0308]: `match` arms have incompatible types - --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:80:14 +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | -LL | / match 13 { -LL | | 0 => 0i32, - | | ---- this is found to be of type `i32` -LL | | 1 => 1u32, - | | ^^^^ expected `i32`, found `u32` -LL | | _ => 2u32, -LL | | } - | |_____- `match` arms have incompatible types +LL | fn hat() -> impl std::fmt::Display { + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` | -help: change the type of the numeric literal from `u32` to `i32` +LL ~ fn hat() -> Box<dyn std::fmt::Display> { +LL | match 13 { +LL | 0 => { +LL ~ return Box::new(0i32); +LL | } +LL | _ => { +LL ~ Box::new(1u32) | -LL | 1 => 1i32, - | ~~~ error[E0746]: return type cannot have an unboxed trait object --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:77:13 @@ -211,43 +192,18 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn pug() -> dyn std::fmt::Display { | ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types> - = note: if all the returned values were of the same type you could use `impl std::fmt::Display` as the return type - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> - = note: you can create a new `enum` with a variant for each returned type -help: return a boxed trait object instead - | -LL | fn pug() -> Box<dyn std::fmt::Display> { - | ++++ + -help: ... and box this value +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | -LL | 0 => Box::new(0i32), - | +++++++++ + -help: ... and box this value - | -LL | 1 => Box::new(1u32), - | +++++++++ + -help: ... and box this value - | -LL | _ => Box::new(2u32), - | +++++++++ + - -error[E0308]: `if` and `else` have incompatible types - --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:89:9 +LL | fn pug() -> impl std::fmt::Display { + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` | -LL | / if false { -LL | | 0i32 - | | ---- expected because of this -LL | | } else { -LL | | 1u32 - | | ^^^^ expected `i32`, found `u32` -LL | | } - | |_____- `if` and `else` have incompatible types - | -help: change the type of the numeric literal from `u32` to `i32` +LL ~ fn pug() -> Box<dyn std::fmt::Display> { +LL | match 13 { +LL ~ 0 => Box::new(0i32), +LL ~ 1 => Box::new(1u32), +LL ~ _ => Box::new(2u32), | -LL | 1i32 - | ~~~ error[E0746]: return type cannot have an unboxed trait object --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:85:13 @@ -255,24 +211,20 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn man() -> dyn std::fmt::Display { | ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types> - = note: if all the returned values were of the same type you could use `impl std::fmt::Display` as the return type - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> - = note: you can create a new `enum` with a variant for each returned type -help: return a boxed trait object instead +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | -LL | fn man() -> Box<dyn std::fmt::Display> { - | ++++ + -help: ... and box this value +LL | fn man() -> impl std::fmt::Display { + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` | -LL | Box::new(0i32) - | +++++++++ + -help: ... and box this value +LL ~ fn man() -> Box<dyn std::fmt::Display> { +LL | if false { +LL ~ Box::new(0i32) +LL | } else { +LL ~ Box::new(1u32) | -LL | Box::new(1u32) - | +++++++++ + -error: aborting due to 14 previous errors +error: aborting due to 12 previous errors Some errors have detailed explanations: E0308, E0746. For more information about an error, try `rustc --explain E0308`. diff --git a/tests/ui/issues/issue-18107.stderr b/tests/ui/issues/issue-18107.stderr index 1669b550a9b..cf4e06316a7 100644 --- a/tests/ui/issues/issue-18107.stderr +++ b/tests/ui/issues/issue-18107.stderr @@ -4,14 +4,18 @@ error[E0746]: return type cannot have an unboxed trait object LL | dyn AbstractRenderer | ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | -help: use `impl AbstractRenderer` as the return type if all return paths have the same type but you want to expose only the trait in the signature +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | LL | impl AbstractRenderer + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` | -help: use a boxed trait object if all return paths implement trait `AbstractRenderer` +LL ~ Box<dyn AbstractRenderer> +LL | +LL | { +LL | match 0 { +LL ~ _ => Box::new(unimplemented!()) | -LL | Box<dyn AbstractRenderer> - | ++++ + error: aborting due to previous error diff --git a/tests/ui/unsized/box-instead-of-dyn-fn.rs b/tests/ui/unsized/box-instead-of-dyn-fn.rs index 2fa741bc1c5..321c2ebf5a1 100644 --- a/tests/ui/unsized/box-instead-of-dyn-fn.rs +++ b/tests/ui/unsized/box-instead-of-dyn-fn.rs @@ -8,7 +8,6 @@ fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> dyn Fn() + 'a { move || println!("{a}") } else { Box::new(move || println!("{}", b)) - //~^ ERROR `if` and `else` have incompatible types } } diff --git a/tests/ui/unsized/box-instead-of-dyn-fn.stderr b/tests/ui/unsized/box-instead-of-dyn-fn.stderr index bfb7c3957f4..6087f5c5465 100644 --- a/tests/ui/unsized/box-instead-of-dyn-fn.stderr +++ b/tests/ui/unsized/box-instead-of-dyn-fn.stderr @@ -1,42 +1,21 @@ -error[E0308]: `if` and `else` have incompatible types - --> $DIR/box-instead-of-dyn-fn.rs:10:9 - | -LL | / if a % 2 == 0 { -LL | | move || println!("{a}") - | | ----------------------- - | | | - | | the expected closure - | | expected because of this -LL | | } else { -LL | | Box::new(move || println!("{}", b)) - | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected closure, found `Box<[closure@box-instead-of-dyn-fn.rs:10:18]>` -LL | | -LL | | } - | |_____- `if` and `else` have incompatible types - | - = note: expected closure `[closure@$DIR/box-instead-of-dyn-fn.rs:8:9: 8:16]` - found struct `Box<[closure@$DIR/box-instead-of-dyn-fn.rs:10:18: 10:25]>` - error[E0746]: return type cannot have an unboxed trait object --> $DIR/box-instead-of-dyn-fn.rs:5:56 | LL | fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> dyn Fn() + 'a { | ^^^^^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types> - = note: if all the returned values were of the same type you could use `impl Fn() + 'a` as the return type - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> - = note: you can create a new `enum` with a variant for each returned type -help: return a boxed trait object instead +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type + | +LL | fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> impl Fn() + 'a { + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` | -LL | fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> Box<dyn Fn() + 'a> { - | ++++ + -help: ... and box this value +LL ~ fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> Box<dyn Fn() + 'a> { +LL | +LL | if a % 2 == 0 { +LL ~ Box::new(move || println!("{a}")) | -LL | Box::new(move || println!("{a}")) - | +++++++++ + -error: aborting due to 2 previous errors +error: aborting due to previous error -Some errors have detailed explanations: E0308, E0746. -For more information about an error, try `rustc --explain E0308`. +For more information about this error, try `rustc --explain E0746`. diff --git a/tests/ui/unsized/issue-91801.stderr b/tests/ui/unsized/issue-91801.stderr index 8795aa1687f..da5c4322403 100644 --- a/tests/ui/unsized/issue-91801.stderr +++ b/tests/ui/unsized/issue-91801.stderr @@ -4,11 +4,10 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> Validator<'a> { | ^^^^^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> -help: use `impl Fn(&'a Something) -> Result<(), ()> + Send + Sync + 'a` as the return type, as all return paths are of type `Box<[closure@$DIR/issue-91801.rs:10:21: 10:70]>`, which implements `Fn(&'a Something) -> Result<(), ()> + Send + Sync + 'a` +help: box the return type, and wrap all of the returned values in `Box::new` | -LL | fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> impl Fn(&'a Something) -> Result<(), ()> + Send + Sync + 'a { - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +LL | fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> Box<Validator<'a>> { + | ++++ + error: aborting due to previous error diff --git a/tests/ui/unsized/issue-91803.stderr b/tests/ui/unsized/issue-91803.stderr index 2dad9e89294..a43b8d0741f 100644 --- a/tests/ui/unsized/issue-91803.stderr +++ b/tests/ui/unsized/issue-91803.stderr @@ -4,11 +4,14 @@ error[E0746]: return type cannot have an unboxed trait object LL | fn or<'a>(first: &'static dyn Foo<'a>) -> dyn Foo<'a> { | ^^^^^^^^^^^ doesn't have a size known at compile-time | - = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> -help: use `impl Foo<'a>` as the return type, as all return paths are of type `Box<_>`, which implements `Foo<'a>` +help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type | LL | fn or<'a>(first: &'static dyn Foo<'a>) -> impl Foo<'a> { - | ~~~~~~~~~~~~ + | ~~~~ +help: box the return type, and wrap all of the returned values in `Box::new` + | +LL | fn or<'a>(first: &'static dyn Foo<'a>) -> Box<dyn Foo<'a>> { + | ++++ + error: aborting due to previous error |
