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.
This commit is contained in:
commit
4b26b80dd5
15 changed files with 182 additions and 455 deletions
|
|
@ -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() {}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
// run-rustfix
|
||||
#![allow(dead_code)]
|
||||
|
||||
struct Struct;
|
||||
trait Trait {}
|
||||
impl Trait for Struct {}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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> {
|
||||
|
|
|
|||
|
|
@ -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`.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
|
||||
|
|
||||
LL | fn hat() -> Box<dyn std::fmt::Display> {
|
||||
| ++++ +
|
||||
help: ... and box this value
|
||||
LL | fn hat() -> impl std::fmt::Display {
|
||||
| ~~~~
|
||||
help: box the return type, and wrap all of the returned values in `Box::new`
|
||||
|
|
||||
LL | return Box::new(0i32);
|
||||
| +++++++++ +
|
||||
help: ... and box this value
|
||||
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 | Box::new(1u32)
|
||||
| +++++++++ +
|
||||
|
||||
error[E0308]: `match` arms have incompatible types
|
||||
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:80:14
|
||||
|
|
||||
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
|
||||
|
|
||||
help: change the type of the numeric literal from `u32` to `i32`
|
||||
|
|
||||
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
|
||||
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
|
||||
|
|
||||
LL | fn pug() -> Box<dyn std::fmt::Display> {
|
||||
| ++++ +
|
||||
help: ... and box this value
|
||||
LL | fn pug() -> impl std::fmt::Display {
|
||||
| ~~~~
|
||||
help: box the return type, and wrap all of the returned values in `Box::new`
|
||||
|
|
||||
LL | 0 => Box::new(0i32),
|
||||
| +++++++++ +
|
||||
help: ... and box this value
|
||||
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 | 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 | / 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 | 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`.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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) -> Box<dyn Fn() + 'a> {
|
||||
| ++++ +
|
||||
help: ... and box this value
|
||||
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> {
|
||||
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`.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue