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

Confusing derive(From) compilation error. #96

Closed
nathan-at-least opened this issue Oct 22, 2019 · 5 comments
Closed

Confusing derive(From) compilation error. #96

nathan-at-least opened this issue Oct 22, 2019 · 5 comments

Comments

@nathan-at-least
Copy link

nathan-at-least commented Oct 22, 2019

I am confused by a compilation error for derive(From):

error[E0119]: conflicting implementations of trait `std::convert::From<std::io::Error>` for type `Error`:
 --> src/main.rs:7:17
  |
7 | #[derive(Debug, derive_more::From)]
  |                 ^^^^^^^^^^^^^^^^^
  |                 |
  |                 first implementation here
  |                 conflicting implementation for `Error`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0119`.

My type's definition is:

#[derive(Debug, derive_more::From)]
enum Error {
    Io(std::io::Error),
    Tokio(tokio::io::Error),
}

My projects' dependencies are:

[dependencies]
derive_more = "0.13.0"
tokio = { version = "=0.2.0-alpha.6", features = ["process"] }

My toolchain versions:

$ cargo --version ; rustc --version
cargo 1.40.0-nightly (3a9abe3f0 2019-10-15)
rustc 1.40.0-nightly (7979016af 2019-10-20)

Discussion:

The error message leads me to believe there are identical/ambiguous types in an enum, so that derive(From) cannot distinguish between them. Yet I believe std::io::Error and tokio::io::Error are distinct types. Here are the docs for tokio::io::Error at v0.2.0-alpha.6.

Where does my confusion lie? Either something is wrong about my understanding or there is a bug in derive(From).

You can browse the project in question at the appropriate revision: iomux @ 8e65ea81

Edit: Added the type definition here to make this ticket clearer for newcomers.
Edit: Added toolchain versions.

@JelteF
Copy link
Owner

JelteF commented Oct 22, 2019

Please share the type definition. Otherwise it's impossible to give the reason for this error. Also, you haven't posted the error output for the second implementation of From. Are you sure you have not implemented one yourself as well?

@nathan-at-least
Copy link
Author

nathan-at-least commented Oct 22, 2019

Apologies for missing the type definition. I've edited the original description to include it.

There is no other error output! That's why I'm confused.

I'm not aware of any other From implementation on my own Error struct. I wondered if I was getting confused by the name Error (and maybe compilation message was about a different identifier), so I renamed struct Error to struct MyError, and rustc indeed complains about MyError.

Edit: Moved the type definition from this comment up to the original description for clarity for newcomers to the ticket.

@JelteF
Copy link
Owner

JelteF commented Oct 22, 2019 via email

@nathan-at-least
Copy link
Author

Ah, it looks like tokio::io::Error is a type alias: https://github.com/tokio-rs/tokio/blob/master/tokio/src/io/mod.rs#L64

Apologies for the confusion.

Would it be possible for derive(From) to somehow detect type aliases and improve the error message?

@JelteF
Copy link
Owner

JelteF commented Oct 27, 2019

That definitely explains it. And no it's not possible to give better errors in this case, because macro expansion doesn't have any type information (only AST info).

@JelteF JelteF closed this as completed Oct 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants