@@ -64,7 +64,7 @@ use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind};
6464use syntax:: ast:: { Local , Mutability , Pat , PatKind , Path } ;
6565use syntax:: ast:: { PathSegment , PathParameters , QSelf , TraitItemKind , TraitRef , Ty , TyKind } ;
6666
67- use syntax_pos:: { Span , DUMMY_SP } ;
67+ use syntax_pos:: { Span , DUMMY_SP , MultiSpan } ;
6868use errors:: DiagnosticBuilder ;
6969
7070use std:: cell:: { Cell , RefCell } ;
@@ -896,6 +896,7 @@ enum NameBindingKind<'a> {
896896 Ambiguity {
897897 b1 : & ' a NameBinding < ' a > ,
898898 b2 : & ' a NameBinding < ' a > ,
899+ legacy : bool ,
899900 }
900901}
901902
@@ -907,13 +908,15 @@ struct AmbiguityError<'a> {
907908 lexical : bool ,
908909 b1 : & ' a NameBinding < ' a > ,
909910 b2 : & ' a NameBinding < ' a > ,
911+ legacy : bool ,
910912}
911913
912914impl < ' a > NameBinding < ' a > {
913915 fn module ( & self ) -> Option < Module < ' a > > {
914916 match self . kind {
915917 NameBindingKind :: Module ( module) => Some ( module) ,
916918 NameBindingKind :: Import { binding, .. } => binding. module ( ) ,
919+ NameBindingKind :: Ambiguity { legacy : true , b1, .. } => b1. module ( ) ,
917920 _ => None ,
918921 }
919922 }
@@ -923,6 +926,7 @@ impl<'a> NameBinding<'a> {
923926 NameBindingKind :: Def ( def) => def,
924927 NameBindingKind :: Module ( module) => module. def ( ) . unwrap ( ) ,
925928 NameBindingKind :: Import { binding, .. } => binding. def ( ) ,
929+ NameBindingKind :: Ambiguity { legacy : true , b1, .. } => b1. def ( ) ,
926930 NameBindingKind :: Ambiguity { .. } => Def :: Err ,
927931 }
928932 }
@@ -1349,11 +1353,14 @@ impl<'a> Resolver<'a> {
13491353 self . record_use ( name, ns, binding, span)
13501354 }
13511355 NameBindingKind :: Import { .. } => false ,
1352- NameBindingKind :: Ambiguity { b1, b2 } => {
1356+ NameBindingKind :: Ambiguity { b1, b2, legacy } => {
13531357 self . ambiguity_errors . push ( AmbiguityError {
1354- span : span, name : name, lexical : false , b1 : b1, b2 : b2,
1358+ span : span, name : name, lexical : false , b1 : b1, b2 : b2, legacy : legacy ,
13551359 } ) ;
1356- true
1360+ if legacy {
1361+ self . record_use ( name, ns, b1, span) ;
1362+ }
1363+ !legacy
13571364 }
13581365 _ => false
13591366 }
@@ -3065,26 +3072,39 @@ impl<'a> Resolver<'a> {
30653072 self . report_shadowing_errors ( ) ;
30663073 let mut reported_spans = FxHashSet ( ) ;
30673074
3068- for & AmbiguityError { span, name, b1, b2, lexical } in & self . ambiguity_errors {
3075+ for & AmbiguityError { span, name, b1, b2, lexical, legacy } in & self . ambiguity_errors {
30693076 if !reported_spans. insert ( span) { continue }
30703077 let participle = |binding : & NameBinding | {
30713078 if binding. is_import ( ) { "imported" } else { "defined" }
30723079 } ;
30733080 let msg1 = format ! ( "`{}` could resolve to the name {} here" , name, participle( b1) ) ;
30743081 let msg2 = format ! ( "`{}` could also resolve to the name {} here" , name, participle( b2) ) ;
3075- self . session . struct_span_err ( span, & format ! ( "`{}` is ambiguous" , name) )
3076- . span_note ( b1. span , & msg1)
3077- . span_note ( b2. span , & msg2)
3078- . note ( & if !lexical && b1. is_glob_import ( ) {
3079- format ! ( "consider adding an explicit import of `{}` to disambiguate" , name)
3080- } else if let Def :: Macro ( ..) = b1. def ( ) {
3081- format ! ( "macro-expanded {} do not shadow" ,
3082- if b1. is_import( ) { "macro imports" } else { "macros" } )
3083- } else {
3084- format ! ( "macro-expanded {} do not shadow when used in a macro invocation path" ,
3085- if b1. is_import( ) { "imports" } else { "items" } )
3086- } )
3087- . emit ( ) ;
3082+ let note = if !lexical && b1. is_glob_import ( ) {
3083+ format ! ( "consider adding an explicit import of `{}` to disambiguate" , name)
3084+ } else if let Def :: Macro ( ..) = b1. def ( ) {
3085+ format ! ( "macro-expanded {} do not shadow" ,
3086+ if b1. is_import( ) { "macro imports" } else { "macros" } )
3087+ } else {
3088+ format ! ( "macro-expanded {} do not shadow when used in a macro invocation path" ,
3089+ if b1. is_import( ) { "imports" } else { "items" } )
3090+ } ;
3091+ if legacy {
3092+ let id = match b2. kind {
3093+ NameBindingKind :: Import { directive, .. } => directive. id ,
3094+ _ => unreachable ! ( ) ,
3095+ } ;
3096+ let mut span = MultiSpan :: from_span ( span) ;
3097+ span. push_span_label ( b1. span , msg1) ;
3098+ span. push_span_label ( b2. span , msg2) ;
3099+ let msg = format ! ( "`{}` is ambiguous" , name) ;
3100+ self . session . add_lint ( lint:: builtin:: LEGACY_IMPORTS , id, span, msg) ;
3101+ } else {
3102+ self . session . struct_span_err ( span, & format ! ( "`{}` is ambiguous" , name) )
3103+ . span_note ( b1. span , & msg1)
3104+ . span_note ( b2. span , & msg2)
3105+ . note ( & note)
3106+ . emit ( ) ;
3107+ }
30883108 }
30893109
30903110 for & PrivacyError ( span, name, binding) in & self . privacy_errors {
0 commit comments