diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs index 1ea0a6ff2185..38bbbb1db1b0 100644 --- a/src/librustc_resolve/late/diagnostics.rs +++ b/src/librustc_resolve/late/diagnostics.rs @@ -12,7 +12,7 @@ use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder} use rustc_hir as hir; use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::{self, CtorKind, DefKind}; -use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::PrimTy; use rustc_session::config::nightly_options; use rustc_span::hygiene::MacroKind; @@ -88,6 +88,18 @@ fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, Str } impl<'a> LateResolutionVisitor<'a, '_, '_> { + fn def_span(&self, def_id: DefId) -> Option { + match def_id.krate { + LOCAL_CRATE => self.r.opt_span(def_id), + _ => Some( + self.r + .session + .source_map() + .guess_head_span(self.r.cstore().get_span_untracked(def_id, self.r.session)), + ), + } + } + /// Handles error reporting for `smart_resolve_path_fragment` function. /// Creates base error and amends it with one short label and possibly some longer helps/notes. pub(crate) fn smart_resolve_report_errors( @@ -552,7 +564,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> { } _ => span, }; - if let Some(span) = self.r.opt_span(def_id) { + if let Some(span) = self.def_span(def_id) { err.span_label(span, &format!("`{}` defined here", path_str)); } let (tail, descr, applicability) = match source { @@ -604,7 +616,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> { if nightly_options::is_nightly_build() { let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \ `type` alias"; - if let Some(span) = self.r.opt_span(def_id) { + if let Some(span) = self.def_span(def_id) { err.span_help(span, msg); } else { err.help(msg); @@ -682,7 +694,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> { bad_struct_syntax_suggestion(def_id); } (Res::Def(DefKind::Ctor(_, CtorKind::Fn), def_id), _) if ns == ValueNS => { - if let Some(span) = self.r.opt_span(def_id) { + if let Some(span) = self.def_span(def_id) { err.span_label(span, &format!("`{}` defined here", path_str)); } err.span_label(span, format!("did you mean `{}( /* fields */ )`?", path_str)); diff --git a/src/test/ui/empty/empty-struct-braces-expr.stderr b/src/test/ui/empty/empty-struct-braces-expr.stderr index af783996f76c..6292ed446978 100644 --- a/src/test/ui/empty/empty-struct-braces-expr.stderr +++ b/src/test/ui/empty/empty-struct-braces-expr.stderr @@ -68,8 +68,10 @@ error[E0423]: expected value, found struct `XEmpty1` LL | let xe1 = XEmpty1; | ^^^^^^^ | - ::: $DIR/auxiliary/empty-struct.rs:2:1 + ::: $DIR/auxiliary/empty-struct.rs:1:1 | +LL | pub struct XEmpty1 {} + | ------------------ `XEmpty1` defined here LL | pub struct XEmpty2; | ------------------- similarly named unit struct `XEmpty2` defined here | @@ -88,8 +90,10 @@ error[E0423]: expected function, tuple struct or tuple variant, found struct `XE LL | let xe1 = XEmpty1(); | ^^^^^^^^^ | - ::: $DIR/auxiliary/empty-struct.rs:2:1 + ::: $DIR/auxiliary/empty-struct.rs:1:1 | +LL | pub struct XEmpty1 {} + | ------------------ `XEmpty1` defined here LL | pub struct XEmpty2; | ------------------- similarly named unit struct `XEmpty2` defined here | diff --git a/src/test/ui/empty/empty-struct-braces-pat-1.stderr b/src/test/ui/empty/empty-struct-braces-pat-1.stderr index 70847ca45d0f..3570012fc375 100644 --- a/src/test/ui/empty/empty-struct-braces-pat-1.stderr +++ b/src/test/ui/empty/empty-struct-braces-pat-1.stderr @@ -13,8 +13,10 @@ error[E0532]: expected unit struct, unit variant or constant, found struct varia LL | XE::XEmpty3 => () | ^^^^^^^^^^^ | - ::: $DIR/auxiliary/empty-struct.rs:7:5 + ::: $DIR/auxiliary/empty-struct.rs:6:5 | +LL | XEmpty3 {}, + | ------- `XE::XEmpty3` defined here LL | XEmpty4, | ------- similarly named unit variant `XEmpty4` defined here | diff --git a/src/test/ui/empty/empty-struct-braces-pat-2.stderr b/src/test/ui/empty/empty-struct-braces-pat-2.stderr index 19792bc9fc5b..3bd3f6a9644a 100644 --- a/src/test/ui/empty/empty-struct-braces-pat-2.stderr +++ b/src/test/ui/empty/empty-struct-braces-pat-2.stderr @@ -27,8 +27,11 @@ error[E0532]: expected tuple struct or tuple variant, found struct `XEmpty1` LL | XEmpty1() => () | ^^^^^^^^^ | - ::: $DIR/auxiliary/empty-struct.rs:3:1 + ::: $DIR/auxiliary/empty-struct.rs:1:1 | +LL | pub struct XEmpty1 {} + | ------------------ `XEmpty1` defined here +LL | pub struct XEmpty2; LL | pub struct XEmpty6(); | --------------------- similarly named tuple struct `XEmpty6` defined here | @@ -70,8 +73,11 @@ error[E0532]: expected tuple struct or tuple variant, found struct `XEmpty1` LL | XEmpty1(..) => () | ^^^^^^^^^^^ | - ::: $DIR/auxiliary/empty-struct.rs:3:1 + ::: $DIR/auxiliary/empty-struct.rs:1:1 | +LL | pub struct XEmpty1 {} + | ------------------ `XEmpty1` defined here +LL | pub struct XEmpty2; LL | pub struct XEmpty6(); | --------------------- similarly named tuple struct `XEmpty6` defined here | diff --git a/src/test/ui/empty/empty-struct-braces-pat-3.stderr b/src/test/ui/empty/empty-struct-braces-pat-3.stderr index 066c42d8181b..aac872ba0ecc 100644 --- a/src/test/ui/empty/empty-struct-braces-pat-3.stderr +++ b/src/test/ui/empty/empty-struct-braces-pat-3.stderr @@ -13,8 +13,11 @@ error[E0532]: expected tuple struct or tuple variant, found struct variant `XE:: LL | XE::XEmpty3() => () | ^^^^^^^^^^^^^ | - ::: $DIR/auxiliary/empty-struct.rs:8:5 + ::: $DIR/auxiliary/empty-struct.rs:6:5 | +LL | XEmpty3 {}, + | ------- `XE::XEmpty3` defined here +LL | XEmpty4, LL | XEmpty5(), | --------- similarly named tuple variant `XEmpty5` defined here | @@ -42,8 +45,11 @@ error[E0532]: expected tuple struct or tuple variant, found struct variant `XE:: LL | XE::XEmpty3(..) => () | ^^^^^^^^^^^^^^^ | - ::: $DIR/auxiliary/empty-struct.rs:8:5 + ::: $DIR/auxiliary/empty-struct.rs:6:5 | +LL | XEmpty3 {}, + | ------- `XE::XEmpty3` defined here +LL | XEmpty4, LL | XEmpty5(), | --------- similarly named tuple variant `XEmpty5` defined here | diff --git a/src/test/ui/empty/empty-struct-tuple-pat.stderr b/src/test/ui/empty/empty-struct-tuple-pat.stderr index 9388ed266571..7b21d31635cf 100644 --- a/src/test/ui/empty/empty-struct-tuple-pat.stderr +++ b/src/test/ui/empty/empty-struct-tuple-pat.stderr @@ -38,6 +38,8 @@ LL | XE::XEmpty5 => (), | LL | XEmpty4, | ------- similarly named unit variant `XEmpty4` defined here +LL | XEmpty5(), + | --------- `XE::XEmpty5` defined here error: aborting due to 4 previous errors diff --git a/src/test/ui/namespace/namespace-mix.stderr b/src/test/ui/namespace/namespace-mix.stderr index 2e25ffa168f2..ded3173f45b2 100644 --- a/src/test/ui/namespace/namespace-mix.stderr +++ b/src/test/ui/namespace/namespace-mix.stderr @@ -74,8 +74,10 @@ error[E0423]: expected value, found struct variant `xm7::V` LL | check(xm7::V); | ^^^^^^ | - ::: $DIR/auxiliary/namespace-mix.rs:7:9 + ::: $DIR/auxiliary/namespace-mix.rs:6:9 | +LL | V {}, + | - `xm7::V` defined here LL | TV(), | ---- similarly named tuple variant `TV` defined here | diff --git a/src/test/ui/resolve/issue-19452.stderr b/src/test/ui/resolve/issue-19452.stderr index d1690d4eef7e..6c519d734802 100644 --- a/src/test/ui/resolve/issue-19452.stderr +++ b/src/test/ui/resolve/issue-19452.stderr @@ -12,6 +12,11 @@ error[E0423]: expected value, found struct variant `issue_19452_aux::Homura::Mad | LL | let homura = issue_19452_aux::Homura::Madoka; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use struct literal syntax instead: `issue_19452_aux::Homura::Madoka { /* fields */ }` + | + ::: $DIR/auxiliary/issue-19452-aux.rs:2:5 + | +LL | Madoka { age: u32 } + | ------ `issue_19452_aux::Homura::Madoka` defined here error: aborting due to 2 previous errors diff --git a/src/test/ui/xcrate/xcrate-unit-struct.stderr b/src/test/ui/xcrate/xcrate-unit-struct.stderr index 813d5d4fdb12..3dc8b90795c4 100644 --- a/src/test/ui/xcrate/xcrate-unit-struct.stderr +++ b/src/test/ui/xcrate/xcrate-unit-struct.stderr @@ -3,6 +3,11 @@ error[E0423]: expected value, found struct `xcrate_unit_struct::StructWithFields | LL | let _ = xcrate_unit_struct::StructWithFields; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use struct literal syntax instead: `xcrate_unit_struct::StructWithFields { foo: val }` + | + ::: $DIR/auxiliary/xcrate_unit_struct.rs:20:1 + | +LL | pub struct StructWithFields { + | --------------------------- `xcrate_unit_struct::StructWithFields` defined here error: aborting due to previous error