From c225e5c5cb1200f46b83752e160b489eba1cd597 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 17 Jun 2020 11:05:30 -0700 Subject: [PATCH 1/2] Provide more information on duplicate lang item error. --- .../rmeta/decoder/cstore_impl.rs | 5 +++++ src/librustc_middle/middle/cstore.rs | 1 + src/librustc_middle/ty/context.rs | 9 ++++++++ src/librustc_passes/lang_items.rs | 21 +++++++++++++++++++ src/test/ui/duplicate_entry_error.rs | 1 + src/test/ui/duplicate_entry_error.stderr | 4 +++- src/test/ui/error-codes/E0152.rs | 1 + src/test/ui/error-codes/E0152.stderr | 4 +++- .../ui/panic-handler/panic-handler-std.rs | 1 + .../ui/panic-handler/panic-handler-std.stderr | 6 ++++-- 10 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/librustc_metadata/rmeta/decoder/cstore_impl.rs b/src/librustc_metadata/rmeta/decoder/cstore_impl.rs index 1b168bf01178c..3472d8987c65c 100644 --- a/src/librustc_metadata/rmeta/decoder/cstore_impl.rs +++ b/src/librustc_metadata/rmeta/decoder/cstore_impl.rs @@ -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, @@ -513,4 +514,8 @@ impl CrateStore for CStore { fn allocator_kind(&self) -> Option { self.allocator_kind() } + + fn crate_extern_paths(&self, cnum: CrateNum) -> Vec { + self.get_crate_data(cnum).source().paths().cloned().collect() + } } diff --git a/src/librustc_middle/middle/cstore.rs b/src/librustc_middle/middle/cstore.rs index 97e877df96663..91754f458c891 100644 --- a/src/librustc_middle/middle/cstore.rs +++ b/src/librustc_middle/middle/cstore.rs @@ -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; + fn crate_extern_paths(&self, cnum: CrateNum) -> Vec; } pub type CrateStoreDyn = dyn CrateStore + sync::Sync; diff --git a/src/librustc_middle/ty/context.rs b/src/librustc_middle/ty/context.rs index e2f601371b1ee..e6c0cd24bff4d 100644 --- a/src/librustc_middle/ty/context.rs +++ b/src/librustc_middle/ty/context.rs @@ -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, ()>; @@ -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 { + 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() { diff --git a/src/librustc_passes/lang_items.rs b/src/librustc_passes/lang_items.rs index 0be37cb096038..d2635b8954502 100644 --- a/src/librustc_passes/lang_items.rs +++ b/src/librustc_passes/lang_items.rs @@ -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(); } diff --git a/src/test/ui/duplicate_entry_error.rs b/src/test/ui/duplicate_entry_error.rs index b8d98a8999b9d..776ecedea7e7e 100644 --- a/src/test/ui/duplicate_entry_error.rs +++ b/src/test/ui/duplicate_entry_error.rs @@ -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 diff --git a/src/test/ui/duplicate_entry_error.stderr b/src/test/ui/duplicate_entry_error.stderr index 2d52ea3f6c20e..93e4f9fa5e94b 100644 --- a/src/test/ui/duplicate_entry_error.stderr +++ b/src/test/ui/duplicate_entry_error.stderr @@ -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 | | @@ -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 diff --git a/src/test/ui/error-codes/E0152.rs b/src/test/ui/error-codes/E0152.rs index 94467b9bddeb0..d716ca1a14fdf 100644 --- a/src/test/ui/error-codes/E0152.rs +++ b/src/test/ui/error-codes/E0152.rs @@ -1,3 +1,4 @@ +// normalize-stderr-test "loaded from .*liballoc-.*.rlib" -> "loaded from SYSROOT/liballoc-*.rlib" #![feature(lang_items)] #[lang = "owned_box"] diff --git a/src/test/ui/error-codes/E0152.stderr b/src/test/ui/error-codes/E0152.stderr index fbaa276ce1093..5520b5454f9ed 100644 --- a/src/test/ui/error-codes/E0152.stderr +++ b/src/test/ui/error-codes/E0152.stderr @@ -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 diff --git a/src/test/ui/panic-handler/panic-handler-std.rs b/src/test/ui/panic-handler/panic-handler-std.rs index 0acc2722cb21f..6183c886cfac7 100644 --- a/src/test/ui/panic-handler/panic-handler-std.rs +++ b/src/test/ui/panic-handler/panic-handler-std.rs @@ -1,3 +1,4 @@ +// normalize-stderr-test "loaded from .*libstd-.*.rlib" -> "loaded from SYSROOT/libstd-*.rlib" // error-pattern: found duplicate lang item `panic_impl` diff --git a/src/test/ui/panic-handler/panic-handler-std.stderr b/src/test/ui/panic-handler/panic-handler-std.stderr index f71c28e5aa641..1cba0ac3b9aaf 100644 --- a/src/test/ui/panic-handler/panic-handler-std.stderr +++ b/src/test/ui/panic-handler/panic-handler-std.stderr @@ -1,5 +1,5 @@ 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 {} @@ -7,9 +7,11 @@ 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) -> ! { | ^^^^^^^^^ From 1b3ef660261c880783db0154b89c09a1c4608845 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 27 Jun 2020 14:54:19 -0700 Subject: [PATCH 2/2] Switch crate_extern_paths to a query, and tweak wording. --- .../rmeta/decoder/cstore_impl.rs | 7 ++----- src/librustc_middle/middle/cstore.rs | 1 - src/librustc_middle/query/mod.rs | 4 ++++ src/librustc_middle/ty/context.rs | 9 --------- src/librustc_middle/ty/query/mod.rs | 1 + src/librustc_passes/lang_items.rs | 19 ++++++++++--------- src/test/ui/duplicate_entry_error.stderr | 2 +- src/test/ui/error-codes/E0152.stderr | 2 +- .../ui/panic-handler/panic-handler-std.stderr | 2 +- 9 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/librustc_metadata/rmeta/decoder/cstore_impl.rs b/src/librustc_metadata/rmeta/decoder/cstore_impl.rs index 3472d8987c65c..abbe45fe02e25 100644 --- a/src/librustc_metadata/rmeta/decoder/cstore_impl.rs +++ b/src/librustc_metadata/rmeta/decoder/cstore_impl.rs @@ -27,7 +27,6 @@ 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, @@ -240,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<'_>) { @@ -514,8 +515,4 @@ impl CrateStore for CStore { fn allocator_kind(&self) -> Option { self.allocator_kind() } - - fn crate_extern_paths(&self, cnum: CrateNum) -> Vec { - self.get_crate_data(cnum).source().paths().cloned().collect() - } } diff --git a/src/librustc_middle/middle/cstore.rs b/src/librustc_middle/middle/cstore.rs index 91754f458c891..97e877df96663 100644 --- a/src/librustc_middle/middle/cstore.rs +++ b/src/librustc_middle/middle/cstore.rs @@ -203,7 +203,6 @@ pub trait CrateStore { fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata; fn metadata_encoding_version(&self) -> &[u8]; fn allocator_kind(&self) -> Option; - fn crate_extern_paths(&self, cnum: CrateNum) -> Vec; } pub type CrateStoreDyn = dyn CrateStore + sync::Sync; diff --git a/src/librustc_middle/query/mod.rs b/src/librustc_middle/query/mod.rs index ba5a8c3ec2052..e7f9ad9d1cfd7 100644 --- a/src/librustc_middle/query/mod.rs +++ b/src/librustc_middle/query/mod.rs @@ -1042,6 +1042,10 @@ rustc_queries! { eval_always desc { "looking up the extra filename for a crate" } } + query crate_extern_paths(_: CrateNum) -> Vec { + eval_always + desc { "looking up the paths for extern crates" } + } } TypeChecking { diff --git a/src/librustc_middle/ty/context.rs b/src/librustc_middle/ty/context.rs index e6c0cd24bff4d..e2f601371b1ee 100644 --- a/src/librustc_middle/ty/context.rs +++ b/src/librustc_middle/ty/context.rs @@ -62,7 +62,6 @@ 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, ()>; @@ -1253,14 +1252,6 @@ 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 { - 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() { diff --git a/src/librustc_middle/ty/query/mod.rs b/src/librustc_middle/ty/query/mod.rs index 35d19b7603faf..2ad49b1acce43 100644 --- a/src/librustc_middle/ty/query/mod.rs +++ b/src/librustc_middle/ty/query/mod.rs @@ -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] diff --git a/src/librustc_passes/lang_items.rs b/src/librustc_passes/lang_items.rs index d2635b8954502..0326591a931f5 100644 --- a/src/librustc_passes/lang_items.rs +++ b/src/librustc_passes/lang_items.rs @@ -147,8 +147,9 @@ impl LanguageItemCollector<'tcx> { } } let mut note_def = |which, def_id: DefId| { - let location = if def_id.is_local() { - "the local crate".to_string() + 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 @@ -156,14 +157,14 @@ impl LanguageItemCollector<'tcx> { .iter() .map(|p| p.display().to_string()) .collect(); - paths.join(", ") + format!( + "{} definition in `{}` loaded from {}", + which, + crate_name, + paths.join(", ") + ) }; - err.note(&format!( - "{} definition in `{}` loaded from {}", - which, - self.tcx.crate_name(def_id.krate), - location - )); + err.note(¬e); }; note_def("first", original_def_id); note_def("second", item_def_id); diff --git a/src/test/ui/duplicate_entry_error.stderr b/src/test/ui/duplicate_entry_error.stderr index 93e4f9fa5e94b..61cccf40ed8a5 100644 --- a/src/test/ui/duplicate_entry_error.stderr +++ b/src/test/ui/duplicate_entry_error.stderr @@ -9,7 +9,7 @@ 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 + = note: second definition in the local crate (`duplicate_entry_error`) error: aborting due to previous error diff --git a/src/test/ui/error-codes/E0152.stderr b/src/test/ui/error-codes/E0152.stderr index 5520b5454f9ed..7445c2880af1c 100644 --- a/src/test/ui/error-codes/E0152.stderr +++ b/src/test/ui/error-codes/E0152.stderr @@ -6,7 +6,7 @@ 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 + = note: second definition in the local crate (`E0152`) error: aborting due to previous error diff --git a/src/test/ui/panic-handler/panic-handler-std.stderr b/src/test/ui/panic-handler/panic-handler-std.stderr index 1cba0ac3b9aaf..bb656089bcaff 100644 --- a/src/test/ui/panic-handler/panic-handler-std.stderr +++ b/src/test/ui/panic-handler/panic-handler-std.stderr @@ -8,7 +8,7 @@ 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 + = note: second definition in the local crate (`panic_handler_std`) error: argument should be `&PanicInfo` --> $DIR/panic-handler-std.rs:8:16