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
On object safety errors, point at specific object unsafe trait in path
We use the new HIR node tracking for well-formedness obligations to
point at the specific trait object in a path/type (`dyn T` instead of a
whole `Box<dyn T>`, for example).
```
error[E0038]: the trait `MapLike` cannot be made into an object
--> $DIR/file.rs:47:16
|
LL | as Box<dyn Trait>;
| ^^^^^^^^^ `Trait` cannot be made into an object
```
We also provide a structured suggestion for `<dyn Trait>::assoc`:
```
= help: when writing `<dyn Trait>::function` you are requiring `Trait` be "object safe", which it isn't
help: you might have meant to access the associated function of a specific `impl` to avoid requiring "object safety" from `Trait`, either with some explicit type...
|
LL | </* Type */ as Trait>::function();
| ~~~~~~~~~~~~~
help: ...or rely on inference if the compiler has enough context to identify the desired type on its own...
|
LL - <dyn Trait>::function();
LL + Trait::function();
|
help: ...which is equivalent to
|
LL | <_ as Trait>::function();
| ~~~~
```
Copy file name to clipboardExpand all lines: tests/ui/async-await/in-trait/object-safety.stderr
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
error[E0038]: the trait `Foo` cannot be made into an object
2
-
--> $DIR/object-safety.rs:9:12
2
+
--> $DIR/object-safety.rs:9:13
3
3
|
4
4
LL | let x: &dyn Foo = todo!();
5
-
| ^^^^^^^^ `Foo` cannot be made into an object
5
+
| ^^^^^^^ `Foo` cannot be made into an object
6
6
|
7
7
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
= note: the trait cannot be made into an object because it requires `Self: Sized`
20
20
= note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
Copy file name to clipboardExpand all lines: tests/ui/dyn-keyword/trait-dyn-in-qualified-path.stderr
+14Lines changed: 14 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,20 @@ LL | trait Trait: Sized {
11
11
| ----- ^^^^^ ...because it requires `Self: Sized`
12
12
| |
13
13
| this trait cannot be made into an object...
14
+
= help: when writing `<dyn Trait>::function` you are requiring `Trait` be "object safe", which it isn't
15
+
help: you might have meant to access the associated function of a specific `impl` to avoid requiring "object safety" from `Trait`, either with some explicit type...
16
+
|
17
+
LL | </* Type */ as Trait>::function();
18
+
| ~~~~~~~~~~~~~
19
+
help: ...or rely on inference if the compiler has enough context to identify the desired type on its own...
20
+
|
21
+
LL - <dyn Trait>::function();
22
+
LL + Trait::function();
23
+
|
24
+
help: ...which is equivalent to
25
+
|
26
+
LL | <_ as Trait>::function();
27
+
| ~~~~
14
28
15
29
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
= note: the trait cannot be made into an object because it requires `Self: Sized`
22
22
= note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
23
+
note: `Default` is the type for the trait in editions 2015 and 2018 and is equivalent to writing `dyn Default`
= help: when writing `<dyn Default>::default` you are requiring `Default` be "object safe", which it isn't
29
+
help: you might have meant to access the associated function of a specific `impl` to avoid requiring "object safety" from `Default`, either with some explicit type...
30
+
|
31
+
LL | let x: u32 = </* Type */ as Default>::default();
32
+
| +++++++++++++
33
+
help: ...or rely on inference if the compiler has enough context to identify the desired type on its own...
34
+
|
35
+
LL - let x: u32 = <Default>::default();
36
+
LL + let x: u32 = Default::default();
37
+
|
38
+
help: ...which is equivalent to
39
+
|
40
+
LL | let x: u32 = <_ as Default>::default();
41
+
| ++++
23
42
24
43
error[E0277]: the size for values of type `dyn Default` cannot be known at compilation time
| ^^^^^^^^^^^^^^^^^^^^ `Default` cannot be made into an object
69
+
| ^^^^^^^ `Default` cannot be made into an object
51
70
|
52
71
= note: the trait cannot be made into an object because it requires `Self: Sized`
53
72
= note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
| --------- help: consider changing method `ptr`'s `self` parameter to be `&self`: `&Self`
6
6
...
7
7
LL | Ptr(Box::new(4)) as Ptr<dyn Trait>;
8
-
| ^^^^^^^^^^^^^^ `Trait` cannot be made into an object
8
+
| ^^^^^^^^^ `Trait` cannot be made into an object
9
9
|
10
10
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
0 commit comments