Skip to content

Commit

Permalink
Rollup merge of rust-lang#73449 - ehuss:duplicate-lang-item, r=matthe…
Browse files Browse the repository at this point in the history
…wjasper

Provide more information on duplicate lang item error.

This gives some notes on the location of the files where the lang items were loaded from. Some duplicate lang item errors can be a little confusing, and this might help in diagnosing what has happened.

Here's an example when hitting a bug with Cargo's build-std:

```
error: duplicate lang item in crate `core` (which `rustc_std_workspace_core` depends on): `try`.
  |
  = note: the lang item is first defined in crate `core` (which `z10` depends on)
  = note: first definition in `core` loaded from /Users/eric/Proj/rust/cargo/scratch/z10/target/target/debug/deps/libcore-a764da499c7385f4.rmeta
  = note: second definition in `core` loaded from /Users/eric/Proj/rust/cargo/scratch/z10/target/target/debug/deps/libcore-5b082675aea34986.rmeta
```
  • Loading branch information
Manishearth authored Jul 2, 2020
2 parents 02484c6 + 1b3ef66 commit 0d26961
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/librustc_metadata/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ provide! { <'tcx> tcx, def_id, other, cdata,

syms
}

crate_extern_paths => { cdata.source().paths().cloned().collect() }
}

pub fn provide(providers: &mut Providers<'_>) {
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_middle/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,10 @@ rustc_queries! {
eval_always
desc { "looking up the extra filename for a crate" }
}
query crate_extern_paths(_: CrateNum) -> Vec<PathBuf> {
eval_always
desc { "looking up the paths for extern crates" }
}
}

TypeChecking {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_middle/ty/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use rustc_span::{Span, DUMMY_SP};
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::Arc;

#[macro_use]
Expand Down
22 changes: 22 additions & 0 deletions src/librustc_passes/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ impl LanguageItemCollector<'tcx> {
));
}
}
let mut note_def = |which, def_id: DefId| {
let crate_name = self.tcx.crate_name(def_id.krate);
let note = if def_id.is_local() {
format!("{} definition in the local crate (`{}`)", which, crate_name)
} else {
let paths: Vec<_> = self
.tcx
.crate_extern_paths(def_id.krate)
.iter()
.map(|p| p.display().to_string())
.collect();
format!(
"{} definition in `{}` loaded from {}",
which,
crate_name,
paths.join(", ")
)
};
err.note(&note);
};
note_def("first", original_def_id);
note_def("second", item_def_id);
}
err.emit();
}
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/duplicate_entry_error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// normalize-stderr-test "loaded from .*libstd-.*.rlib" -> "loaded from SYSROOT/libstd-*.rlib"
// note-pattern: first defined in crate `std`.

// Test for issue #31788 and E0152
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/duplicate_entry_error.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0152]: found duplicate lang item `panic_impl`
--> $DIR/duplicate_entry_error.rs:10:1
--> $DIR/duplicate_entry_error.rs:11:1
|
LL | / fn panic_impl(info: &PanicInfo) -> ! {
LL | |
Expand All @@ -8,6 +8,8 @@ LL | | }
| |_^
|
= note: the lang item is first defined in crate `std` (which `duplicate_entry_error` depends on)
= note: first definition in `std` loaded from SYSROOT/libstd-*.rlib
= note: second definition in the local crate (`duplicate_entry_error`)

error: aborting due to previous error

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/error-codes/E0152.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// normalize-stderr-test "loaded from .*liballoc-.*.rlib" -> "loaded from SYSROOT/liballoc-*.rlib"
#![feature(lang_items)]

#[lang = "owned_box"]
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/error-codes/E0152.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
error[E0152]: found duplicate lang item `owned_box`
--> $DIR/E0152.rs:4:1
--> $DIR/E0152.rs:5:1
|
LL | struct Foo;
| ^^^^^^^^^^^
|
= note: the lang item is first defined in crate `alloc` (which `std` depends on)
= note: first definition in `alloc` loaded from SYSROOT/liballoc-*.rlib
= note: second definition in the local crate (`E0152`)

error: aborting due to previous error

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/panic-handler/panic-handler-std.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// normalize-stderr-test "loaded from .*libstd-.*.rlib" -> "loaded from SYSROOT/libstd-*.rlib"
// error-pattern: found duplicate lang item `panic_impl`


Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/panic-handler/panic-handler-std.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
error[E0152]: found duplicate lang item `panic_impl`
--> $DIR/panic-handler-std.rs:7:1
--> $DIR/panic-handler-std.rs:8:1
|
LL | / fn panic(info: PanicInfo) -> ! {
LL | | loop {}
LL | | }
| |_^
|
= note: the lang item is first defined in crate `std` (which `panic_handler_std` depends on)
= note: first definition in `std` loaded from SYSROOT/libstd-*.rlib
= note: second definition in the local crate (`panic_handler_std`)

error: argument should be `&PanicInfo`
--> $DIR/panic-handler-std.rs:7:16
--> $DIR/panic-handler-std.rs:8:16
|
LL | fn panic(info: PanicInfo) -> ! {
| ^^^^^^^^^
Expand Down

0 comments on commit 0d26961

Please sign in to comment.