]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0746.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0746.md
1 An unboxed trait object was used as a return value.
2
3 Erroneous code example:
4
5 ```compile_fail,E0746
6 trait T {
7     fn bar(&self);
8 }
9 struct S(usize);
10 impl T for S {
11     fn bar(&self) {}
12 }
13
14 // Having the trait `T` as return type is invalid because
15 // unboxed trait objects do not have a statically known size:
16 fn foo() -> dyn T { // error!
17     S(42)
18 }
19 ```
20
21 Return types cannot be `dyn Trait`s as they must be `Sized`.
22
23 To avoid the error there are a couple of options.
24
25 If there is a single type involved, you can use [`impl Trait`]:
26
27 ```
28 # trait T {
29 #     fn bar(&self);
30 # }
31 # struct S(usize);
32 # impl T for S {
33 #     fn bar(&self) {}
34 # }
35 // The compiler will select `S(usize)` as the materialized return type of this
36 // function, but callers will only know that the return type implements `T`.
37 fn foo() -> impl T { // ok!
38     S(42)
39 }
40 ```
41
42 If there are multiple types involved, the only way you care to interact with
43 them is through the trait's interface, and having to rely on dynamic dispatch
44 is acceptable, then you can use [trait objects] with `Box`, or other container
45 types like `Rc` or `Arc`:
46
47 ```
48 # trait T {
49 #     fn bar(&self);
50 # }
51 # struct S(usize);
52 # impl T for S {
53 #     fn bar(&self) {}
54 # }
55 struct O(&'static str);
56 impl T for O {
57     fn bar(&self) {}
58 }
59
60 // This now returns a "trait object" and callers are only be able to access
61 // associated items from `T`.
62 fn foo(x: bool) -> Box<dyn T> { // ok!
63     if x {
64         Box::new(S(42))
65     } else {
66         Box::new(O("val"))
67     }
68 }
69 ```
70
71 Finally, if you wish to still be able to access the original type, you can
72 create a new `enum` with a variant for each type:
73
74 ```
75 # trait T {
76 #     fn bar(&self);
77 # }
78 # struct S(usize);
79 # impl T for S {
80 #     fn bar(&self) {}
81 # }
82 # struct O(&'static str);
83 # impl T for O {
84 #     fn bar(&self) {}
85 # }
86 enum E {
87     S(S),
88     O(O),
89 }
90
91 // The caller can access the original types directly, but it needs to match on
92 // the returned `enum E`.
93 fn foo(x: bool) -> E {
94     if x {
95         E::S(S(42))
96     } else {
97         E::O(O("val"))
98     }
99 }
100 ```
101
102 You can even implement the `trait` on the returned `enum` so the callers
103 *don't* have to match on the returned value to invoke the associated items:
104
105 ```
106 # trait T {
107 #     fn bar(&self);
108 # }
109 # struct S(usize);
110 # impl T for S {
111 #     fn bar(&self) {}
112 # }
113 # struct O(&'static str);
114 # impl T for O {
115 #     fn bar(&self) {}
116 # }
117 # enum E {
118 #     S(S),
119 #     O(O),
120 # }
121 impl T for E {
122     fn bar(&self) {
123         match self {
124             E::S(s) => s.bar(),
125             E::O(o) => o.bar(),
126         }
127     }
128 }
129 ```
130
131 If you decide to use trait objects, be aware that these rely on
132 [dynamic dispatch], which has performance implications, as the compiler needs
133 to emit code that will figure out which method to call *at runtime* instead of
134 during compilation. Using trait objects we are trading flexibility for
135 performance.
136
137 [`impl Trait`]: https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits
138 [trait objects]: https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types
139 [dynamic dispatch]: https://doc.rust-lang.org/book/ch17-02-trait-objects.html#trait-objects-perform-dynamic-dispatch