Specific error for unsized dyn Trait return type
Suggest `impl Trait` when possible, and `Box<dyn Trait>` otherwise.
This commit is contained in:
parent
9fe05e9456
commit
6fd564112f
7 changed files with 512 additions and 2 deletions
|
|
@ -0,0 +1,36 @@
|
|||
#![allow(bare_trait_objects)]
|
||||
struct Struct;
|
||||
trait Trait {}
|
||||
impl Trait for Struct {}
|
||||
impl Trait for u32 {}
|
||||
|
||||
fn fuz() -> (usize, Trait) { (42, Struct) }
|
||||
//~^ ERROR E0277
|
||||
//~| ERROR E0308
|
||||
fn bar() -> (usize, dyn Trait) { (42, Struct) }
|
||||
//~^ ERROR E0277
|
||||
//~| ERROR E0308
|
||||
fn bap() -> Trait { Struct }
|
||||
//~^ ERROR E0746
|
||||
//~| ERROR E0308
|
||||
fn ban() -> dyn Trait { Struct }
|
||||
//~^ ERROR E0746
|
||||
//~| ERROR E0308
|
||||
fn bak() -> dyn Trait { unimplemented!() } //~ ERROR E0277
|
||||
// Suggest using `Box<dyn Trait>`
|
||||
fn bal() -> dyn Trait { //~ ERROR E0746
|
||||
if true {
|
||||
return Struct; //~ ERROR E0308
|
||||
}
|
||||
42 //~ ERROR E0308
|
||||
}
|
||||
|
||||
// Suggest using `impl Trait`
|
||||
fn bat() -> dyn Trait { //~ ERROR E0746
|
||||
if true {
|
||||
return 0; //~ ERROR E0308
|
||||
}
|
||||
42 //~ ERROR E0308
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
@ -0,0 +1,186 @@
|
|||
error[E0308]: mismatched types
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:7:35
|
||||
|
|
||||
LL | fn fuz() -> (usize, Trait) { (42, Struct) }
|
||||
| ^^^^^^ expected trait `Trait`, found struct `Struct`
|
||||
|
|
||||
= note: expected trait object `(dyn Trait + 'static)`
|
||||
found struct `Struct`
|
||||
|
||||
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:7:13
|
||||
|
|
||||
LL | fn fuz() -> (usize, Trait) { (42, Struct) }
|
||||
| ^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
||||
|
|
||||
= help: within `(usize, (dyn Trait + 'static))`, the trait `std::marker::Sized` is not implemented for `(dyn Trait + 'static)`
|
||||
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
|
||||
= note: required because it appears within the type `(usize, (dyn Trait + 'static))`
|
||||
= note: the return type of a function must have a statically known size
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:10:39
|
||||
|
|
||||
LL | fn bar() -> (usize, dyn Trait) { (42, Struct) }
|
||||
| ^^^^^^ expected trait `Trait`, found struct `Struct`
|
||||
|
|
||||
= note: expected trait object `(dyn Trait + 'static)`
|
||||
found struct `Struct`
|
||||
|
||||
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:10:13
|
||||
|
|
||||
LL | fn bar() -> (usize, dyn Trait) { (42, Struct) }
|
||||
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
||||
|
|
||||
= help: within `(usize, (dyn Trait + 'static))`, the trait `std::marker::Sized` is not implemented for `(dyn Trait + 'static)`
|
||||
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
|
||||
= note: required because it appears within the type `(usize, (dyn Trait + 'static))`
|
||||
= note: the return type of a function must have a statically known size
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:13:21
|
||||
|
|
||||
LL | fn bap() -> Trait { Struct }
|
||||
| ----- ^^^^^^ expected trait `Trait`, found struct `Struct`
|
||||
| |
|
||||
| expected `(dyn Trait + 'static)` because of return type
|
||||
|
|
||||
= note: expected trait object `(dyn Trait + 'static)`
|
||||
found struct `Struct`
|
||||
|
||||
error[E0746]: return type cannot have a bare trait because it must be `Sized`
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:13:13
|
||||
|
|
||||
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: you can use the `impl Trait` feature in the return type because all the return paths are of type `Struct`, which implements `dyn Trait`
|
||||
|
|
||||
LL | fn bap() -> impl Trait { Struct }
|
||||
| ^^^^^^^^^^
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:16:25
|
||||
|
|
||||
LL | fn ban() -> dyn Trait { Struct }
|
||||
| --------- ^^^^^^ expected trait `Trait`, found struct `Struct`
|
||||
| |
|
||||
| expected `(dyn Trait + 'static)` because of return type
|
||||
|
|
||||
= note: expected trait object `(dyn Trait + 'static)`
|
||||
found struct `Struct`
|
||||
|
||||
error[E0746]: return type cannot have a bare trait because it must be `Sized`
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:16:13
|
||||
|
|
||||
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: you can use the `impl Trait` feature in the return type because all the return paths are of type `Struct`, which implements `dyn Trait`
|
||||
|
|
||||
LL | fn ban() -> impl Trait { Struct }
|
||||
| ^^^^^^^^^^
|
||||
|
||||
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:19:13
|
||||
|
|
||||
LL | fn bak() -> dyn Trait { unimplemented!() }
|
||||
| ^^^^^^^^^ doesn't have a size known at compile-time
|
||||
|
|
||||
= help: the trait `std::marker::Sized` is not implemented for `(dyn Trait + 'static)`
|
||||
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
|
||||
= note: the return type of a function must have a statically known size
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:23:16
|
||||
|
|
||||
LL | fn bal() -> dyn Trait {
|
||||
| --------- expected `(dyn Trait + 'static)` because of return type
|
||||
LL | if true {
|
||||
LL | return Struct;
|
||||
| ^^^^^^ expected trait `Trait`, found struct `Struct`
|
||||
|
|
||||
= note: expected trait object `(dyn Trait + 'static)`
|
||||
found struct `Struct`
|
||||
|
||||
error[E0746]: return type cannot have a bare trait because it must be `Sized`
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:21:13
|
||||
|
|
||||
LL | fn bal() -> dyn Trait {
|
||||
| ^^^^^^^^^ doesn't have a size known at compile-time
|
||||
|
|
||||
help: if all the returned values were of the same type you could use `impl Trait` as the return type
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:25:5
|
||||
|
|
||||
LL | return Struct;
|
||||
| ^^^^^^
|
||||
LL | }
|
||||
LL | 42
|
||||
| ^^
|
||||
= help: alternatively, you can always create a new `enum` with a variant for each returned type
|
||||
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
|
||||
= 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>
|
||||
help: if the performance implications are acceptable, you can return a trait object
|
||||
|
|
||||
LL | fn bal() -> Box<dyn Trait> {
|
||||
LL | if true {
|
||||
LL | return Box::new(Struct);
|
||||
LL | }
|
||||
LL | Box::new(42)
|
||||
|
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:25:5
|
||||
|
|
||||
LL | fn bal() -> dyn Trait {
|
||||
| --------- expected `(dyn Trait + 'static)` because of return type
|
||||
...
|
||||
LL | 42
|
||||
| ^^ expected trait `Trait`, found integer
|
||||
|
|
||||
= note: expected trait object `(dyn Trait + 'static)`
|
||||
found type `{integer}`
|
||||
|
||||
error[E0746]: return type cannot have a bare trait because it must be `Sized`
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:29:13
|
||||
|
|
||||
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: you can use the `impl Trait` feature in the return type because all the return paths are of type `{integer}`, which implements `dyn Trait`
|
||||
|
|
||||
LL | fn bat() -> impl Trait {
|
||||
| ^^^^^^^^^^
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:31:16
|
||||
|
|
||||
LL | fn bat() -> dyn Trait {
|
||||
| --------- expected `(dyn Trait + 'static)` because of return type
|
||||
LL | if true {
|
||||
LL | return 0;
|
||||
| ^ expected trait `Trait`, found integer
|
||||
|
|
||||
= note: expected trait object `(dyn Trait + 'static)`
|
||||
found type `{integer}`
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:33:5
|
||||
|
|
||||
LL | fn bat() -> dyn Trait {
|
||||
| --------- expected `(dyn Trait + 'static)` because of return type
|
||||
...
|
||||
LL | 42
|
||||
| ^^ expected trait `Trait`, found integer
|
||||
|
|
||||
= note: expected trait object `(dyn Trait + 'static)`
|
||||
found type `{integer}`
|
||||
|
||||
error: aborting due to 15 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0277, E0308.
|
||||
For more information about an error, try `rustc --explain E0277`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue