Skip to content

Commit 444a32e

Browse files
authored
Rollup merge of #75190 - GuillaumeGomez:cleanup-e0746, r=Dylan-DPC
Clean up E0746 explanation r? @Dylan-DPC
2 parents 422ee12 + cd46339 commit 444a32e

File tree

1 file changed

+6
-4
lines changed
  • src/librustc_error_codes/error_codes

1 file changed

+6
-4
lines changed

src/librustc_error_codes/error_codes/E0746.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Return types cannot be `dyn Trait`s as they must be `Sized`.
1+
An unboxed trait object was used as a return value.
22

33
Erroneous code example:
44

@@ -13,11 +13,13 @@ impl T for S {
1313
1414
// Having the trait `T` as return type is invalid because
1515
// unboxed trait objects do not have a statically known size:
16-
fn foo() -> dyn T {
16+
fn foo() -> dyn T { // error!
1717
S(42)
1818
}
1919
```
2020

21+
Return types cannot be `dyn Trait`s as they must be `Sized`.
22+
2123
To avoid the error there are a couple of options.
2224

2325
If there is a single type involved, you can use [`impl Trait`]:
@@ -32,7 +34,7 @@ If there is a single type involved, you can use [`impl Trait`]:
3234
# }
3335
// The compiler will select `S(usize)` as the materialized return type of this
3436
// function, but callers will only know that the return type implements `T`.
35-
fn foo() -> impl T {
37+
fn foo() -> impl T { // ok!
3638
S(42)
3739
}
3840
```
@@ -57,7 +59,7 @@ impl T for O {
5759
5860
// This now returns a "trait object" and callers are only be able to access
5961
// associated items from `T`.
60-
fn foo(x: bool) -> Box<dyn T> {
62+
fn foo(x: bool) -> Box<dyn T> { // ok!
6163
if x {
6264
Box::new(S(42))
6365
} else {

0 commit comments

Comments
 (0)