-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Description
Problem
I'm working on some piece of proprietary code, and for the internal dependencies, we use git repositories instead of crates on crates.io or some internal registry.
Today I faced I really annoying error message:
error[E0308]: mismatched types
--> src/main.rs:6:9
|
6 | foo(x);
| --- ^ expected enum `lower::X`, found enum `upper::X`
| |
| arguments to this function are incorrect
|
= note: enum `upper::X` and enum `lower::X` have similar names, but are actually distinct types
note: enum `upper::X` is defined in crate `lower`
--> /home/stymaar/.cargo/git/checkouts/repro_cargo_lower-dc970fbba3cb3609/5914bef/src/lib.rs:1:1
|
1 | pub enum X{
| ^^^^^^^^^^
note: enum `lower::X` is defined in crate `lower`
--> /home/stymaar/.cargo/git/checkouts/repro_cargo_lower-dc970fbba3cb3609/5914bef/src/lib.rs:1:1
|
1 | pub enum X{
| ^^^^^^^^^^
= note: perhaps two different versions of crate `lower` are being used?
note: function defined here
--> /home/stymaar/.cargo/git/checkouts/repro_cargo_lower-dc970fbba3cb3609/5914bef/src/lib.rs:6:8
|
6 | pub fn foo(x:X){
| ^^^
So it complains that the type comes from different versions of the lower crate, whereas it comes from the same version as we can see in the error message itself (for both, it's repro_cargo_lower-dc970fbba3cb3609/5914bef).
I was very confused for a while and then I managed to understand what's going on an reproduce it:
Steps
git clone [email protected]:StyMaar/repro_cargo_top.gitcargo check
Notes
The problem comes from the upper dependency which does not specify the target commit (there is no rev in the declaration of the lower dependency in upper) but there's a Cargo.lock file which points to the same revision of lower as the one being used in the top-level crate.
In the end, the lower dependency is imported twice for the same revision, since cargo doesn't realize that it's in fact the same revision, and then rustc complains about the presence of two different types.
Version
cargo 1.67.0-nightly (eb5d35917 2022-11-17)
release: 1.67.0-nightly
commit-hash: eb5d35917b2395194593c9ca70c3778f60c1573b
commit-date: 2022-11-17
host: x86_64-unknown-linux-gnu
libgit2: 1.5.0 (sys:0.15.0 vendored)
libcurl: 7.86.0-DEV (sys:0.4.59+curl-7.86.0 vendored ssl:OpenSSL/1.1.1q)
os: Linux 19.3 (tricia) [64-bit]