@@ -540,6 +540,7 @@ pub enum Namespace {
540
540
pub struct PerNS < T > {
541
541
value_ns : T ,
542
542
type_ns : T ,
543
+ macro_ns : Option < T > ,
543
544
}
544
545
545
546
impl < T > :: std:: ops:: Index < Namespace > for PerNS < T > {
@@ -548,7 +549,7 @@ impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
548
549
match ns {
549
550
ValueNS => & self . value_ns ,
550
551
TypeNS => & self . type_ns ,
551
- MacroNS => unreachable ! ( ) ,
552
+ MacroNS => self . macro_ns . as_ref ( ) . unwrap ( ) ,
552
553
}
553
554
}
554
555
}
@@ -558,7 +559,7 @@ impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
558
559
match ns {
559
560
ValueNS => & mut self . value_ns ,
560
561
TypeNS => & mut self . type_ns ,
561
- MacroNS => unreachable ! ( ) ,
562
+ MacroNS => self . macro_ns . as_mut ( ) . unwrap ( ) ,
562
563
}
563
564
}
564
565
}
@@ -675,22 +676,14 @@ impl<'a> Visitor for Resolver<'a> {
675
676
676
677
pub type ErrorMessage = Option < ( Span , String ) > ;
677
678
678
- #[ derive( Clone , PartialEq , Eq ) ]
679
+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
679
680
pub enum ResolveResult < T > {
680
681
Failed ( ErrorMessage ) , // Failed to resolve the name, optional helpful error message.
681
682
Indeterminate , // Couldn't determine due to unresolved globs.
682
683
Success ( T ) , // Successfully resolved the import.
683
684
}
684
685
685
686
impl < 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
-
694
687
fn success ( self ) -> Option < T > {
695
688
match self {
696
689
Success ( t) => Some ( t) ,
@@ -825,6 +818,7 @@ pub struct ModuleS<'a> {
825
818
normal_ancestor_id : Option < NodeId > ,
826
819
827
820
resolutions : RefCell < FxHashMap < ( Name , Namespace ) , & ' a RefCell < NameResolution < ' a > > > > ,
821
+ legacy_macro_resolutions : RefCell < Vec < ( Mark , Name , Span ) > > ,
828
822
829
823
// Macro invocations that can expand into items in this module.
830
824
unresolved_invocations : RefCell < FxHashSet < Mark > > ,
@@ -852,6 +846,7 @@ impl<'a> ModuleS<'a> {
852
846
kind : kind,
853
847
normal_ancestor_id : None ,
854
848
resolutions : RefCell :: new ( FxHashMap ( ) ) ,
849
+ legacy_macro_resolutions : RefCell :: new ( Vec :: new ( ) ) ,
855
850
unresolved_invocations : RefCell :: new ( FxHashSet ( ) ) ,
856
851
no_implicit_prelude : false ,
857
852
glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
@@ -943,6 +938,7 @@ struct PrivacyError<'a>(Span, Name, &'a NameBinding<'a>);
943
938
struct AmbiguityError < ' a > {
944
939
span : Span ,
945
940
name : Name ,
941
+ lexical : bool ,
946
942
b1 : & ' a NameBinding < ' a > ,
947
943
b2 : & ' a NameBinding < ' a > ,
948
944
}
@@ -1001,7 +997,7 @@ impl<'a> NameBinding<'a> {
1001
997
fn is_glob_import ( & self ) -> bool {
1002
998
match self . kind {
1003
999
NameBindingKind :: Import { directive, .. } => directive. is_glob ( ) ,
1004
- NameBindingKind :: Ambiguity { .. } => true ,
1000
+ NameBindingKind :: Ambiguity { b1 , .. } => b1 . is_glob_import ( ) ,
1005
1001
_ => false ,
1006
1002
}
1007
1003
}
@@ -1136,6 +1132,7 @@ pub struct Resolver<'a> {
1136
1132
arenas : & ' a ResolverArenas < ' a > ,
1137
1133
dummy_binding : & ' a NameBinding < ' a > ,
1138
1134
new_import_semantics : bool , // true if `#![feature(item_like_imports)]`
1135
+ use_extern_macros : bool , // true if `#![feature(use_extern_macros)]`
1139
1136
1140
1137
pub exported_macros : Vec < ast:: MacroDef > ,
1141
1138
crate_loader : & ' a mut CrateLoader ,
@@ -1300,6 +1297,7 @@ impl<'a> Resolver<'a> {
1300
1297
ribs : PerNS {
1301
1298
value_ns : vec ! [ Rib :: new( ModuleRibKind ( graph_root) ) ] ,
1302
1299
type_ns : vec ! [ Rib :: new( ModuleRibKind ( graph_root) ) ] ,
1300
+ macro_ns : None ,
1303
1301
} ,
1304
1302
label_ribs : Vec :: new ( ) ,
1305
1303
@@ -1336,6 +1334,7 @@ impl<'a> Resolver<'a> {
1336
1334
vis : ty:: Visibility :: Public ,
1337
1335
} ) ,
1338
1336
new_import_semantics : session. features . borrow ( ) . item_like_imports ,
1337
+ use_extern_macros : session. features . borrow ( ) . use_extern_macros ,
1339
1338
1340
1339
exported_macros : Vec :: new ( ) ,
1341
1340
crate_loader : crate_loader,
@@ -1365,6 +1364,10 @@ impl<'a> Resolver<'a> {
1365
1364
PerNS {
1366
1365
type_ns : f ( self , TypeNS ) ,
1367
1366
value_ns : f ( self , ValueNS ) ,
1367
+ macro_ns : match self . use_extern_macros {
1368
+ true => Some ( f ( self , MacroNS ) ) ,
1369
+ false => None ,
1370
+ } ,
1368
1371
}
1369
1372
}
1370
1373
@@ -1403,8 +1406,9 @@ impl<'a> Resolver<'a> {
1403
1406
}
1404
1407
NameBindingKind :: Import { .. } => false ,
1405
1408
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
+ } ) ;
1408
1412
true
1409
1413
}
1410
1414
_ => false
@@ -1438,7 +1442,7 @@ impl<'a> Resolver<'a> {
1438
1442
-> ResolveResult < Module < ' a > > {
1439
1443
fn search_parent_externals < ' a > ( this : & mut Resolver < ' a > , needle : Name , module : Module < ' a > )
1440
1444
-> 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 ) {
1442
1446
Success ( binding) if binding. is_extern_crate ( ) => Some ( module) ,
1443
1447
_ => if let ( & ModuleKind :: Def ( ..) , Some ( parent) ) = ( & module. kind , module. parent ) {
1444
1448
search_parent_externals ( this, needle, parent)
@@ -1456,7 +1460,7 @@ impl<'a> Resolver<'a> {
1456
1460
// modules as we go.
1457
1461
while index < module_path_len {
1458
1462
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) {
1460
1464
Failed ( _) => {
1461
1465
let segment_name = name. as_str ( ) ;
1462
1466
let module_name = module_to_string ( search_module) ;
@@ -1613,7 +1617,7 @@ impl<'a> Resolver<'a> {
1613
1617
1614
1618
if let ModuleRibKind ( module) = self . ribs [ ns] [ i] . kind {
1615
1619
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) ;
1617
1621
if let Success ( binding) = item {
1618
1622
// The ident resolves to an item.
1619
1623
return Some ( LexicalScopeBinding :: Item ( binding) ) ;
@@ -1622,7 +1626,7 @@ impl<'a> Resolver<'a> {
1622
1626
if let ModuleKind :: Block ( ..) = module. kind { // We can see through blocks
1623
1627
} else if !module. no_implicit_prelude {
1624
1628
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 ( )
1626
1630
} ) . map ( LexicalScopeBinding :: Item )
1627
1631
} else {
1628
1632
return None ;
@@ -1717,6 +1721,7 @@ impl<'a> Resolver<'a> {
1717
1721
self . ribs [ ValueNS ] . push ( Rib :: new ( ModuleRibKind ( module) ) ) ;
1718
1722
self . ribs [ TypeNS ] . push ( Rib :: new ( ModuleRibKind ( module) ) ) ;
1719
1723
1724
+ self . finalize_current_module_macro_resolutions ( ) ;
1720
1725
f ( self ) ;
1721
1726
1722
1727
self . current_module = orig_module;
@@ -2221,6 +2226,7 @@ impl<'a> Resolver<'a> {
2221
2226
self . ribs [ ValueNS ] . push ( Rib :: new ( ModuleRibKind ( anonymous_module) ) ) ;
2222
2227
self . ribs [ TypeNS ] . push ( Rib :: new ( ModuleRibKind ( anonymous_module) ) ) ;
2223
2228
self . current_module = anonymous_module;
2229
+ self . finalize_current_module_macro_resolutions ( ) ;
2224
2230
} else {
2225
2231
self . ribs [ ValueNS ] . push ( Rib :: new ( NormalRibKind ) ) ;
2226
2232
}
@@ -2754,23 +2760,19 @@ impl<'a> Resolver<'a> {
2754
2760
let module_path =
2755
2761
segments. split_last ( ) . unwrap ( ) . 1 . iter ( ) . map ( |ps| ps. identifier ) . collect :: < Vec < _ > > ( ) ;
2756
2762
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) ) {
2759
2764
Failed ( err) => {
2760
2765
if let Some ( ( span, msg) ) = err {
2761
2766
resolve_error ( self , span, ResolutionError :: FailedToResolve ( & msg) ) ;
2762
2767
}
2763
2768
return Err ( true ) ;
2764
2769
}
2765
2770
Indeterminate => return Err ( false ) ,
2766
- Success ( resulting_module) => {
2767
- containing_module = resulting_module;
2768
- }
2769
- }
2771
+ Success ( module) => module,
2772
+ } ;
2770
2773
2771
2774
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) ) ;
2774
2776
result. success ( ) . ok_or ( false )
2775
2777
}
2776
2778
@@ -2782,10 +2784,9 @@ impl<'a> Resolver<'a> {
2782
2784
where T : Named ,
2783
2785
{
2784
2786
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 ;
2786
2788
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) ) {
2789
2790
Failed ( err) => {
2790
2791
if let Some ( ( span, msg) ) = err {
2791
2792
resolve_error ( self , span, ResolutionError :: FailedToResolve ( & msg) ) ;
@@ -2795,14 +2796,11 @@ impl<'a> Resolver<'a> {
2795
2796
2796
2797
Indeterminate => return Err ( false ) ,
2797
2798
2798
- Success ( resulting_module) => {
2799
- containing_module = resulting_module;
2800
- }
2801
- }
2799
+ Success ( module) => module,
2800
+ } ;
2802
2801
2803
2802
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) ) ;
2806
2804
result. success ( ) . ok_or ( false )
2807
2805
}
2808
2806
@@ -3383,14 +3381,18 @@ impl<'a> Resolver<'a> {
3383
3381
self . report_shadowing_errors ( ) ;
3384
3382
let mut reported_spans = FxHashSet ( ) ;
3385
3383
3386
- for & AmbiguityError { span, name, b1, b2 } in & self . ambiguity_errors {
3384
+ for & AmbiguityError { span, name, b1, b2, lexical } in & self . ambiguity_errors {
3387
3385
if !reported_spans. insert ( span) { continue }
3388
3386
let msg1 = format ! ( "`{}` could resolve to the name imported here" , name) ;
3389
3387
let msg2 = format ! ( "`{}` could also resolve to the name imported here" , name) ;
3390
3388
self . session . struct_span_err ( span, & format ! ( "`{}` is ambiguous" , name) )
3391
3389
. span_note ( b1. span , & msg1)
3392
3390
. 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
+ } )
3394
3396
. emit ( ) ;
3395
3397
}
3396
3398
@@ -3413,12 +3415,12 @@ impl<'a> Resolver<'a> {
3413
3415
3414
3416
fn report_shadowing_errors ( & mut self ) {
3415
3417
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 ) ;
3417
3419
}
3418
3420
3419
3421
let mut reported_errors = FxHashSet ( ) ;
3420
3422
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 ( ) &&
3422
3424
reported_errors. insert ( ( binding. name , binding. span ) ) {
3423
3425
let msg = format ! ( "`{}` is already in scope" , binding. name) ;
3424
3426
self . session . struct_span_err ( binding. span , & msg)
0 commit comments