Skip to content

Confusing error message with mismatched proc-macro-derive dependencies #46561

Open
@mthebridge

Description

@mthebridge

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-proc-macrosArea: Procedural macrosD-crate-version-mismatchDiagnostics: Errors or lints caused be the use of two different crate versions.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions