]> git.lizzy.rs Git - rust.git/blob - tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr
Fix #106496, suggest remove deref for type mismatch
[rust.git] / tests / ui / impl-trait / dyn-trait-return-should-be-impl-trait.stderr
1 error[E0308]: mismatched types
2   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:7:35
3    |
4 LL | fn fuz() -> (usize, Trait) { (42, Struct) }
5    |                                   ^^^^^^ expected trait object `dyn Trait`, found struct `Struct`
6    |
7    = note: expected trait object `(dyn Trait + 'static)`
8                     found struct `Struct`
9
10 error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
11   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:7:13
12    |
13 LL | fn fuz() -> (usize, Trait) { (42, Struct) }
14    |             ^^^^^^^^^^^^^^   ------------ this returned value is of type `(usize, (dyn Trait + 'static))`
15    |             |
16    |             doesn't have a size known at compile-time
17    |
18    = help: within `(usize, (dyn Trait + 'static))`, the trait `Sized` is not implemented for `(dyn Trait + 'static)`
19    = note: required because it appears within the type `(usize, (dyn Trait + 'static))`
20    = note: the return type of a function must have a statically known size
21
22 error[E0308]: mismatched types
23   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:10:39
24    |
25 LL | fn bar() -> (usize, dyn Trait) { (42, Struct) }
26    |                                       ^^^^^^ expected trait object `dyn Trait`, found struct `Struct`
27    |
28    = note: expected trait object `(dyn Trait + 'static)`
29                     found struct `Struct`
30
31 error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
32   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:10:13
33    |
34 LL | fn bar() -> (usize, dyn Trait) { (42, Struct) }
35    |             ^^^^^^^^^^^^^^^^^^   ------------ this returned value is of type `(usize, (dyn Trait + 'static))`
36    |             |
37    |             doesn't have a size known at compile-time
38    |
39    = help: within `(usize, (dyn Trait + 'static))`, the trait `Sized` is not implemented for `(dyn Trait + 'static)`
40    = note: required because it appears within the type `(usize, (dyn Trait + 'static))`
41    = note: the return type of a function must have a statically known size
42
43 error[E0746]: return type cannot have an unboxed trait object
44   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:13:13
45    |
46 LL | fn bap() -> Trait { Struct }
47    |             ^^^^^ doesn't have a size known at compile-time
48    |
49    = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
50 help: use `impl Trait` as the return type, as all return paths are of type `Struct`, which implements `Trait`
51    |
52 LL | fn bap() -> impl Trait { Struct }
53    |             ~~~~~~~~~~
54
55 error[E0746]: return type cannot have an unboxed trait object
56   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:15:13
57    |
58 LL | fn ban() -> dyn Trait { Struct }
59    |             ^^^^^^^^^ doesn't have a size known at compile-time
60    |
61    = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
62 help: use `impl Trait` as the return type, as all return paths are of type `Struct`, which implements `Trait`
63    |
64 LL | fn ban() -> impl Trait { Struct }
65    |             ~~~~~~~~~~
66
67 error[E0746]: return type cannot have an unboxed trait object
68   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:17:13
69    |
70 LL | fn bak() -> dyn Trait { unimplemented!() }
71    |             ^^^^^^^^^ doesn't have a size known at compile-time
72    |
73 help: use `impl Trait` as the return type if all return paths have the same type but you want to expose only the trait in the signature
74    |
75 LL | fn bak() -> impl Trait { unimplemented!() }
76    |             ~~~~~~~~~~
77 help: use a boxed trait object if all return paths implement trait `Trait`
78    |
79 LL | fn bak() -> Box<dyn Trait> { unimplemented!() }
80    |             ++++         +
81
82 error[E0746]: return type cannot have an unboxed trait object
83   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:19:13
84    |
85 LL | fn bal() -> dyn Trait {
86    |             ^^^^^^^^^ doesn't have a size known at compile-time
87    |
88    = note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
89    = note: if all the returned values were of the same type you could use `impl Trait` as the return type
90    = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
91    = note: you can create a new `enum` with a variant for each returned type
92 help: return a boxed trait object instead
93    |
94 LL | fn bal() -> Box<dyn Trait> {
95    |             ++++         +
96 help: ... and box this value
97    |
98 LL |         return Box::new(Struct);
99    |                +++++++++      +
100 help: ... and box this value
101    |
102 LL |     Box::new(42)
103    |     +++++++++  +
104
105 error[E0308]: `if` and `else` have incompatible types
106   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:29:9
107    |
108 LL | /     if true {
109 LL | |         Struct
110    | |         ------ expected because of this
111 LL | |     } else {
112 LL | |         42
113    | |         ^^ expected struct `Struct`, found integer
114 LL | |     }
115    | |_____- `if` and `else` have incompatible types
116
117 error[E0746]: return type cannot have an unboxed trait object
118   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:25:13
119    |
120 LL | fn bax() -> dyn Trait {
121    |             ^^^^^^^^^ doesn't have a size known at compile-time
122    |
123    = note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
124    = note: if all the returned values were of the same type you could use `impl Trait` as the return type
125    = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
126    = note: you can create a new `enum` with a variant for each returned type
127 help: return a boxed trait object instead
128    |
129 LL | fn bax() -> Box<dyn Trait> {
130    |             ++++         +
131 help: ... and box this value
132    |
133 LL |         Box::new(Struct)
134    |         +++++++++      +
135 help: ... and box this value
136    |
137 LL |         Box::new(42)
138    |         +++++++++  +
139
140 error[E0308]: mismatched types
141   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:34:16
142    |
143 LL | fn bam() -> Box<dyn Trait> {
144    |             -------------- expected `Box<(dyn Trait + 'static)>` because of return type
145 LL |     if true {
146 LL |         return Struct;
147    |                ^^^^^^ expected struct `Box`, found struct `Struct`
148    |
149    = note: expected struct `Box<(dyn Trait + 'static)>`
150               found struct `Struct`
151    = 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
152 help: store this in the heap by calling `Box::new`
153    |
154 LL |         return Box::new(Struct);
155    |                +++++++++      +
156
157 error[E0308]: mismatched types
158   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:36:5
159    |
160 LL | fn bam() -> Box<dyn Trait> {
161    |             -------------- expected `Box<(dyn Trait + 'static)>` because of return type
162 ...
163 LL |     42
164    |     ^^ expected struct `Box`, found integer
165    |
166    = note: expected struct `Box<(dyn Trait + 'static)>`
167                 found type `{integer}`
168    = 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
169 help: store this in the heap by calling `Box::new`
170    |
171 LL |     Box::new(42)
172    |     +++++++++  +
173
174 error[E0308]: mismatched types
175   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:40:16
176    |
177 LL | fn baq() -> Box<dyn Trait> {
178    |             -------------- expected `Box<(dyn Trait + 'static)>` because of return type
179 LL |     if true {
180 LL |         return 0;
181    |                ^ expected struct `Box`, found integer
182    |
183    = note: expected struct `Box<(dyn Trait + 'static)>`
184                 found type `{integer}`
185    = 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
186 help: store this in the heap by calling `Box::new`
187    |
188 LL |         return Box::new(0);
189    |                +++++++++ +
190
191 error[E0308]: mismatched types
192   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:42:5
193    |
194 LL | fn baq() -> Box<dyn Trait> {
195    |             -------------- expected `Box<(dyn Trait + 'static)>` because of return type
196 ...
197 LL |     42
198    |     ^^ expected struct `Box`, found integer
199    |
200    = note: expected struct `Box<(dyn Trait + 'static)>`
201                 found type `{integer}`
202    = 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
203 help: store this in the heap by calling `Box::new`
204    |
205 LL |     Box::new(42)
206    |     +++++++++  +
207
208 error[E0308]: mismatched types
209   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:46:9
210    |
211 LL | fn baz() -> Box<dyn Trait> {
212    |             -------------- expected `Box<(dyn Trait + 'static)>` because of return type
213 LL |     if true {
214 LL |         Struct
215    |         ^^^^^^ expected struct `Box`, found struct `Struct`
216    |
217    = note: expected struct `Box<(dyn Trait + 'static)>`
218               found struct `Struct`
219    = 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
220 help: store this in the heap by calling `Box::new`
221    |
222 LL |         Box::new(Struct)
223    |         +++++++++      +
224
225 error[E0308]: mismatched types
226   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:48:9
227    |
228 LL | fn baz() -> Box<dyn Trait> {
229    |             -------------- expected `Box<(dyn Trait + 'static)>` because of return type
230 ...
231 LL |         42
232    |         ^^ expected struct `Box`, found integer
233    |
234    = note: expected struct `Box<(dyn Trait + 'static)>`
235                 found type `{integer}`
236    = 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
237 help: store this in the heap by calling `Box::new`
238    |
239 LL |         Box::new(42)
240    |         +++++++++  +
241
242 error[E0308]: mismatched types
243   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:53:9
244    |
245 LL | fn baw() -> Box<dyn Trait> {
246    |             -------------- expected `Box<(dyn Trait + 'static)>` because of return type
247 LL |     if true {
248 LL |         0
249    |         ^ expected struct `Box`, found integer
250    |
251    = note: expected struct `Box<(dyn Trait + 'static)>`
252                 found type `{integer}`
253    = 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
254 help: store this in the heap by calling `Box::new`
255    |
256 LL |         Box::new(0)
257    |         +++++++++ +
258
259 error[E0308]: mismatched types
260   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:55:9
261    |
262 LL | fn baw() -> Box<dyn Trait> {
263    |             -------------- expected `Box<(dyn Trait + 'static)>` because of return type
264 ...
265 LL |         42
266    |         ^^ expected struct `Box`, found integer
267    |
268    = note: expected struct `Box<(dyn Trait + 'static)>`
269                 found type `{integer}`
270    = 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
271 help: store this in the heap by calling `Box::new`
272    |
273 LL |         Box::new(42)
274    |         +++++++++  +
275
276 error[E0746]: return type cannot have an unboxed trait object
277   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:60:13
278    |
279 LL | fn bat() -> dyn Trait {
280    |             ^^^^^^^^^ doesn't have a size known at compile-time
281    |
282    = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
283 help: use `impl Trait` as the return type, as all return paths are of type `{integer}`, which implements `Trait`
284    |
285 LL | fn bat() -> impl Trait {
286    |             ~~~~~~~~~~
287
288 error[E0746]: return type cannot have an unboxed trait object
289   --> $DIR/dyn-trait-return-should-be-impl-trait.rs:66:13
290    |
291 LL | fn bay() -> dyn Trait {
292    |             ^^^^^^^^^ doesn't have a size known at compile-time
293    |
294    = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
295 help: use `impl Trait` as the return type, as all return paths are of type `{integer}`, which implements `Trait`
296    |
297 LL | fn bay() -> impl Trait {
298    |             ~~~~~~~~~~
299
300 error: aborting due to 20 previous errors
301
302 Some errors have detailed explanations: E0277, E0308, E0746.
303 For more information about an error, try `rustc --explain E0277`.