Confusing error message with mismatched proc-macro-derive dependencies

50e6129
Opened by Mark Thebridge at 2024-08-29 23:35:01

Not sure if this is that urgent, but it took me several hours to debug this simple compiler error with help from multiple people, so raising anyway...

Suppose you have a crate foo defining some trait Foo, and then a foo-derive crate of type proc-macro-lib that allows you to custom derive Foo. foo-derive must then depend on foo in Cargo.toml, because it mentions the Foo trait. If I then want to use the derivation I have to use both foo and foo-derive crates. Fine.

The problem comes if the using crate (let's call it bar) uses a different version of foo from the one that foo-derive does. So let's say that foo-derive 1.0 has in Cargo.toml:

[dependencies]
foo = "1.1"

Now, if in bar's Cargo.toml I mistakenly add:

[dependencies]
foo = "1.0"
foo-derive = "1.0"

And then use the custom derive of Foo within bar:

`#[derive(foo)]`
struct BarStruct{ ... }

I get the confusing error the trait Foo is not implemented for BarStruct. Even though I can see using cargo-expand that the trait is implemented. The point is that the derive code has implemented a different version of Foo, and the fix is to change bar to depend on foo version 1.1. But the compiler error does not make that at all obvious!