@@ -65,7 +65,7 @@ use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind};
6565use syntax:: ast:: { Local , Mutability , Pat , PatKind , Path } ;
6666use syntax:: ast:: { PathSegment , PathParameters , QSelf , TraitItemKind , TraitRef , Ty , TyKind } ;
6767
68- use syntax_pos:: { Span , DUMMY_SP } ;
68+ use syntax_pos:: { Span , DUMMY_SP , MultiSpan } ;
6969use errors:: DiagnosticBuilder ;
7070
7171use std:: cell:: { Cell , RefCell } ;
@@ -897,6 +897,7 @@ enum NameBindingKind<'a> {
897897 Ambiguity {
898898 b1 : & ' a NameBinding < ' a > ,
899899 b2 : & ' a NameBinding < ' a > ,
900+ legacy : bool ,
900901 }
901902}
902903
@@ -908,13 +909,15 @@ struct AmbiguityError<'a> {
908909 lexical : bool ,
909910 b1 : & ' a NameBinding < ' a > ,
910911 b2 : & ' a NameBinding < ' a > ,
912+ legacy : bool ,
911913}
912914
913915impl < ' a > NameBinding < ' a > {
914916 fn module ( & self ) -> Option < Module < ' a > > {
915917 match self . kind {
916918 NameBindingKind :: Module ( module) => Some ( module) ,
917919 NameBindingKind :: Import { binding, .. } => binding. module ( ) ,
920+ NameBindingKind :: Ambiguity { legacy : true , b1, .. } => b1. module ( ) ,
918921 _ => None ,
919922 }
920923 }
@@ -924,6 +927,7 @@ impl<'a> NameBinding<'a> {
924927 NameBindingKind :: Def ( def) => def,
925928 NameBindingKind :: Module ( module) => module. def ( ) . unwrap ( ) ,
926929 NameBindingKind :: Import { binding, .. } => binding. def ( ) ,
930+ NameBindingKind :: Ambiguity { legacy : true , b1, .. } => b1. def ( ) ,
927931 NameBindingKind :: Ambiguity { .. } => Def :: Err ,
928932 }
929933 }
@@ -1350,11 +1354,14 @@ impl<'a> Resolver<'a> {
13501354 self . record_use ( name, ns, binding, span)
13511355 }
13521356 NameBindingKind :: Import { .. } => false ,
1353- NameBindingKind :: Ambiguity { b1, b2 } => {
1357+ NameBindingKind :: Ambiguity { b1, b2, legacy } => {
13541358 self . ambiguity_errors . push ( AmbiguityError {
1355- span : span, name : name, lexical : false , b1 : b1, b2 : b2,
1359+ span : span, name : name, lexical : false , b1 : b1, b2 : b2, legacy : legacy ,
13561360 } ) ;
1357- true
1361+ if legacy {
1362+ self . record_use ( name, ns, b1, span) ;
1363+ }
1364+ !legacy
13581365 }
13591366 _ => false
13601367 }
@@ -3064,26 +3071,39 @@ impl<'a> Resolver<'a> {
30643071 self . report_shadowing_errors ( ) ;
30653072 let mut reported_spans = FxHashSet ( ) ;
30663073
3067- for & AmbiguityError { span, name, b1, b2, lexical } in & self . ambiguity_errors {
3074+ for & AmbiguityError { span, name, b1, b2, lexical, legacy } in & self . ambiguity_errors {
30683075 if !reported_spans. insert ( span) { continue }
30693076 let participle = |binding : & NameBinding | {
30703077 if binding. is_import ( ) { "imported" } else { "defined" }
30713078 } ;
30723079 let msg1 = format ! ( "`{}` could resolve to the name {} here" , name, participle( b1) ) ;
30733080 let msg2 = format ! ( "`{}` could also resolve to the name {} here" , name, participle( b2) ) ;
3074- self . session . struct_span_err ( span, & format ! ( "`{}` is ambiguous" , name) )
3075- . span_note ( b1. span , & msg1)
3076- . span_note ( b2. span , & msg2)
3077- . note ( & if !lexical && b1. is_glob_import ( ) {
3078- format ! ( "consider adding an explicit import of `{}` to disambiguate" , name)
3079- } else if let Def :: Macro ( ..) = b1. def ( ) {
3080- format ! ( "macro-expanded {} do not shadow" ,
3081- if b1. is_import( ) { "macro imports" } else { "macros" } )
3082- } else {
3083- format ! ( "macro-expanded {} do not shadow when used in a macro invocation path" ,
3084- if b1. is_import( ) { "imports" } else { "items" } )
3085- } )
3086- . emit ( ) ;
3081+ let note = if !lexical && b1. is_glob_import ( ) {
3082+ format ! ( "consider adding an explicit import of `{}` to disambiguate" , name)
3083+ } else if let Def :: Macro ( ..) = b1. def ( ) {
3084+ format ! ( "macro-expanded {} do not shadow" ,
3085+ if b1. is_import( ) { "macro imports" } else { "macros" } )
3086+ } else {
3087+ format ! ( "macro-expanded {} do not shadow when used in a macro invocation path" ,
3088+ if b1. is_import( ) { "imports" } else { "items" } )
3089+ } ;
3090+ if legacy {
3091+ let id = match b2. kind {
3092+ NameBindingKind :: Import { directive, .. } => directive. id ,
3093+ _ => unreachable ! ( ) ,
3094+ } ;
3095+ let mut span = MultiSpan :: from_span ( span) ;
3096+ span. push_span_label ( b1. span , msg1) ;
3097+ span. push_span_label ( b2. span , msg2) ;
3098+ let msg = format ! ( "`{}` is ambiguous" , name) ;
3099+ self . session . add_lint ( lint:: builtin:: LEGACY_IMPORTS , id, span, msg) ;
3100+ } else {
3101+ self . session . struct_span_err ( span, & format ! ( "`{}` is ambiguous" , name) )
3102+ . span_note ( b1. span , & msg1)
3103+ . span_note ( b2. span , & msg2)
3104+ . note ( & note)
3105+ . emit ( ) ;
3106+ }
30873107 }
30883108
30893109 for & PrivacyError ( span, name, binding) in & self . privacy_errors {
0 commit comments