Skip to content

Commit

Permalink
Provide more information on duplicate lang item error.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Jun 30, 2020
1 parent 665190b commit c225e5c
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/librustc_metadata/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use rustc_span::symbol::{Ident, Symbol};
use rustc_data_structures::sync::Lrc;
use smallvec::SmallVec;
use std::any::Any;
use std::path::PathBuf;

macro_rules! provide {
(<$lt:tt> $tcx:ident, $def_id:ident, $other:ident, $cdata:ident,
Expand Down Expand Up @@ -513,4 +514,8 @@ impl CrateStore for CStore {
fn allocator_kind(&self) -> Option<AllocatorKind> {
self.allocator_kind()
}

fn crate_extern_paths(&self, cnum: CrateNum) -> Vec<PathBuf> {
self.get_crate_data(cnum).source().paths().cloned().collect()
}
}
1 change: 1 addition & 0 deletions src/librustc_middle/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ pub trait CrateStore {
fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata;
fn metadata_encoding_version(&self) -> &[u8];
fn allocator_kind(&self) -> Option<AllocatorKind>;
fn crate_extern_paths(&self, cnum: CrateNum) -> Vec<PathBuf>;
}

pub type CrateStoreDyn = dyn CrateStore + sync::Sync;
Expand Down
9 changes: 9 additions & 0 deletions src/librustc_middle/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use std::hash::{Hash, Hasher};
use std::iter;
use std::mem;
use std::ops::{Bound, Deref};
use std::path::PathBuf;
use std::sync::Arc;

type InternedSet<'tcx, T> = ShardedHashMap<Interned<'tcx, T>, ()>;
Expand Down Expand Up @@ -1252,6 +1253,14 @@ impl<'tcx> TyCtxt<'tcx> {
if cnum == LOCAL_CRATE { false } else { self.cstore.crate_is_private_dep_untracked(cnum) }
}

pub fn crate_extern_paths(&self, cnum: CrateNum) -> Vec<PathBuf> {
if cnum == LOCAL_CRATE {
self.sess.local_crate_source_file.iter().cloned().collect()
} else {
self.cstore.crate_extern_paths(cnum)
}
}

#[inline]
pub fn def_path_hash(self, def_id: DefId) -> rustc_hir::definitions::DefPathHash {
if let Some(def_id) = def_id.as_local() {
Expand Down
21 changes: 21 additions & 0 deletions src/librustc_passes/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ impl LanguageItemCollector<'tcx> {
));
}
}
let mut note_def = |which, def_id: DefId| {
let location = if def_id.is_local() {
"the local crate".to_string()
} else {
let paths: Vec<_> = self
.tcx
.crate_extern_paths(def_id.krate)
.iter()
.map(|p| p.display().to_string())
.collect();
paths.join(", ")
};
err.note(&format!(
"{} definition in `{}` loaded from {}",
which,
self.tcx.crate_name(def_id.krate),
location
));
};
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 `duplicate_entry_error` loaded from the local crate

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 `E0152` loaded from the local crate

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 `panic_handler_std` loaded from the local crate

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 c225e5c

Please sign in to comment.