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 #120315 - estebank:issue-102629-2, r=wesleywiser
On E0308 involving `dyn Trait`, mention trait objects
When encountering a type mismatch error involving `dyn Trait`, mention the existence of boxed trait objects if the other type involved implements `Trait`.
Fix#102629.
Copy file name to clipboardexpand all lines: tests/ui/dst/dst-bad-assign-3.stderr
+1
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,7 @@ LL | f5.2 = Bar1 {f: 36};
8
8
|
9
9
= note: expected trait object `dyn ToBar`
10
10
found struct `Bar1`
11
+
= help: `Bar1` implements `ToBar` so you could box the found value and coerce it to the trait object `Box<dyn ToBar>`, you will have to change the expected type as well
11
12
12
13
error[E0277]: the size for values of type `dyn ToBar` cannot be known at compilation time
Copy file name to clipboardexpand all lines: tests/ui/dst/dst-bad-assign.stderr
+1
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,7 @@ LL | f5.ptr = Bar1 {f: 36};
8
8
|
9
9
= note: expected trait object `dyn ToBar`
10
10
found struct `Bar1`
11
+
= help: `Bar1` implements `ToBar` so you could box the found value and coerce it to the trait object `Box<dyn ToBar>`, you will have to change the expected type as well
11
12
12
13
error[E0277]: the size for values of type `dyn ToBar` cannot be known at compilation time
= help: `&u8` implements `RefCont` so you could box the found value and coerce it to the trait object `Box<dyn RefCont>`, you will have to change the expected type as well
30
31
= note: required for the cast from `Box<BTreeMap<u8, u8>>` to `Box<dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>>`
| ^^^^^ expected `Box<dyn Trait>`, found opaque type
9
+
|
10
+
= note: expected struct `Box<dyn Trait>`
11
+
found opaque type `impl Trait`
12
+
= note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
13
+
help: store this in the heap by calling `Box::new`
14
+
|
15
+
LL | Box::new(foo())
16
+
| +++++++++ +
17
+
18
+
error[E0308]: mismatched types
19
+
--> $DIR/dyn-impl-type-mismatch.rs:14:9
20
+
|
21
+
LL | Struct
22
+
| ^^^^^^ expected `dyn Trait`, found `Struct`
23
+
|
24
+
= note: expected trait object `dyn Trait`
25
+
found struct `Struct`
26
+
= help: `Struct` implements `Trait` so you could box the found value and coerce it to the trait object `Box<dyn Trait>`, you will have to change the expected type as well
27
+
28
+
error[E0308]: mismatched types
29
+
--> $DIR/dyn-impl-type-mismatch.rs:16:9
30
+
|
31
+
LL | fn foo() -> impl Trait {
32
+
| ---------- the found opaque type
33
+
...
34
+
LL | foo()
35
+
| ^^^^^ expected `dyn Trait`, found opaque type
36
+
|
37
+
= note: expected trait object `dyn Trait`
38
+
found opaque type `impl Trait`
39
+
= help: you can box the `impl Trait` to coerce it to `Box<dyn Trait>`, but you'll have to change the expected type as well
40
+
41
+
error: aborting due to 3 previous errors
42
+
43
+
For more information about this error, try `rustc --explain E0308`.
= help: `Struct` implements `Trait` so you could box the found value and coerce it to the trait object `Box<dyn Trait>`, you will have to change the expected type as well
9
10
10
11
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
= help: `Struct` implements `Trait` so you could box the found value and coerce it to the trait object `Box<dyn Trait>`, you will have to change the expected type as well
30
32
31
33
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
= help: `&'static str` implements `ToString` so you could box the found value and coerce it to the trait object `Box<dyn ToString>`, you will have to change the expected type as well
0 commit comments