Trait mismatch across crates

error[E0277]: the trait bound `rand_xorshift::XorShiftRng: rand_core::SeedableRng` is not satisfied

This is refering to XorShiftRng from the newly-published rand_xorshift crate. It does implement SeedableRng, but apparently not the same one?

I hit this error recently: because I had multiple, incompatible versions of rand_core (which provides SeedableRng) in my tree. But I have since published a compatibility shim, and in this case there is in fact only one version of rand_core in the tree (according to Cargo.lock, only version 0.3.0 is used).

What else can cause this error? And what tools are available to diagnose it?

There are at least two avenues to explore:

  1. You can use cargo tree to print the dependency tree of your crate.

  2. You can run cargo build --verbose and manually inspect all the crate paths that are being passed on the command line.

cargo tree shows only a single version of rand_core (and no other crate that should define SeedableRng).

That said, rand re-exports SeedableRng, but I’m nearly certain this wouldn’t cause a problem: pub use rand_core::{RngCore, CryptoRng, SeedableRng};.

I checked the build command; it only shows a single instance of rand_core:

rustc --crate-name rand src/lib.rs --color always --crate-type lib --emit=dep-info,link -C debuginfo=2 --cfg 'feature="alloc"' --cfg 'feature="cloudabi"' --cfg 'feature="default"' --cfg 'feature="fuchsia-zircon"' --cfg 'feature="libc"' --cfg 'feature="rand_core"' --cfg 'feature="std"' --cfg 'feature="winapi"' -C metadata=7e3bdd302bfc2846 -C extra-filename=-7e3bdd302bfc2846 --out-dir /home/dhardy/projects/rand/rand/target/debug/deps -C incremental=/home/dhardy/projects/rand/rand/target/debug/incremental -L dependency=/home/dhardy/projects/rand/rand/target/debug/deps --extern libc=/home/dhardy/projects/rand/rand/target/debug/deps/liblibc-4352b4e227216509.rlib --extern rand_chacha=/home/dhardy/projects/rand/rand/target/debug/deps/librand_chacha-d48461c6797c7159.rlib --extern rand_core=/home/dhardy/projects/rand/rand/target/debug/deps/librand_core-3d01d6cd88157b95.rlib --extern rand_hc128=/home/dhardy/projects/rand/rand/target/debug/deps/librand_hc128-aea679b9d1ad3581.rlib --extern rand_isaac=/home/dhardy/projects/rand/rand/target/debug/deps/librand_isaac-e883d9eeff2b78b7.rlib --extern rand_pcg=/home/dhardy/projects/rand/rand/target/debug/deps/librand_pcg-9de83fb8ccc92d78.rlib --extern rand_xorshift=/home/dhardy/projects/rand/rand/target/debug/deps/librand_xorshift-296e9cc137479965.rlib --cfg rust_1_26 --cfg rust_1_27

But what is the build command for rand_xorshift?

Ah, I see; yes, I see a different version of rand_core used. Would this be because of different feature flags maybe? No, it’s because one of the crates depends on rand_core via a path, the other via a released crate.

Thanks for the hints.

You should be able to see this in cargo tree, I think?