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

Open
jebrosen opened this Issue Oct 13, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@jebrosen
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment