@@ -12,7 +12,7 @@ use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder}
1212use rustc_hir as hir;
1313use rustc_hir:: def:: Namespace :: { self , * } ;
1414use 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 } ;
1616use rustc_hir:: PrimTy ;
1717use rustc_session:: config:: nightly_options;
1818use rustc_span:: hygiene:: MacroKind ;
@@ -88,6 +88,18 @@ fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, Str
8888}
8989
9090impl < ' 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) ) ;
0 commit comments