Skip to content
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

"trait is not implemented" error when returning "impl Trait" hides a more useful error #55027

Closed
jebrosen opened this issue Oct 13, 2018 · 2 comments

Comments

@jebrosen
Copy link
Contributor

@jebrosen jebrosen commented Oct 13, 2018

This code:

trait Trait { }
struct Oops;
impl Trait for Oops { }

fn make_oops() {
    Oops
}

fn make_trait() -> impl Trait {
    make_oops()
}

(playground: https://play.rust-lang.org/?gist=d5e6ddf80e412b0acae65a1c58a18e32&version=nightly&mode=debug&edition=2015)

fails to compile with the following error:

error[E0277]: the trait bound `(): Trait` is not satisfied
 --> src/main.rs:8:20
  |
8 | fn make_trait() -> impl Trait {
  |                    ^^^^^^^^^^ the trait `Trait` is not implemented for `()`
  |
  = note: the return type of a function must have a statically known size

error: aborting due to previous error

Commenting out the make_trait function reveals this error, which in my opinion is much better:

error[E0308]: mismatched types
 --> src/lib.rs:6:5
  |
5 | fn make_oops() {
  |                - help: try adding a return type: `-> Oops`
6 |     Oops
  |     ^^^^ expected (), found struct `Oops`
  |
  = note: expected type `()`
             found type `Oops`

Apologies if this is a duplicate; I didn't find an issue that looked similar.

Interestingly, using Clone or Debug instead of Trait also reveals the "better" error message. Using and implementing Read in place of Trait still shows the bad error message -- I originally encountered this when trying to return an impl Read.

@eddyp
Copy link
Contributor

@eddyp eddyp commented May 22, 2019

Currently (2019-May-23rd) on nightly(1.36.0-nightly) both errors are shown, while only the first is shown in beta(1.35.0-beta11) and current stable (1.34.2).

Here is the output on nightly:

error[E0277]: the trait bound (): Trait is not satisfied
--> src/lib.rs:9:20
|
9 | fn make_trait() -> impl Trait {
| ^^^^^^^^^^ the trait Trait is not implemented for ()
|
= note: the return type of a function must have a statically known size

error[E0308]: mismatched types
--> src/lib.rs:6:5
|
5 | fn make_oops() {
| - help: try adding a return type: -> Oops
6 | Oops
| ^^^^ expected (), found struct Oops
|
= note: expected type ()
found type Oops

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try rustc --explain E0277.
error: Could not compile playground.

To learn more, run the command again with --verbose.

@jebrosen
Copy link
Contributor Author

@jebrosen jebrosen commented May 23, 2019

So this issue looks fixed. That's great! If I'm bored some time I might bisect and try to find which commit resolved this, but I'm content to close the issue.

@jebrosen jebrosen closed this May 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants