]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0782.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0782.md
1 Trait objects must include the `dyn` keyword.
2
3 Erroneous code example:
4
5 ```edition2021,compile_fail,E0782
6 trait Foo {}
7 fn test(arg: Box<Foo>) {} // error!
8 ```
9
10 Trait objects are a way to call methods on types that are not known until
11 runtime but conform to some trait.
12
13 Trait objects should be formed with `Box<dyn Foo>`, but in the code above
14 `dyn` is left off.
15
16 This makes it harder to see that `arg` is a trait object and not a
17 simply a heap allocated type called `Foo`.
18
19 To fix this issue, add `dyn` before the trait name.
20
21 ```edition2021
22 trait Foo {}
23 fn test(arg: Box<dyn Foo>) {} // ok!
24 ```
25
26 This used to be allowed before edition 2021, but is now an error.