Point at item definition in foreign crates

This commit is contained in:
Esteban Küber 2020-08-09 21:14:59 -07:00
parent 089810a1cb
commit 6c0755a7dc
9 changed files with 56 additions and 12 deletions

View File

@ -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<Span> {
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));

View File

@ -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
|

View File

@ -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
|

View File

@ -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
|

View File

@ -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
|

View File

@ -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

View File

@ -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
|

View File

@ -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

View File

@ -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