@@ -540,6 +540,7 @@ pub enum Namespace {
540540pub struct PerNS < T > {
541541 value_ns : T ,
542542 type_ns : T ,
543+ macro_ns : Option < T > ,
543544}
544545
545546impl < T > :: std:: ops:: Index < Namespace > for PerNS < T > {
@@ -548,7 +549,7 @@ impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
548549 match ns {
549550 ValueNS => & self . value_ns ,
550551 TypeNS => & self . type_ns ,
551- MacroNS => unreachable ! ( ) ,
552+ MacroNS => self . macro_ns . as_ref ( ) . unwrap ( ) ,
552553 }
553554 }
554555}
@@ -558,7 +559,7 @@ impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
558559 match ns {
559560 ValueNS => & mut self . value_ns ,
560561 TypeNS => & mut self . type_ns ,
561- MacroNS => unreachable ! ( ) ,
562+ MacroNS => self . macro_ns . as_mut ( ) . unwrap ( ) ,
562563 }
563564 }
564565}
@@ -675,22 +676,14 @@ impl<'a> Visitor for Resolver<'a> {
675676
676677pub type ErrorMessage = Option < ( Span , String ) > ;
677678
678- #[ derive( Clone , PartialEq , Eq ) ]
679+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
679680pub enum ResolveResult < T > {
680681 Failed ( ErrorMessage ) , // Failed to resolve the name, optional helpful error message.
681682 Indeterminate , // Couldn't determine due to unresolved globs.
682683 Success ( T ) , // Successfully resolved the import.
683684}
684685
685686impl < T > ResolveResult < T > {
686- fn and_then < U , F : FnOnce ( T ) -> ResolveResult < U > > ( self , f : F ) -> ResolveResult < U > {
687- match self {
688- Failed ( msg) => Failed ( msg) ,
689- Indeterminate => Indeterminate ,
690- Success ( t) => f ( t) ,
691- }
692- }
693-
694687 fn success ( self ) -> Option < T > {
695688 match self {
696689 Success ( t) => Some ( t) ,
@@ -825,6 +818,7 @@ pub struct ModuleS<'a> {
825818 normal_ancestor_id : Option < NodeId > ,
826819
827820 resolutions : RefCell < FxHashMap < ( Name , Namespace ) , & ' a RefCell < NameResolution < ' a > > > > ,
821+ legacy_macro_resolutions : RefCell < Vec < ( Mark , Name , Span ) > > ,
828822
829823 // Macro invocations that can expand into items in this module.
830824 unresolved_invocations : RefCell < FxHashSet < Mark > > ,
@@ -852,6 +846,7 @@ impl<'a> ModuleS<'a> {
852846 kind : kind,
853847 normal_ancestor_id : None ,
854848 resolutions : RefCell :: new ( FxHashMap ( ) ) ,
849+ legacy_macro_resolutions : RefCell :: new ( Vec :: new ( ) ) ,
855850 unresolved_invocations : RefCell :: new ( FxHashSet ( ) ) ,
856851 no_implicit_prelude : false ,
857852 glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
@@ -943,6 +938,7 @@ struct PrivacyError<'a>(Span, Name, &'a NameBinding<'a>);
943938struct AmbiguityError < ' a > {
944939 span : Span ,
945940 name : Name ,
941+ lexical : bool ,
946942 b1 : & ' a NameBinding < ' a > ,
947943 b2 : & ' a NameBinding < ' a > ,
948944}
@@ -1001,7 +997,7 @@ impl<'a> NameBinding<'a> {
1001997 fn is_glob_import ( & self ) -> bool {
1002998 match self . kind {
1003999 NameBindingKind :: Import { directive, .. } => directive. is_glob ( ) ,
1004- NameBindingKind :: Ambiguity { .. } => true ,
1000+ NameBindingKind :: Ambiguity { b1 , .. } => b1 . is_glob_import ( ) ,
10051001 _ => false ,
10061002 }
10071003 }
@@ -1136,6 +1132,7 @@ pub struct Resolver<'a> {
11361132 arenas : & ' a ResolverArenas < ' a > ,
11371133 dummy_binding : & ' a NameBinding < ' a > ,
11381134 new_import_semantics : bool , // true if `#![feature(item_like_imports)]`
1135+ use_extern_macros : bool , // true if `#![feature(use_extern_macros)]`
11391136
11401137 pub exported_macros : Vec < ast:: MacroDef > ,
11411138 crate_loader : & ' a mut CrateLoader ,
@@ -1300,6 +1297,7 @@ impl<'a> Resolver<'a> {
13001297 ribs : PerNS {
13011298 value_ns : vec ! [ Rib :: new( ModuleRibKind ( graph_root) ) ] ,
13021299 type_ns : vec ! [ Rib :: new( ModuleRibKind ( graph_root) ) ] ,
1300+ macro_ns : None ,
13031301 } ,
13041302 label_ribs : Vec :: new ( ) ,
13051303
@@ -1336,6 +1334,7 @@ impl<'a> Resolver<'a> {
13361334 vis : ty:: Visibility :: Public ,
13371335 } ) ,
13381336 new_import_semantics : session. features . borrow ( ) . item_like_imports ,
1337+ use_extern_macros : session. features . borrow ( ) . use_extern_macros ,
13391338
13401339 exported_macros : Vec :: new ( ) ,
13411340 crate_loader : crate_loader,
@@ -1365,6 +1364,10 @@ impl<'a> Resolver<'a> {
13651364 PerNS {
13661365 type_ns : f ( self , TypeNS ) ,
13671366 value_ns : f ( self , ValueNS ) ,
1367+ macro_ns : match self . use_extern_macros {
1368+ true => Some ( f ( self , MacroNS ) ) ,
1369+ false => None ,
1370+ } ,
13681371 }
13691372 }
13701373
@@ -1403,8 +1406,9 @@ impl<'a> Resolver<'a> {
14031406 }
14041407 NameBindingKind :: Import { .. } => false ,
14051408 NameBindingKind :: Ambiguity { b1, b2 } => {
1406- let ambiguity_error = AmbiguityError { span : span, name : name, b1 : b1, b2 : b2 } ;
1407- self . ambiguity_errors . push ( ambiguity_error) ;
1409+ self . ambiguity_errors . push ( AmbiguityError {
1410+ span : span, name : name, lexical : false , b1 : b1, b2 : b2,
1411+ } ) ;
14081412 true
14091413 }
14101414 _ => false
@@ -1438,7 +1442,7 @@ impl<'a> Resolver<'a> {
14381442 -> ResolveResult < Module < ' a > > {
14391443 fn search_parent_externals < ' a > ( this : & mut Resolver < ' a > , needle : Name , module : Module < ' a > )
14401444 -> Option < Module < ' a > > {
1441- match this. resolve_name_in_module ( module, needle, TypeNS , false , None ) {
1445+ match this. resolve_name_in_module ( module, needle, TypeNS , false , false , None ) {
14421446 Success ( binding) if binding. is_extern_crate ( ) => Some ( module) ,
14431447 _ => if let ( & ModuleKind :: Def ( ..) , Some ( parent) ) = ( & module. kind , module. parent ) {
14441448 search_parent_externals ( this, needle, parent)
@@ -1456,7 +1460,7 @@ impl<'a> Resolver<'a> {
14561460 // modules as we go.
14571461 while index < module_path_len {
14581462 let name = module_path[ index] . name ;
1459- match self . resolve_name_in_module ( search_module, name, TypeNS , false , span) {
1463+ match self . resolve_name_in_module ( search_module, name, TypeNS , false , false , span) {
14601464 Failed ( _) => {
14611465 let segment_name = name. as_str ( ) ;
14621466 let module_name = module_to_string ( search_module) ;
@@ -1613,7 +1617,7 @@ impl<'a> Resolver<'a> {
16131617
16141618 if let ModuleRibKind ( module) = self . ribs [ ns] [ i] . kind {
16151619 let name = ident. name ;
1616- let item = self . resolve_name_in_module ( module, name, ns, true , record_used) ;
1620+ let item = self . resolve_name_in_module ( module, name, ns, true , false , record_used) ;
16171621 if let Success ( binding) = item {
16181622 // The ident resolves to an item.
16191623 return Some ( LexicalScopeBinding :: Item ( binding) ) ;
@@ -1622,7 +1626,7 @@ impl<'a> Resolver<'a> {
16221626 if let ModuleKind :: Block ( ..) = module. kind { // We can see through blocks
16231627 } else if !module. no_implicit_prelude {
16241628 return self . prelude . and_then ( |prelude| {
1625- self . resolve_name_in_module ( prelude, name, ns, false , None ) . success ( )
1629+ self . resolve_name_in_module ( prelude, name, ns, false , false , None ) . success ( )
16261630 } ) . map ( LexicalScopeBinding :: Item )
16271631 } else {
16281632 return None ;
@@ -1717,6 +1721,7 @@ impl<'a> Resolver<'a> {
17171721 self . ribs [ ValueNS ] . push ( Rib :: new ( ModuleRibKind ( module) ) ) ;
17181722 self . ribs [ TypeNS ] . push ( Rib :: new ( ModuleRibKind ( module) ) ) ;
17191723
1724+ self . finalize_current_module_macro_resolutions ( ) ;
17201725 f ( self ) ;
17211726
17221727 self . current_module = orig_module;
@@ -2221,6 +2226,7 @@ impl<'a> Resolver<'a> {
22212226 self . ribs [ ValueNS ] . push ( Rib :: new ( ModuleRibKind ( anonymous_module) ) ) ;
22222227 self . ribs [ TypeNS ] . push ( Rib :: new ( ModuleRibKind ( anonymous_module) ) ) ;
22232228 self . current_module = anonymous_module;
2229+ self . finalize_current_module_macro_resolutions ( ) ;
22242230 } else {
22252231 self . ribs [ ValueNS ] . push ( Rib :: new ( NormalRibKind ) ) ;
22262232 }
@@ -2754,23 +2760,19 @@ impl<'a> Resolver<'a> {
27542760 let module_path =
27552761 segments. split_last ( ) . unwrap ( ) . 1 . iter ( ) . map ( |ps| ps. identifier ) . collect :: < Vec < _ > > ( ) ;
27562762
2757- let containing_module;
2758- match self . resolve_module_path ( & module_path, UseLexicalScope , Some ( span) ) {
2763+ let module = match self . resolve_module_path ( & module_path, UseLexicalScope , Some ( span) ) {
27592764 Failed ( err) => {
27602765 if let Some ( ( span, msg) ) = err {
27612766 resolve_error ( self , span, ResolutionError :: FailedToResolve ( & msg) ) ;
27622767 }
27632768 return Err ( true ) ;
27642769 }
27652770 Indeterminate => return Err ( false ) ,
2766- Success ( resulting_module) => {
2767- containing_module = resulting_module;
2768- }
2769- }
2771+ Success ( module) => module,
2772+ } ;
27702773
27712774 let name = segments. last ( ) . unwrap ( ) . identifier . name ;
2772- let result =
2773- self . resolve_name_in_module ( containing_module, name, namespace, false , Some ( span) ) ;
2775+ let result = self . resolve_name_in_module ( module, name, namespace, false , false , Some ( span) ) ;
27742776 result. success ( ) . ok_or ( false )
27752777 }
27762778
@@ -2782,10 +2784,9 @@ impl<'a> Resolver<'a> {
27822784 where T : Named ,
27832785 {
27842786 let module_path = segments. split_last ( ) . unwrap ( ) . 1 . iter ( ) . map ( T :: ident) . collect :: < Vec < _ > > ( ) ;
2785- let root_module = self . graph_root ;
2787+ let root = self . graph_root ;
27862788
2787- let containing_module;
2788- match self . resolve_module_path_from_root ( root_module, & module_path, 0 , Some ( span) ) {
2789+ let module = match self . resolve_module_path_from_root ( root, & module_path, 0 , Some ( span) ) {
27892790 Failed ( err) => {
27902791 if let Some ( ( span, msg) ) = err {
27912792 resolve_error ( self , span, ResolutionError :: FailedToResolve ( & msg) ) ;
@@ -2795,14 +2796,11 @@ impl<'a> Resolver<'a> {
27952796
27962797 Indeterminate => return Err ( false ) ,
27972798
2798- Success ( resulting_module) => {
2799- containing_module = resulting_module;
2800- }
2801- }
2799+ Success ( module) => module,
2800+ } ;
28022801
28032802 let name = segments. last ( ) . unwrap ( ) . ident ( ) . name ;
2804- let result =
2805- self . resolve_name_in_module ( containing_module, name, namespace, false , Some ( span) ) ;
2803+ let result = self . resolve_name_in_module ( module, name, namespace, false , false , Some ( span) ) ;
28062804 result. success ( ) . ok_or ( false )
28072805 }
28082806
@@ -3383,14 +3381,18 @@ impl<'a> Resolver<'a> {
33833381 self . report_shadowing_errors ( ) ;
33843382 let mut reported_spans = FxHashSet ( ) ;
33853383
3386- for & AmbiguityError { span, name, b1, b2 } in & self . ambiguity_errors {
3384+ for & AmbiguityError { span, name, b1, b2, lexical } in & self . ambiguity_errors {
33873385 if !reported_spans. insert ( span) { continue }
33883386 let msg1 = format ! ( "`{}` could resolve to the name imported here" , name) ;
33893387 let msg2 = format ! ( "`{}` could also resolve to the name imported here" , name) ;
33903388 self . session . struct_span_err ( span, & format ! ( "`{}` is ambiguous" , name) )
33913389 . span_note ( b1. span , & msg1)
33923390 . span_note ( b2. span , & msg2)
3393- . note ( & format ! ( "Consider adding an explicit import of `{}` to disambiguate" , name) )
3391+ . note ( & if lexical || !b1. is_glob_import ( ) {
3392+ "macro-expanded macro imports do not shadow" . to_owned ( )
3393+ } else {
3394+ format ! ( "consider adding an explicit import of `{}` to disambiguate" , name)
3395+ } )
33943396 . emit ( ) ;
33953397 }
33963398
@@ -3413,12 +3415,12 @@ impl<'a> Resolver<'a> {
34133415
34143416 fn report_shadowing_errors ( & mut self ) {
34153417 for ( name, scope) in replace ( & mut self . lexical_macro_resolutions , Vec :: new ( ) ) {
3416- self . resolve_macro_name ( scope, name) ;
3418+ self . resolve_legacy_scope ( scope, name, true ) ;
34173419 }
34183420
34193421 let mut reported_errors = FxHashSet ( ) ;
34203422 for binding in replace ( & mut self . disallowed_shadowing , Vec :: new ( ) ) {
3421- if self . resolve_macro_name ( binding. parent , binding. name ) . is_some ( ) &&
3423+ if self . resolve_legacy_scope ( binding. parent , binding. name , false ) . is_some ( ) &&
34223424 reported_errors. insert ( ( binding. name , binding. span ) ) {
34233425 let msg = format ! ( "`{}` is already in scope" , binding. name) ;
34243426 self . session . struct_span_err ( binding. span , & msg)
0 commit comments