Skip to content

Commit 6c0755a

Browse files
committed
Point at item definition in foreign crates
1 parent 089810a commit 6c0755a

9 files changed

+56
-12
lines changed

src/librustc_resolve/late/diagnostics.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder}
1212
use rustc_hir as hir;
1313
use rustc_hir::def::Namespace::{self, *};
1414
use rustc_hir::def::{self, CtorKind, DefKind};
15-
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
15+
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1616
use rustc_hir::PrimTy;
1717
use rustc_session::config::nightly_options;
1818
use rustc_span::hygiene::MacroKind;
@@ -88,6 +88,18 @@ fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, Str
8888
}
8989

9090
impl<'a> LateResolutionVisitor<'a, '_, '_> {
91+
fn def_span(&self, def_id: DefId) -> Option<Span> {
92+
match def_id.krate {
93+
LOCAL_CRATE => self.r.opt_span(def_id),
94+
_ => Some(
95+
self.r
96+
.session
97+
.source_map()
98+
.guess_head_span(self.r.cstore().get_span_untracked(def_id, self.r.session)),
99+
),
100+
}
101+
}
102+
91103
/// Handles error reporting for `smart_resolve_path_fragment` function.
92104
/// Creates base error and amends it with one short label and possibly some longer helps/notes.
93105
pub(crate) fn smart_resolve_report_errors(
@@ -552,7 +564,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
552564
}
553565
_ => span,
554566
};
555-
if let Some(span) = self.r.opt_span(def_id) {
567+
if let Some(span) = self.def_span(def_id) {
556568
err.span_label(span, &format!("`{}` defined here", path_str));
557569
}
558570
let (tail, descr, applicability) = match source {
@@ -604,7 +616,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
604616
if nightly_options::is_nightly_build() {
605617
let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
606618
`type` alias";
607-
if let Some(span) = self.r.opt_span(def_id) {
619+
if let Some(span) = self.def_span(def_id) {
608620
err.span_help(span, msg);
609621
} else {
610622
err.help(msg);
@@ -682,7 +694,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
682694
bad_struct_syntax_suggestion(def_id);
683695
}
684696
(Res::Def(DefKind::Ctor(_, CtorKind::Fn), def_id), _) if ns == ValueNS => {
685-
if let Some(span) = self.r.opt_span(def_id) {
697+
if let Some(span) = self.def_span(def_id) {
686698
err.span_label(span, &format!("`{}` defined here", path_str));
687699
}
688700
err.span_label(span, format!("did you mean `{}( /* fields */ )`?", path_str));

src/test/ui/empty/empty-struct-braces-expr.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ error[E0423]: expected value, found struct `XEmpty1`
6868
LL | let xe1 = XEmpty1;
6969
| ^^^^^^^
7070
|
71-
::: $DIR/auxiliary/empty-struct.rs:2:1
71+
::: $DIR/auxiliary/empty-struct.rs:1:1
7272
|
73+
LL | pub struct XEmpty1 {}
74+
| ------------------ `XEmpty1` defined here
7375
LL | pub struct XEmpty2;
7476
| ------------------- similarly named unit struct `XEmpty2` defined here
7577
|
@@ -88,8 +90,10 @@ error[E0423]: expected function, tuple struct or tuple variant, found struct `XE
8890
LL | let xe1 = XEmpty1();
8991
| ^^^^^^^^^
9092
|
91-
::: $DIR/auxiliary/empty-struct.rs:2:1
93+
::: $DIR/auxiliary/empty-struct.rs:1:1
9294
|
95+
LL | pub struct XEmpty1 {}
96+
| ------------------ `XEmpty1` defined here
9397
LL | pub struct XEmpty2;
9498
| ------------------- similarly named unit struct `XEmpty2` defined here
9599
|

src/test/ui/empty/empty-struct-braces-pat-1.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ error[E0532]: expected unit struct, unit variant or constant, found struct varia
1313
LL | XE::XEmpty3 => ()
1414
| ^^^^^^^^^^^
1515
|
16-
::: $DIR/auxiliary/empty-struct.rs:7:5
16+
::: $DIR/auxiliary/empty-struct.rs:6:5
1717
|
18+
LL | XEmpty3 {},
19+
| ------- `XE::XEmpty3` defined here
1820
LL | XEmpty4,
1921
| ------- similarly named unit variant `XEmpty4` defined here
2022
|

src/test/ui/empty/empty-struct-braces-pat-2.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ error[E0532]: expected tuple struct or tuple variant, found struct `XEmpty1`
2727
LL | XEmpty1() => ()
2828
| ^^^^^^^^^
2929
|
30-
::: $DIR/auxiliary/empty-struct.rs:3:1
30+
::: $DIR/auxiliary/empty-struct.rs:1:1
3131
|
32+
LL | pub struct XEmpty1 {}
33+
| ------------------ `XEmpty1` defined here
34+
LL | pub struct XEmpty2;
3235
LL | pub struct XEmpty6();
3336
| --------------------- similarly named tuple struct `XEmpty6` defined here
3437
|
@@ -70,8 +73,11 @@ error[E0532]: expected tuple struct or tuple variant, found struct `XEmpty1`
7073
LL | XEmpty1(..) => ()
7174
| ^^^^^^^^^^^
7275
|
73-
::: $DIR/auxiliary/empty-struct.rs:3:1
76+
::: $DIR/auxiliary/empty-struct.rs:1:1
7477
|
78+
LL | pub struct XEmpty1 {}
79+
| ------------------ `XEmpty1` defined here
80+
LL | pub struct XEmpty2;
7581
LL | pub struct XEmpty6();
7682
| --------------------- similarly named tuple struct `XEmpty6` defined here
7783
|

src/test/ui/empty/empty-struct-braces-pat-3.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ error[E0532]: expected tuple struct or tuple variant, found struct variant `XE::
1313
LL | XE::XEmpty3() => ()
1414
| ^^^^^^^^^^^^^
1515
|
16-
::: $DIR/auxiliary/empty-struct.rs:8:5
16+
::: $DIR/auxiliary/empty-struct.rs:6:5
1717
|
18+
LL | XEmpty3 {},
19+
| ------- `XE::XEmpty3` defined here
20+
LL | XEmpty4,
1821
LL | XEmpty5(),
1922
| --------- similarly named tuple variant `XEmpty5` defined here
2023
|
@@ -42,8 +45,11 @@ error[E0532]: expected tuple struct or tuple variant, found struct variant `XE::
4245
LL | XE::XEmpty3(..) => ()
4346
| ^^^^^^^^^^^^^^^
4447
|
45-
::: $DIR/auxiliary/empty-struct.rs:8:5
48+
::: $DIR/auxiliary/empty-struct.rs:6:5
4649
|
50+
LL | XEmpty3 {},
51+
| ------- `XE::XEmpty3` defined here
52+
LL | XEmpty4,
4753
LL | XEmpty5(),
4854
| --------- similarly named tuple variant `XEmpty5` defined here
4955
|

src/test/ui/empty/empty-struct-tuple-pat.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ LL | XE::XEmpty5 => (),
3838
|
3939
LL | XEmpty4,
4040
| ------- similarly named unit variant `XEmpty4` defined here
41+
LL | XEmpty5(),
42+
| --------- `XE::XEmpty5` defined here
4143

4244
error: aborting due to 4 previous errors
4345

src/test/ui/namespace/namespace-mix.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ error[E0423]: expected value, found struct variant `xm7::V`
7474
LL | check(xm7::V);
7575
| ^^^^^^
7676
|
77-
::: $DIR/auxiliary/namespace-mix.rs:7:9
77+
::: $DIR/auxiliary/namespace-mix.rs:6:9
7878
|
79+
LL | V {},
80+
| - `xm7::V` defined here
7981
LL | TV(),
8082
| ---- similarly named tuple variant `TV` defined here
8183
|

src/test/ui/resolve/issue-19452.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ error[E0423]: expected value, found struct variant `issue_19452_aux::Homura::Mad
1212
|
1313
LL | let homura = issue_19452_aux::Homura::Madoka;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use struct literal syntax instead: `issue_19452_aux::Homura::Madoka { /* fields */ }`
15+
|
16+
::: $DIR/auxiliary/issue-19452-aux.rs:2:5
17+
|
18+
LL | Madoka { age: u32 }
19+
| ------ `issue_19452_aux::Homura::Madoka` defined here
1520

1621
error: aborting due to 2 previous errors
1722

src/test/ui/xcrate/xcrate-unit-struct.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0423]: expected value, found struct `xcrate_unit_struct::StructWithFields
33
|
44
LL | let _ = xcrate_unit_struct::StructWithFields;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use struct literal syntax instead: `xcrate_unit_struct::StructWithFields { foo: val }`
6+
|
7+
::: $DIR/auxiliary/xcrate_unit_struct.rs:20:1
8+
|
9+
LL | pub struct StructWithFields {
10+
| --------------------------- `xcrate_unit_struct::StructWithFields` defined here
611

712
error: aborting due to previous error
813

0 commit comments

Comments
 (0)