8000 Rollup of 6 pull requests by JohnTitor · Pull Request #75206 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Rollup of 6 pull requests #75206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean up E0746 explanation
  • Loading branch information
GuillaumeGomez committed Aug 5, 2020
commit cd4633917d4dd446f9f37efbf750bbd4ed2b46d6
10 changes: 6 additions & 4 deletions src/librustc_error_codes/error_codes/E0746.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Return types cannot be `dyn Trait`s as they must be `Sized`.
An unboxed trait object was used as a return value.

Erroneous code example:

Expand All @@ -13,11 +13,13 @@ impl T for S {

// Having the trait `T` as return type is invalid because
// unboxed trait objects do not have a statically known size:
fn foo() -> dyn T {
fn foo() -> dyn T { // error!
S(42)
}
```

Return types cannot be `dyn Trait`s as they must be `Sized`.

To avoid the error there are a couple of options.

If there is a single type involved, you can use [`impl Trait`]:
Expand All @@ -32,7 +34,7 @@ If there is a single type involved, you can use [`impl Trait`]:
# }
// The compiler will select `S(usize)` as the materialized return type of this
// function, but callers will only know that the return type implements `T`.
fn foo() -> impl T {
fn foo() -> impl T { // ok!
S(42)
}
```
Expand All @@ -57,7 +59,7 @@ impl T for O {

// This now returns a "trait object" and callers are only be able to access
// associated items from `T`.
fn foo(x: bool) -> Box<dyn T> {
fn foo(x: bool) -> Box<dyn T> { // ok!
if x {
Box::new(S(42))
} else {
Expand Down
0