You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardexpand all lines: tests/ui/generic-associated-types/gat-in-trait-path.base.stderr
+3
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,9 @@ LL | trait Foo {
12
12
LL | type A<'a> where Self: 'a;
13
13
| ^ ...because it contains the generic associated type `A`
14
14
= help: consider moving `A` to another trait
15
+
= 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:
Copy file name to clipboardexpand all lines: tests/ui/generic-associated-types/issue-76535.base.stderr
+4
Original file line number
Diff line number
Diff line change
@@ -28,6 +28,8 @@ LL | pub trait SuperTrait {
28
28
LL | type SubType<'a>: SubTrait where Self: 'a;
29
29
| ^^^^^^^ ...because it contains the generic associated type `SubType`
30
30
= help: consider moving `SubType` to another trait
31
+
= help: only type `SuperStruct` is seen to implement the trait in this crate, consider using it directly instead
32
+
= 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
31
33
32
34
error[E0038]: the trait `SuperTrait` cannot be made into an object
33
35
--> $DIR/issue-76535.rs:39:57
@@ -43,6 +45,8 @@ LL | pub trait SuperTrait {
43
45
LL | type SubType<'a>: SubTrait where Self: 'a;
44
46
| ^^^^^^^ ...because it contains the generic associated type `SubType`
45
47
= help: consider moving `SubType` to another trait
48
+
= help: only type `SuperStruct` is seen to implement the trait in this crate, consider using it directly instead
49
+
= 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
46
50
= note: required for the cast from `Box<SuperStruct>` to `Box<dyn SuperTrait<SubType = SubStruct<'_>>>`
Copy file name to clipboardexpand all lines: tests/ui/generic-associated-types/issue-79422.base.stderr
+6
Original file line number
Diff line number
Diff line change
@@ -28,6 +28,9 @@ LL | trait MapLike<K, V> {
28
28
LL | type VRefCont<'a>: RefCont<'a, V> where Self: 'a;
29
29
| ^^^^^^^^ ...because it contains the generic associated type `VRefCont`
30
30
= help: consider moving `VRefCont` to another trait
31
+
= 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:
32
+
std::collections::BTreeMap<K, V>
33
+
Source
31
34
32
35
error[E0038]: the trait `MapLike` cannot be made into an object
33
36
--> $DIR/issue-79422.rs:44:13
@@ -43,6 +46,9 @@ LL | trait MapLike<K, V> {
43
46
LL | type VRefCont<'a>: RefCont<'a, V> where Self: 'a;
44
47
| ^^^^^^^^ ...because it contains the generic associated type `VRefCont`
45
48
= help: consider moving `VRefCont` to another trait
49
+
= 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:
50
+
std::collections::BTreeMap<K, V>
51
+
Source
46
52
= note: required for the cast from `Box<BTreeMap<u8, u8>>` to `Box<dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>>`
Copy file name to clipboardexpand all lines: tests/ui/impl-trait/object-unsafe-trait-in-return-position-dyn-trait.stderr
+6
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,9 @@ LL | trait NotObjectSafe {
11
11
| ------------- this trait cannot be made into an object...
12
12
LL | fn foo() -> Self;
13
13
| ^^^ ...because associated function `foo` has no `self` parameter
14
+
= 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:
15
+
A
16
+
B
14
17
help: consider turning `foo` into a method by giving it a `&self` argument
15
18
|
16
19
LL | fn foo(&self) -> Self;
@@ -33,6 +36,9 @@ LL | trait NotObjectSafe {
33
36
| ------------- this trait cannot be made into an object...
34
37
LL | fn foo() -> Self;
35
38
| ^^^ ...because associated function `foo` has no `self` parameter
39
+
= 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:
40
+
A
41
+
B
36
42
help: consider turning `foo` into a method by giving it a `&self` argument
= 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:
46
+
i32
47
+
u32
45
48
46
49
error[E0038]: the trait `bar` 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 `bar` for this new enum and using it instead:
66
+
i32
67
+
u32
62
68
63
69
error[E0038]: the trait `bar` 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 `bar` for this new enum and using it instead:
86
+
i32
87
+
u32
79
88
= note: required for the cast from `Box<{integer}>` to `Box<dyn bar>`
0 commit comments