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 · 7 comments
Open

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

jyn514 opened this issue Apr 7, 2023 · 7 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.
@estebank
Copy link
Contributor

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: two different versions of crate `hashbrown` are being used; two types coming from two different versions of the same crate are different types even if they look the same
    |
   ::: /home/gh-estebank/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/hashbrown-0.12.3/src/map.rs:188:1
    |
188 | pub struct HashMap<K, V, S = DefaultHashBuilder, A: Allocator + Clone = Global> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is the expected type `hashbrown::map::HashMap`
   --> /rust/deps/hashbrown-0.15.2/src/map.rs:185:1
    |
    = note: this is the found type `hashbrown::HashMap`
    |
   ::: src/main.rs:2:1
    |
2   | extern crate hashbrown;
    | ----------------------- one version of crate `hashbrown` used here, as a direct dependency of the current crate
...
6   |     inner::foo(map);
    |     ----- one version of crate `hashbrown` used here, as a dependency of crate `inner`
    = help: you can use `cargo tree` to explore your dependency tree
note: function defined here
   --> /home/gh-estebank/sysrootdep/inner/src/lib.rs:1:8
    |
1   | pub fn foo(_: hashbrown::HashMap<u32, u32>) {}
    |        ^^^

Do you feel this is enough specificity @jyn514?

@jyn514
Copy link
Member Author

jyn514 commented Jan 16, 2025

The language there seems mostly the same. The only thing that’s improved is that it now says /rust/deps instead of pointing to the cargo registry. And that’s easy to overlook, and I’m not entirely sure it’s not just because you don’t have the rustc-src component installed. So I would prefer to keep this open until the message explicitly mentions the sysroot.

(I do think the message is more clear if you do in fact actually have two different versions of hashbrown, like would normally be the case when this error happens. but that is not the case in the original issue, notice how the version numbers are the same.)

@jyn514
Copy link
Member Author

jyn514 commented Jan 16, 2025

in particular, the new language about cargo tree is very helpful when you’re using cargo, and actively misleading if you’re loading dependencies from sysroot.

@estebank
Copy link
Contributor

For my locally built rustc, I get the right span. It's for my nightly that it doesn't display it (I think because of the path renaming/anonymization). I'm not quite sure how to get the right sysroot path (corresponding to /rust/deps/ on nightly) so that we could identify this is happening and provide some extra text in the output to make that clearer.

@jyn514
Copy link
Member Author

jyn514 commented Jan 16, 2025

i would expect that rustc_metadata stores info somewhere about whether the crate was loaded from a -L deps/ folder or from the sysroot? i strongly think we should not do this based on the original source span, we should do it based on the location of the .rmeta that got loaded.

@estebank
Copy link
Contributor

I'm just not sure that we still have all of the necessary information by hir typeck.

@bjorn3
Copy link
Member

bjorn3 commented Jan 16, 2025

rustc_metadata stores the path to the rlib/dylib/rmeta file(s) for the crate. And it should be easy to add another flag stating if the crate came from the sysroot or not.

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.

3 participants