@@ -12,7 +12,7 @@ use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder}
12
12
use rustc_hir as hir;
13
13
use rustc_hir:: def:: Namespace :: { self , * } ;
14
14
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 } ;
16
16
use rustc_hir:: PrimTy ;
17
17
use rustc_session:: config:: nightly_options;
18
18
use rustc_span:: hygiene:: MacroKind ;
@@ -88,6 +88,18 @@ fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, Str
88
88
}
89
89
90
90
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
+
91
103
/// Handles error reporting for `smart_resolve_path_fragment` function.
92
104
/// Creates base error and amends it with one short label and possibly some longer helps/notes.
93
105
pub ( crate ) fn smart_resolve_report_errors (
@@ -552,7 +564,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
552
564
}
553
565
_ => span,
554
566
} ;
555
- if let Some ( span) = self . r . opt_span ( def_id) {
567
+ if let Some ( span) = self . def_span ( def_id) {
556
568
err. span_label ( span, & format ! ( "`{}` defined here" , path_str) ) ;
557
569
}
558
570
let ( tail, descr, applicability) = match source {
@@ -604,7 +616,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
604
616
if nightly_options:: is_nightly_build ( ) {
605
617
let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
606
618
`type` alias";
607
- if let Some ( span) = self . r . opt_span ( def_id) {
619
+ if let Some ( span) = self . def_span ( def_id) {
608
620
err. span_help ( span, msg) ;
609
621
} else {
610
622
err. help ( msg) ;
@@ -682,7 +694,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
682
694
bad_struct_syntax_suggestion ( def_id) ;
683
695
}
684
696
( 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) {
686
698
err. span_label ( span, & format ! ( "`{}` defined here" , path_str) ) ;
687
699
}
688
700
err. span_label ( span, format ! ( "did you mean `{}( /* fields */ )`?" , path_str) ) ;
0 commit comments