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

Distinguish versions of a crate loaded from the sysroot and from cargo #110055

Open
jyn514 opened this issue Apr 7, 2023 · 0 comments
Open

Distinguish versions of a crate loaded from the sysroot and from cargo #110055

jyn514 opened this issue Apr 7, 2023 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-metadata Area: Crate metadata D-crate-version-mismatch Diagnostics: Errors or lints caused be the use of two different crate versions. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Apr 7, 2023

Code

// inner/Cargo.toml
[package]
name = "inner"
edition = "2021"

[dependencies]
hashbrown = "0.12.3"

// inner/src/lib.rs
pub fn foo(_: hashbrown::HashMap<u32, u32>) {}

// outer/src/main.rs
#![feature(rustc_private)]
extern crate hashbrown;

fn main() {
    let map = hashbrown::HashMap::default();
    inner::foo(map);
}

Current output

error[E0308]: mismatched types
   --> src/main.rs:6:16
    |
6   |     inner::foo(map);
    |     ---------- ^^^ expected `HashMap<u32, u32>`, found `HashMap<_, _, _, _>`
    |     |
    |     arguments to this function are incorrect
    |
    = note: `HashMap<_, _, _, _>` and `HashMap<u32, u32>` have similar names, but are actually distinct types
note: `HashMap<_, _, _, _>` is defined in crate `hashbrown`
   --> /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.12.3/src/map.rs:188:1
note: `HashMap<u32, u32>` is defined in crate `hashbrown`
   --> /Users/jyn/.local/lib/cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.12.3/src/map.rs:188:1
    |
188 | pub struct HashMap<K, V, S = DefaultHashBuilder, A: Allocator + Clone = Global> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: perhaps two different versions of crate `hashbrown` are being used?
note: function defined here
   --> /Users/jyn/src/example/inner/src/lib.rs:1:8
    |
1   | pub fn foo(_: hashbrown::HashMap<u32, u32>) {}
    |        ^^^

Desired output

note: `HashMap<_, _, _, _>` is defined in crate `hashbrown`
   --> /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.12.3/src/map.rs:188:1
   = note: loaded from /Users/jyn/.local/lib/rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libhashbrown-016a8bf1dc67c69f.rlib
   = note: which is in the compiler's "sysroot": https://rustc-dev-guide.rust-lang.org/building/bootstrapping.html#what-is-a-sysroot
note: `HashMap<u32, u32>` is defined in crate `hashbrown` (loaded from `/Users/jyn/.local/lib/cargo/target/debug/deps/`)
   --> /Users/jyn/.local/lib/cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.12.3/src/map.rs:188:1
    |
188 | pub struct HashMap<K, V, S = DefaultHashBuilder, A: Allocator + Clone = Global> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: loaded from /Users/jyn/.local/lib/cargo/target/debug/deps/libhashbrown-408ce4dce55d643d.rmeta
    = note: perhaps two different versions of crate `hashbrown` are being used?

Rationale and extra context

It's not clear that "/Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.12.3/src/map.rs:188:1" is referring to a crate loaded from the sysroot; that path doesn't correspond to anything on disk. It would be nice to explain what's going on; when dealing with sysroots the exact paths of the artifacts often really are important.

Other cases

No response

Anything else?

This is minimized from a real error @aDotInTheVoid saw in rustdoc:

   Compiling rustdoc v0.0.0 (/home/alona/dev/rust/rust/src/librustdoc)
error[E0308]: mismatched types
   --> src/librustdoc/json/mod.rs:235:13
    |
235 |             index,
    |             ^^^^^ expected `rustc_hash::FxHasher`, found `FxHasher`
    |
    = note: `FxHasher` and `rustc_hash::FxHasher` have similar names, but are actually distinct types
note: `FxHasher` is defined in crate `rustc_hash`
   --> /cargo/registry/src/index.crates.io-6f17d22bba15001f/rustc-hash-1.1.0/src/lib.rs:60:1
note: `rustc_hash::FxHasher` is defined in crate `rustc_hash`
   --> /home/alona/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rustc-hash-1.1.0/src/lib.rs:60:1
    |
60  | pub struct FxHasher {
    | ^^^^^^^^^^^^^^^^^^^
    = note: perhaps two different versions of crate `rustc_hash` are being used?
@jyn514 jyn514 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-metadata Area: Crate metadata labels Apr 7, 2023
@jyn514 jyn514 added the D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. label Apr 17, 2023
@jyn514 jyn514 added the D-crate-version-mismatch Diagnostics: Errors or lints caused be the use of two different crate versions. label Apr 25, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 6, 2023
Distinguish crates with the same name in type errors

Previously, errors for crates with the same name would only distinguish them by the span of the source:
```
note: `HashMap<_, _, _, _>` is defined in crate `hashbrown`
   --> /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.12.3/src/map.rs:188:1
note: `HashMap<u32, u32>` is defined in crate `hashbrown`
   --> /Users/jyn/.local/lib/cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.12.3/src/map.rs:188:1
```

When the same version of the crate is loaded twice, this isn't particularly helpful. Additionally show where the .rlib was loaded from (in this case one was loaded from the sysroot and the other from a cargo target dir).

Fixes rust-lang#110055.
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 30, 2024
Distinguish crates with the same name in type errors

Previously, errors for crates with the same name would only distinguish them by the span of the source:
```
note: `HashMap<_, _, _, _>` is defined in crate `hashbrown`
   --> /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.12.3/src/map.rs:188:1
note: `HashMap<u32, u32>` is defined in crate `hashbrown`
   --> /Users/jyn/.local/lib/cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.12.3/src/map.rs:188:1
```

When the same version of the crate is loaded twice, this isn't particularly helpful. Additionally
show where the .rlib was loaded from (in this case one was loaded from the sysroot and the other
from a cargo target dir).

---

This also remaps cargo paths more eagerly; particularly, it unconditionally remaps cargo paths for
libstd unconditionally, even if `remap-debuginfo = false`. This is the first time that cargo paths
have shown up in UI tests; if the paths aren't remapped, they will differ depending on the value of
`remap-debuginfo`, which means tests will fail either locally or in CI. This can't be worked around
with adhoc normalization in the test makefile because it impacts the column width used for line
numbers (when paths are remapped, we don't show the cargo sources).

I could be convinced this is not the best solution. It's possible we could take some other approach,
like how `CFG_VIRTUAL_RUST_SOURCE_BASE_DIR` opportunistically reverses the mapping for `compiler/`,
which doesn't impact the error messages standard library developers see. That would be a bit more
involved to get working, though.

---

Fixes rust-lang#110055.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-metadata Area: Crate metadata D-crate-version-mismatch Diagnostics: Errors or lints caused be the use of two different crate versions. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant