@@ -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 (
@@ -339,8 +351,6 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
339
351
340
352
// Try Levenshtein algorithm.
341
353
let typo_sugg = self . lookup_typo_candidate ( path, ns, is_expected, span) ;
342
- let levenshtein_worked = self . r . add_typo_suggestion ( & mut err, typo_sugg, ident_span) ;
343
-
344
354
// Try context-dependent help if relaxed lookup didn't work.
345
355
if let Some ( res) = res {
346
356
if self . smart_resolve_context_dependent_help (
@@ -351,14 +361,18 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
351
361
& path_str,
352
362
& fallback_label,
353
363
) {
364
+ // We do this to avoid losing a secondary span when we override the main error span.
365
+ self . r . add_typo_suggestion ( & mut err, typo_sugg, ident_span) ;
354
366
return ( err, candidates) ;
355
367
}
356
368
}
357
369
358
- // Fallback label.
359
- if !levenshtein_worked {
370
+ if !self . type_ascription_suggestion ( & mut err, base_span)
371
+ && !self . r . add_typo_suggestion ( & mut err, typo_sugg, ident_span)
372
+ {
373
+ // Fallback label.
360
374
err. span_label ( base_span, fallback_label) ;
361
- self . type_ascription_suggestion ( & mut err , base_span ) ;
375
+
362
376
match self . diagnostic_metadata . current_let_binding {
363
377
Some ( ( pat_sp, Some ( ty_sp) , None ) ) if ty_sp. contains ( base_span) && could_be_expr => {
364
378
err. span_suggestion_short (
@@ -518,6 +532,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
518
532
} ) ,
519
533
) if followed_by_brace => {
520
534
if let Some ( sp) = closing_brace {
535
+ err. span_label ( span, fallback_label) ;
521
536
err. multipart_suggestion (
522
537
"surround the struct literal with parentheses" ,
523
538
vec ! [
@@ -550,7 +565,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
550
565
}
551
566
_ => span,
552
567
} ;
553
- if let Some ( span) = self . r . opt_span ( def_id) {
568
+ if let Some ( span) = self . def_span ( def_id) {
554
569
err. span_label ( span, & format ! ( "`{}` defined here" , path_str) ) ;
555
570
}
556
571
let ( tail, descr, applicability) = match source {
@@ -581,12 +596,15 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
581
596
applicability,
582
597
) ;
583
598
}
584
- _ => { }
599
+ _ => {
600
+ err. span_label ( span, fallback_label) ;
601
+ }
585
602
}
586
603
} ;
587
604
588
605
match ( res, source) {
589
606
( Res :: Def ( DefKind :: Macro ( MacroKind :: Bang ) , _) , _) => {
607
+ err. span_label ( span, fallback_label) ;
590
608
err. span_suggestion_verbose (
591
609
span. shrink_to_hi ( ) ,
592
610
"use `!` to invoke the macro" ,
@@ -602,7 +620,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
602
620
if nightly_options:: is_nightly_build ( ) {
603
621
let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
604
622
`type` alias";
605
- if let Some ( span) = self . r . opt_span ( def_id) {
623
+ if let Some ( span) = self . def_span ( def_id) {
606
624
err. span_help ( span, msg) ;
607
625
} else {
608
626
err. help ( msg) ;
@@ -680,7 +698,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
680
698
bad_struct_syntax_suggestion ( def_id) ;
681
699
}
682
700
( Res :: Def ( DefKind :: Ctor ( _, CtorKind :: Fn ) , def_id) , _) if ns == ValueNS => {
683
- if let Some ( span) = self . r . opt_span ( def_id) {
701
+ if let Some ( span) = self . def_span ( def_id) {
684
702
err. span_label ( span, & format ! ( "`{}` defined here" , path_str) ) ;
685
703
}
686
704
err. span_label ( span, format ! ( "did you mean `{}( /* fields */ )`?" , path_str) ) ;
@@ -869,7 +887,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
869
887
start. to ( sm. next_point ( start) )
870
888
}
871
889
872
- fn type_ascription_suggestion ( & self , err : & mut DiagnosticBuilder < ' _ > , base_span : Span ) {
890
+ fn type_ascription_suggestion ( & self , err : & mut DiagnosticBuilder < ' _ > , base_span : Span ) -> bool {
873
891
let sm = self . r . session . source_map ( ) ;
874
892
let base_snippet = sm. span_to_snippet ( base_span) ;
875
893
if let Some ( & sp) = self . diagnostic_metadata . current_type_ascription . last ( ) {
@@ -939,9 +957,11 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
939
957
"expecting a type here because of type ascription" ,
940
958
) ;
941
959
}
960
+ return show_label;
942
961
}
943
962
}
944
963
}
964
+ false
945
965
}
946
966
947
967
fn find_module ( & mut self , def_id : DefId ) -> Option < ( Module < ' a > , ImportSuggestion ) > {
0 commit comments