Rollup merge of #117132 - estebank:issue-80194, r=petrochenkov
On object safety error, mention new enum as alternative When we encounter a `dyn Trait` that isn't object safe, look for its implementors. If there's one, mention using it directly If there are less than 9, mention the possibility of creating a new enum and using that instead. Fix #80194.
This commit is contained in:
commit
95de91b5ce
25 changed files with 130 additions and 1 deletions
|
|
@ -15,6 +15,7 @@ LL | fn test(&self) -> [u8; bar::<Self>()];
|
|||
| ...because method `test` references the `Self` type in its `where` clause
|
||||
= help: consider moving `test` to another trait
|
||||
= help: consider moving `test` to another trait
|
||||
= help: only type `()` implements the trait, consider using it directly instead
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ LL | trait Trait {
|
|||
| ----- this trait cannot be made into an object...
|
||||
LL | fn ptr(self: Ptr<Self>);
|
||||
| ^^^^^^^^^ ...because method `ptr`'s `self` parameter cannot be dispatched on
|
||||
= help: only type `i32` implements the trait, consider using it directly instead
|
||||
|
||||
error[E0038]: the trait `Trait` cannot be made into an object
|
||||
--> $DIR/feature-gate-dispatch-from-dyn-missing-impl.rs:32:5
|
||||
|
|
@ -31,6 +32,7 @@ LL | trait Trait {
|
|||
| ----- this trait cannot be made into an object...
|
||||
LL | fn ptr(self: Ptr<Self>);
|
||||
| ^^^^^^^^^ ...because method `ptr`'s `self` parameter cannot be dispatched on
|
||||
= help: only type `i32` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `Ptr<{integer}>` to `Ptr<dyn Trait>`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
|
|
|||
|
|
@ -12,6 +12,9 @@ LL | trait Foo {
|
|||
LL | type A<'a> where Self: 'a;
|
||||
| ^ ...because it contains the generic associated type `A`
|
||||
= help: consider moving `A` to another trait
|
||||
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `Foo` for this new enum and using it instead:
|
||||
Fooy
|
||||
Fooer<T>
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ LL | pub trait SuperTrait {
|
|||
LL | type SubType<'a>: SubTrait where Self: 'a;
|
||||
| ^^^^^^^ ...because it contains the generic associated type `SubType`
|
||||
= help: consider moving `SubType` to another trait
|
||||
= help: only type `SuperStruct` is seen to implement the trait in this crate, consider using it directly instead
|
||||
= note: `SuperTrait` can be implemented in other crates; if you want to support your users passing their own types here, you can't refer to a specific type
|
||||
|
||||
error[E0038]: the trait `SuperTrait` cannot be made into an object
|
||||
--> $DIR/issue-76535.rs:39:57
|
||||
|
|
@ -43,6 +45,8 @@ LL | pub trait SuperTrait {
|
|||
LL | type SubType<'a>: SubTrait where Self: 'a;
|
||||
| ^^^^^^^ ...because it contains the generic associated type `SubType`
|
||||
= help: consider moving `SubType` to another trait
|
||||
= help: only type `SuperStruct` is seen to implement the trait in this crate, consider using it directly instead
|
||||
= note: `SuperTrait` can be implemented in other crates; if you want to support your users passing their own types here, you can't refer to a specific type
|
||||
= note: required for the cast from `Box<SuperStruct>` to `Box<dyn SuperTrait<SubType = SubStruct<'_>>>`
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
|
|
|||
|
|
@ -28,6 +28,9 @@ LL | trait MapLike<K, V> {
|
|||
LL | type VRefCont<'a>: RefCont<'a, V> where Self: 'a;
|
||||
| ^^^^^^^^ ...because it contains the generic associated type `VRefCont`
|
||||
= help: consider moving `VRefCont` to another trait
|
||||
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `MapLike` for this new enum and using it instead:
|
||||
std::collections::BTreeMap<K, V>
|
||||
Source
|
||||
|
||||
error[E0038]: the trait `MapLike` cannot be made into an object
|
||||
--> $DIR/issue-79422.rs:44:13
|
||||
|
|
@ -43,6 +46,9 @@ LL | trait MapLike<K, V> {
|
|||
LL | type VRefCont<'a>: RefCont<'a, V> where Self: 'a;
|
||||
| ^^^^^^^^ ...because it contains the generic associated type `VRefCont`
|
||||
= help: consider moving `VRefCont` to another trait
|
||||
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `MapLike` for this new enum and using it instead:
|
||||
std::collections::BTreeMap<K, V>
|
||||
Source
|
||||
= note: required for the cast from `Box<BTreeMap<u8, u8>>` to `Box<dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>>`
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ note: for a trait to be "object safe" it needs to allow building a vtable to all
|
|||
|
|
||||
LL | fn bar(self) -> impl Deref<Target = impl Sized>;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait cannot be made into an object because method `bar` references an `impl Trait` type in its return type
|
||||
= help: only type `rpitit::Foreign` implements the trait, consider using it directly instead
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ LL | trait Foo {
|
|||
LL | fn baz(&self) -> impl Debug;
|
||||
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
|
||||
= help: consider moving `baz` to another trait
|
||||
= help: only type `u32` implements the trait, consider using it directly instead
|
||||
|
||||
error[E0038]: the trait `Foo` cannot be made into an object
|
||||
--> $DIR/object-safety.rs:17:15
|
||||
|
|
@ -27,6 +28,7 @@ LL | trait Foo {
|
|||
LL | fn baz(&self) -> impl Debug;
|
||||
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
|
||||
= help: consider moving `baz` to another trait
|
||||
= help: only type `u32` implements the trait, consider using it directly instead
|
||||
|
||||
error[E0038]: the trait `Foo` cannot be made into an object
|
||||
--> $DIR/object-safety.rs:17:13
|
||||
|
|
@ -42,6 +44,7 @@ LL | trait Foo {
|
|||
LL | fn baz(&self) -> impl Debug;
|
||||
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
|
||||
= help: consider moving `baz` to another trait
|
||||
= help: only type `u32` implements the trait, consider using it directly instead
|
||||
|
||||
error[E0038]: the trait `Foo` cannot be made into an object
|
||||
--> $DIR/object-safety.rs:14:13
|
||||
|
|
@ -57,6 +60,7 @@ LL | trait Foo {
|
|||
LL | fn baz(&self) -> impl Debug;
|
||||
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
|
||||
= help: consider moving `baz` to another trait
|
||||
= help: only type `u32` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `Box<u32>` to `Box<dyn Foo>`
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@ LL | trait NotObjectSafe {
|
|||
| ------------- this trait cannot be made into an object...
|
||||
LL | fn foo() -> Self;
|
||||
| ^^^ ...because associated function `foo` has no `self` parameter
|
||||
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `NotObjectSafe` for this new enum and using it instead:
|
||||
A
|
||||
B
|
||||
help: consider turning `foo` into a method by giving it a `&self` argument
|
||||
|
|
||||
LL | fn foo(&self) -> Self;
|
||||
|
|
@ -33,6 +36,9 @@ LL | trait NotObjectSafe {
|
|||
| ------------- this trait cannot be made into an object...
|
||||
LL | fn foo() -> Self;
|
||||
| ^^^ ...because associated function `foo` has no `self` parameter
|
||||
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `NotObjectSafe` for this new enum and using it instead:
|
||||
A
|
||||
B
|
||||
help: consider turning `foo` into a method by giving it a `&self` argument
|
||||
|
|
||||
LL | fn foo(&self) -> Self;
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ LL | trait Qiz {
|
|||
| --- this trait cannot be made into an object...
|
||||
LL | fn qiz();
|
||||
| ^^^ ...because associated function `qiz` has no `self` parameter
|
||||
= help: only type `Foo` implements the trait, consider using it directly instead
|
||||
help: consider turning `qiz` into a method by giving it a `&self` argument
|
||||
|
|
||||
LL | fn qiz(&self);
|
||||
|
|
@ -33,6 +34,7 @@ LL | trait Qiz {
|
|||
| --- this trait cannot be made into an object...
|
||||
LL | fn qiz();
|
||||
| ^^^ ...because associated function `qiz` has no `self` parameter
|
||||
= help: only type `Foo` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `&Foo` to `&'static (dyn Qiz + 'static)`
|
||||
help: consider turning `qiz` into a method by giving it a `&self` argument
|
||||
|
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ LL | fn foo<T>(&self, val: T);
|
|||
LL | trait Bar: Foo { }
|
||||
| --- this trait cannot be made into an object...
|
||||
= help: consider moving `foo` to another trait
|
||||
= help: only type `Thing` implements the trait, consider using it directly instead
|
||||
|
||||
error[E0038]: the trait `Bar` cannot be made into an object
|
||||
--> $DIR/issue-19538.rs:17:30
|
||||
|
|
@ -29,6 +30,7 @@ LL | fn foo<T>(&self, val: T);
|
|||
LL | trait Bar: Foo { }
|
||||
| --- this trait cannot be made into an object...
|
||||
= help: consider moving `foo` to another trait
|
||||
= help: only type `Thing` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `&mut Thing` to `&mut dyn Bar`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ LL | trait Expr: Debug + PartialEq {
|
|||
| ---- ^^^^^^^^^ ...because it uses `Self` as a type parameter
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `SExpr<'x>` implements the trait, consider using it directly instead
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ LL | trait Foo {
|
|||
| --- this trait cannot be made into an object...
|
||||
LL | fn foo() {}
|
||||
| ^^^ ...because associated function `foo` has no `self` parameter
|
||||
= help: only type `Bar` implements the trait, consider using it directly instead
|
||||
help: consider turning `foo` into a method by giving it a `&self` argument
|
||||
|
|
||||
LL | fn foo(&self) {}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ LL | trait Foo {
|
|||
| --- this trait cannot be made into an object...
|
||||
LL | fn foo() {}
|
||||
| ^^^ ...because associated function `foo` has no `self` parameter
|
||||
= help: only type `Bar` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `Box<Bar>` to `Box<dyn Foo>`
|
||||
help: consider turning `foo` into a method by giving it a `&self` argument
|
||||
|
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ LL | trait Foo {
|
|||
| --- this trait cannot be made into an object...
|
||||
LL | fn foo(self: &Rc<Self>) -> usize;
|
||||
| ^^^^^^^^^ ...because method `foo`'s `self` parameter cannot be dispatched on
|
||||
= help: only type `usize` implements the trait, consider using it directly instead
|
||||
|
||||
error[E0038]: the trait `Foo` cannot be made into an object
|
||||
--> $DIR/arbitrary-self-types-not-object-safe.rs:33:13
|
||||
|
|
@ -31,6 +32,7 @@ LL | trait Foo {
|
|||
| --- this trait cannot be made into an object...
|
||||
LL | fn foo(self: &Rc<Self>) -> usize;
|
||||
| ^^^^^^^^^ ...because method `foo`'s `self` parameter cannot be dispatched on
|
||||
= help: only type `usize` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `Rc<usize>` to `Rc<dyn Foo>`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ LL | trait Foo {
|
|||
| --- this trait cannot be made into an object...
|
||||
LL | fn foo(self: &Rc<Self>) -> usize;
|
||||
| ^^^^^^^^^ ...because method `foo`'s `self` parameter cannot be dispatched on
|
||||
= help: only type `usize` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `Rc<usize>` to `Rc<dyn Foo>`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ LL | trait Foo where u32: Q<Self> {
|
|||
| --- ^^^^^^^ ...because it uses `Self` as a type parameter
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `()` implements the trait, consider using it directly instead
|
||||
|
||||
error[E0038]: the trait `Foo` cannot be made into an object
|
||||
--> $DIR/issue-38604.rs:15:9
|
||||
|
|
@ -25,6 +26,7 @@ LL | trait Foo where u32: Q<Self> {
|
|||
| --- ^^^^^^^ ...because it uses `Self` as a type parameter
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `()` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `Box<()>` to `Box<dyn Foo>`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
|
|
|||
|
|
@ -143,6 +143,7 @@ LL | const C: u8 = 0;
|
|||
= help: consider moving `C` to another trait
|
||||
= help: consider moving `A` to another trait
|
||||
= help: consider moving `B` to another trait
|
||||
= help: only type `S` implements the trait, consider using it directly instead
|
||||
|
||||
error[E0223]: ambiguous associated type
|
||||
--> $DIR/item-privacy.rs:115:12
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ LL | trait Foo: for<T> Bar<T> {}
|
|||
| --- ^^^^^^^^^^^^^ ...because where clause cannot reference non-lifetime `for<...>` variables
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `()` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `&()` to `&dyn Foo`
|
||||
|
||||
error[E0038]: the trait `Foo` cannot be made into an object
|
||||
|
|
@ -35,6 +36,7 @@ LL | trait Foo: for<T> Bar<T> {}
|
|||
| --- ^^^^^^^^^^^^^ ...because where clause cannot reference non-lifetime `for<...>` variables
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `()` implements the trait, consider using it directly instead
|
||||
|
||||
error[E0038]: the trait `Foo` cannot be made into an object
|
||||
--> $DIR/supertrait-object-safety.rs:22:5
|
||||
|
|
@ -49,6 +51,7 @@ LL | trait Foo: for<T> Bar<T> {}
|
|||
| --- ^^^^^^^^^^^^^ ...because where clause cannot reference non-lifetime `for<...>` variables
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `()` implements the trait, consider using it directly instead
|
||||
|
||||
error: aborting due to 3 previous errors; 1 warning emitted
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ LL | trait Tr {
|
|||
| -- this trait cannot be made into an object...
|
||||
LL | fn foo();
|
||||
| ^^^ ...because associated function `foo` has no `self` parameter
|
||||
= help: only type `St` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `&St` to `&dyn Tr`
|
||||
help: consider turning `foo` into a method by giving it a `&self` argument
|
||||
|
|
||||
|
|
@ -34,6 +35,7 @@ LL | trait Tr {
|
|||
| -- this trait cannot be made into an object...
|
||||
LL | fn foo();
|
||||
| ^^^ ...because associated function `foo` has no `self` parameter
|
||||
= help: only type `St` implements the trait, consider using it directly instead
|
||||
help: consider turning `foo` into a method by giving it a `&self` argument
|
||||
|
|
||||
LL | fn foo(&self);
|
||||
|
|
|
|||
|
|
@ -42,6 +42,9 @@ LL | trait bar { fn dup(&self) -> Self; fn blah<X>(&self); }
|
|||
| this trait cannot be made into an object...
|
||||
= help: consider moving `dup` to another trait
|
||||
= help: consider moving `blah` to another trait
|
||||
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `bar` for this new enum and using it instead:
|
||||
i32
|
||||
u32
|
||||
|
||||
error[E0038]: the trait `bar` cannot be made into an object
|
||||
--> $DIR/test-2.rs:13:5
|
||||
|
|
@ -59,6 +62,9 @@ LL | trait bar { fn dup(&self) -> Self; fn blah<X>(&self); }
|
|||
| this trait cannot be made into an object...
|
||||
= help: consider moving `dup` to another trait
|
||||
= help: consider moving `blah` to another trait
|
||||
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `bar` for this new enum and using it instead:
|
||||
i32
|
||||
u32
|
||||
|
||||
error[E0038]: the trait `bar` cannot be made into an object
|
||||
--> $DIR/test-2.rs:13:6
|
||||
|
|
@ -76,6 +82,9 @@ LL | trait bar { fn dup(&self) -> Self; fn blah<X>(&self); }
|
|||
| this trait cannot be made into an object...
|
||||
= help: consider moving `dup` to another trait
|
||||
= help: consider moving `blah` to another trait
|
||||
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `bar` for this new enum and using it instead:
|
||||
i32
|
||||
u32
|
||||
= note: required for the cast from `Box<{integer}>` to `Box<dyn bar>`
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ LL | trait MyAdd<Rhs=Self> { fn add(&self, other: &Rhs) -> Self; }
|
|||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: consider moving `add` to another trait
|
||||
= help: only type `i32` implements the trait, consider using it directly instead
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ LL | trait Trait: Sized {}
|
|||
| ----- ^^^^^ ...because it requires `Self: Sized`
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `S` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `Box<S>` to `Box<dyn Trait>`
|
||||
|
||||
error[E0038]: the trait `Trait` cannot be made into an object
|
||||
|
|
@ -26,6 +27,7 @@ LL | trait Trait: Sized {}
|
|||
| ----- ^^^^^ ...because it requires `Self: Sized`
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `S` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `Box<S>` to `Box<(dyn Trait + 'static)>`
|
||||
|
||||
error[E0038]: the trait `Trait` cannot be made into an object
|
||||
|
|
@ -41,6 +43,7 @@ LL | trait Trait: Sized {}
|
|||
| ----- ^^^^^ ...because it requires `Self: Sized`
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `S` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `Box<S>` to `Box<dyn Trait>`
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ LL | trait Trait: Sized {}
|
|||
| ----- ^^^^^ ...because it requires `Self: Sized`
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `S` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `&S` to `&dyn Trait`
|
||||
|
||||
error[E0038]: the trait `Trait` cannot be made into an object
|
||||
|
|
@ -26,6 +27,7 @@ LL | trait Trait: Sized {}
|
|||
| ----- ^^^^^ ...because it requires `Self: Sized`
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `S` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `&S` to `&dyn Trait`
|
||||
|
||||
error[E0038]: the trait `Trait` cannot be made into an object
|
||||
|
|
@ -41,6 +43,7 @@ LL | trait Trait: Sized {}
|
|||
| ----- ^^^^^ ...because it requires `Self: Sized`
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: only type `S` implements the trait, consider using it directly instead
|
||||
= note: required for the cast from `&S` to `&dyn Trait`
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
|
|
|||
|
|
@ -29,6 +29,9 @@ LL | trait Trait: Sized {}
|
|||
| ----- ^^^^^ ...because it requires `Self: Sized`
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `Trait` for this new enum and using it instead:
|
||||
S
|
||||
R
|
||||
= note: required for the cast from `&S` to `&dyn Trait`
|
||||
|
||||
error[E0038]: the trait `Trait` cannot be made into an object
|
||||
|
|
@ -48,6 +51,9 @@ LL | trait Trait: Sized {}
|
|||
| ----- ^^^^^ ...because it requires `Self: Sized`
|
||||
| |
|
||||
| this trait cannot be made into an object...
|
||||
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `Trait` for this new enum and using it instead:
|
||||
S
|
||||
R
|
||||
= note: required for the cast from `&R` to `&dyn Trait`
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue