Skip to content

Commit 7feec4e

Browse files
committed
Auto merge of #117609 - jyn514:sysroot-errors, r=davidtwco
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 #110055.
2 parents 5ad7454 + c790313 commit 7feec4e

File tree

8 files changed

+80
-2
lines changed

8 files changed

+80
-2
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
18431843
let expected_defid = expected_adt.did();
18441844

18451845
diagnostic.note(format!("{found_name} and {expected_name} have similar names, but are actually distinct types"));
1846+
let have_same_crate_name = self.tcx.crate_name(found_defid.krate)
1847+
== self.tcx.crate_name(expected_defid.krate);
18461848
for (defid, name) in
18471849
[(found_defid, found_name), (expected_defid, expected_name)]
18481850
{
@@ -1862,7 +1864,21 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
18621864
format!("{name} is defined in the current crate")
18631865
} else {
18641866
let crate_name = self.tcx.crate_name(defid.krate);
1865-
format!("{name} is defined in crate `{crate_name}`")
1867+
diagnostic.span_note(
1868+
def_span,
1869+
format!("{name} is defined in crate `{crate_name}`"),
1870+
);
1871+
1872+
// If these are named the same, give a hint about why the compiler thinks they're different.
1873+
if have_same_crate_name {
1874+
let crate_paths = self.tcx.crate_extern_paths(defid.krate);
1875+
diagnostic.note(format!(
1876+
"`{crate_name}` was loaded from {}",
1877+
crate_paths[0].display()
1878+
));
1879+
}
1880+
1881+
continue;
18661882
};
18671883
diagnostic.span_note(def_span, msg);
18681884
}

src/bootstrap/src/core/builder.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,11 @@ impl<'a> Builder<'a> {
17971797
cargo.env("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR", map_to);
17981798
}
17991799

1800-
if self.config.rust_remap_debuginfo {
1800+
// Users won't have the original cargo registry from the CI builder available, even if they have `rust-src` installed.
1801+
// Don't show their sources in UI tests even if they're available.
1802+
// As a happy side-effect, this fixes a few UI tests when download-rustc is enabled.
1803+
// NOTE: only set this when building std so the error messages are better for rustc itself.
1804+
if self.config.rust_remap_debuginfo || mode == Mode::Std {
18011805
let mut env_var = OsString::new();
18021806
if self.config.vendor {
18031807
let vendor = self.build.src.join("vendor");

tests/incremental/circular-dependencies.rs

+2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ fn test() {
2626
//[cfail2]~| NOTE arguments to this function are incorrect
2727
//[cfail2]~| NOTE `Foo` and `circular_dependencies::Foo` have similar names, but are actually distinct types
2828
//[cfail2]~| NOTE the crate `circular_dependencies` is compiled multiple times, possibly with different configurations
29+
//[cfail2]~| NOTE loaded from
2930
//[cfail2]~| NOTE function defined here
3031

3132
consume_foo(aux::produce_foo());
3233
//[cfail2]~^ ERROR mismatched types [E0308]
3334
//[cfail2]~| NOTE expected `Foo`, found `circular_dependencies::Foo`
3435
//[cfail2]~| NOTE arguments to this function are incorrect
3536
//[cfail2]~| NOTE `circular_dependencies::Foo` and `Foo` have similar names, but are actually distinct types
37+
//[cfail2]~| NOTE loaded from
3638
//[cfail2]~| NOTE the crate `circular_dependencies` is compiled multiple times, possibly with different configurations
3739
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Test that we emit useful diagnostics when the same crate is loaded from the sysroot and --extern.
2+
# This can't be a run-make test because it needs the aux-file to itself have a dependency passed with --extern
3+
# (and just bare `--extern hashbrown` errors too early, while compiling `uses_hashbrown.rs`).
4+
5+
include ../tools.mk
6+
define SED_REGEX
7+
s#/hashbrown-[^/]*/src#/hashbrown-VERSION/src#;
8+
s#[^ ]*/build/[^ ]*/lib/rustlib/[^ ]*#BUILD_DIR/HOST/stageN/lib/rustlib/TARGET/lib/libhashbrown.rlib#;
9+
s#[^ ]*/build/[^ ]*/test/run-make/#BUILD_DIR/HOST/test/run-make/#
10+
s#[^ ]*tests/run-make/#TEST_DIR/#
11+
endef
12+
export SED_REGEX
13+
14+
all:
15+
$(RUSTC) hashbrown.rs --crate-type lib
16+
$(RUSTC) uses-hashbrown.rs --extern hashbrown=$(TMPDIR)/libhashbrown.rlib --crate-type lib
17+
$(RUSTC) mismatch.rs --extern uses_hashbrown=$(TMPDIR)/libuses_hashbrown.rlib 2>$(TMPDIR)/stderr.txt || true
18+
sed -e "$$SED_REGEX" < $(TMPDIR)/stderr.txt > $(TMPDIR)/normalized.txt
19+
$(RUSTC_TEST_OP) $(TMPDIR)/normalized.txt expected.txt
20+
$(CGREP) "loaded from" < $(TMPDIR)/stderr.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0308]: mismatched types
2+
--> mismatch.rs:5:25
3+
|
4+
5 | uses_hashbrown::foo(hashbrown::HashMap::default())
5+
| ------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `HashMap`, found `HashMap<_, _, _, _>`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: `hashbrown::HashMap<_, _, _, _>` and `hashbrown::HashMap` have similar names, but are actually distinct types
10+
note: `hashbrown::HashMap<_, _, _, _>` is defined in crate `hashbrown`
11+
--> /rust/deps/hashbrown-VERSION/src/map.rs:190:1
12+
= note: `hashbrown` was loaded from BUILD_DIR/HOST/stageN/lib/rustlib/TARGET/lib/libhashbrown.rlib
13+
note: `hashbrown::HashMap` is defined in crate `hashbrown`
14+
--> TEST_DIR/type-mismatch-sysroot-crate/hashbrown.rs:1:1
15+
|
16+
1 | pub struct HashMap;
17+
| ^^^^^^^^^^^^^^^^^^
18+
= note: `hashbrown` was loaded from BUILD_DIR/HOST/test/run-make/type-mismatch-sysroot-crate/type-mismatch-sysroot-crate/libhashbrown.rlib
19+
= note: perhaps two different versions of crate `hashbrown` are being used?
20+
note: function defined here
21+
--> TEST_DIR/type-mismatch-sysroot-crate/uses-hashbrown.rs:1:8
22+
|
23+
1 | pub fn foo(_: hashbrown::HashMap) {}
24+
| ^^^
25+
26+
error: aborting due to 1 previous error
27+
28+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct HashMap;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![feature(rustc_private)]
2+
extern crate hashbrown;
3+
4+
fn main() {
5+
uses_hashbrown::foo(hashbrown::HashMap::default())
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn foo(_: hashbrown::HashMap) {}

0 commit comments

Comments
 (0)