Skip to content

Commit

Permalink
Rollup merge of rust-lang#117609 - jyn514:sysroot-errors, r=davidtwco
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matthiaskrgr authored Nov 6, 2023
2 parents 6f45723 + c25af3d commit f9dd9ba
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
11 changes: 10 additions & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let expected_defid = expected_adt.did();

diagnostic.note(format!("{found_name} and {expected_name} have similar names, but are actually distinct types"));
let have_same_crate_name = self.tcx.crate_name(found_defid.krate) == self.tcx.crate_name(expected_defid.krate);
for (defid, name) in
[(found_defid, found_name), (expected_defid, expected_name)]
{
Expand All @@ -1843,7 +1844,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
format!("{name} is defined in the current crate")
} else {
let crate_name = self.tcx.crate_name(defid.krate);
format!("{name} is defined in crate `{crate_name}`")
diagnostic.span_note(def_span, format!("{name} is defined in crate `{crate_name}`"));

// If these are named the same, give a hint about why the compiler thinks they're different.
if have_same_crate_name {
let crate_paths = self.tcx.crate_extern_paths(defid.krate);
diagnostic.note(format!("`{crate_name}` was loaded from {}", crate_paths[0].display()));
}

continue;
};
diagnostic.span_note(def_span, msg);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/incremental/circular-dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ fn test() {
//[cfail2]~| NOTE arguments to this function are incorrect
//[cfail2]~| NOTE `Foo` and `circular_dependencies::Foo` have similar names, but are actually distinct types
//[cfail2]~| NOTE the crate `circular_dependencies` is compiled multiple times, possibly with different configurations
//[cfail2]~| NOTE loaded from
//[cfail2]~| NOTE function defined here

consume_foo(aux::produce_foo());
//[cfail2]~^ ERROR mismatched types [E0308]
//[cfail2]~| NOTE expected `Foo`, found `circular_dependencies::Foo`
//[cfail2]~| NOTE arguments to this function are incorrect
//[cfail2]~| NOTE `circular_dependencies::Foo` and `Foo` have similar names, but are actually distinct types
//[cfail2]~| NOTE loaded from
//[cfail2]~| NOTE the crate `circular_dependencies` is compiled multiple times, possibly with different configurations
}
20 changes: 20 additions & 0 deletions tests/run-make/type-mismatch-sysroot-crate/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Test that we emit useful diagnostics when the same crate is loaded from the sysroot and --extern.
# This can't be a run-make test because it needs the aux-file to itself have a dependency passed with --extern
# (and just bare `--extern hashbrown` errors too early, while compiling `uses_hashbrown.rs`).

include ../tools.mk
define SED_REGEX
s#[^ ]*registry/src/index.crates.io[^ ]*/src#CARGO_REGISTRY/hashbrown-VERSION/src#;
s#[^ ]*/build/[^ ]*/lib/rustlib/[^ ]*#BUILD_DIR/HOST/stageN/lib/rustlib/TARGET/lib/libhashbrown.rlib#;
s#[^ ]*/build/[^ ]*/test/run-make/#BUILD_DIR/HOST/test/run-make/#
s#[^ ]*tests/run-make/#TEST_DIR/#
endef
export SED_REGEX

all:
$(RUSTC) hashbrown.rs --crate-type lib
$(RUSTC) uses-hashbrown.rs --extern hashbrown=$(TMPDIR)/libhashbrown.rlib --crate-type lib
$(RUSTC) mismatch.rs --extern uses_hashbrown=$(TMPDIR)/libuses_hashbrown.rlib 2>$(TMPDIR)/stderr.txt || true
sed -e "$$SED_REGEX" < $(TMPDIR)/stderr.txt > $(TMPDIR)/normalized.txt
$(RUSTC_TEST_OP) $(TMPDIR)/normalized.txt expected.txt
$(CGREP) "loaded from" < $(TMPDIR)/stderr.txt
31 changes: 31 additions & 0 deletions tests/run-make/type-mismatch-sysroot-crate/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error[E0308]: mismatched types
--> mismatch.rs:5:25
|
5 | uses_hashbrown::foo(hashbrown::HashMap::default())
| ------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `HashMap`, found `HashMap<_, _, _, _>`
| |
| arguments to this function are incorrect
|
= note: `hashbrown::HashMap<_, _, _, _>` and `hashbrown::HashMap` have similar names, but are actually distinct types
note: `hashbrown::HashMap<_, _, _, _>` is defined in crate `hashbrown`
--> CARGO_REGISTRY/hashbrown-VERSION/src/map.rs:190:1
|
190 | pub struct HashMap<K, V, S = DefaultHashBuilder, A: Allocator = Global> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `hashbrown` was loaded from BUILD_DIR/HOST/stageN/lib/rustlib/TARGET/lib/libhashbrown.rlib
note: `hashbrown::HashMap` is defined in crate `hashbrown`
--> TEST_DIR/type-mismatch-sysroot-crate/hashbrown.rs:1:1
|
1 | pub struct HashMap;
| ^^^^^^^^^^^^^^^^^^
= note: `hashbrown` was loaded from BUILD_DIR/HOST/test/run-make/type-mismatch-sysroot-crate/type-mismatch-sysroot-crate/libhashbrown.rlib
= note: perhaps two different versions of crate `hashbrown` are being used?
note: function defined here
--> TEST_DIR/type-mismatch-sysroot-crate/uses-hashbrown.rs:1:8
|
1 | pub fn foo(_: hashbrown::HashMap) {}
| ^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
1 change: 1 addition & 0 deletions tests/run-make/type-mismatch-sysroot-crate/hashbrown.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub struct HashMap;
6 changes: 6 additions & 0 deletions tests/run-make/type-mismatch-sysroot-crate/mismatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![feature(rustc_private)]
extern crate hashbrown;

fn main() {
uses_hashbrown::foo(hashbrown::HashMap::default())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn foo(_: hashbrown::HashMap) {}

0 comments on commit f9dd9ba

Please sign in to comment.