@@ -57,6 +57,7 @@ use syntax::ext::base::MultiItemModifier;
57
57
use syntax:: ext:: hygiene:: Mark ;
58
58
use syntax:: ast:: { self , FloatTy } ;
59
59
use syntax:: ast:: { CRATE_NODE_ID , Name , NodeId , IntTy , UintTy } ;
60
+ use syntax:: ext:: base:: SyntaxExtension ;
60
61
use syntax:: parse:: token:: { self , keywords} ;
61
62
use syntax:: util:: lev_distance:: find_best_match_for_name;
62
63
@@ -77,6 +78,7 @@ use std::mem::replace;
77
78
use std:: rc:: Rc ;
78
79
79
80
use resolve_imports:: { ImportDirective , NameResolution } ;
81
+ use macros:: { InvocationData , LegacyBinding , LegacyScope } ;
80
82
81
83
// NB: This module needs to be declared first so diagnostics are
82
84
// registered before they are used.
@@ -791,9 +793,6 @@ pub struct ModuleS<'a> {
791
793
// access the children must be preceded with a
792
794
// `populate_module_if_necessary` call.
793
795
populated : Cell < bool > ,
794
-
795
- macros : RefCell < FnvHashMap < Name , macros:: NameBinding > > ,
796
- macros_escape : bool ,
797
796
}
798
797
799
798
pub type Module < ' a > = & ' a ModuleS < ' a > ;
@@ -811,8 +810,6 @@ impl<'a> ModuleS<'a> {
811
810
globs : RefCell :: new ( ( Vec :: new ( ) ) ) ,
812
811
traits : RefCell :: new ( None ) ,
813
812
populated : Cell :: new ( true ) ,
814
- macros : RefCell :: new ( FnvHashMap ( ) ) ,
815
- macros_escape : false ,
816
813
}
817
814
}
818
815
@@ -1076,7 +1073,7 @@ pub struct Resolver<'a> {
1076
1073
1077
1074
privacy_errors : Vec < PrivacyError < ' a > > ,
1078
1075
ambiguity_errors : Vec < AmbiguityError < ' a > > ,
1079
- macro_shadowing_errors : FnvHashSet < Span > ,
1076
+ disallowed_shadowing : Vec < ( Name , Span , LegacyScope < ' a > ) > ,
1080
1077
1081
1078
arenas : & ' a ResolverArenas < ' a > ,
1082
1079
dummy_binding : & ' a NameBinding < ' a > ,
@@ -1086,9 +1083,10 @@ pub struct Resolver<'a> {
1086
1083
pub derive_modes : FnvHashMap < Name , Rc < MultiItemModifier > > ,
1087
1084
crate_loader : & ' a mut CrateLoader ,
1088
1085
macro_names : FnvHashSet < Name > ,
1086
+ builtin_macros : FnvHashMap < Name , Rc < SyntaxExtension > > ,
1089
1087
1090
1088
// Maps the `Mark` of an expansion to its containing module or block.
1091
- expansion_data : FnvHashMap < Mark , macros :: ExpansionData < ' a > > ,
1089
+ invocations : FnvHashMap < Mark , & ' a InvocationData < ' a > > ,
1092
1090
}
1093
1091
1094
1092
pub struct ResolverArenas < ' a > {
@@ -1097,6 +1095,8 @@ pub struct ResolverArenas<'a> {
1097
1095
name_bindings : arena:: TypedArena < NameBinding < ' a > > ,
1098
1096
import_directives : arena:: TypedArena < ImportDirective < ' a > > ,
1099
1097
name_resolutions : arena:: TypedArena < RefCell < NameResolution < ' a > > > ,
1098
+ invocation_data : arena:: TypedArena < InvocationData < ' a > > ,
1099
+ legacy_bindings : arena:: TypedArena < LegacyBinding < ' a > > ,
1100
1100
}
1101
1101
1102
1102
impl < ' a > ResolverArenas < ' a > {
@@ -1120,6 +1120,13 @@ impl<'a> ResolverArenas<'a> {
1120
1120
fn alloc_name_resolution ( & ' a self ) -> & ' a RefCell < NameResolution < ' a > > {
1121
1121
self . name_resolutions . alloc ( Default :: default ( ) )
1122
1122
}
1123
+ fn alloc_invocation_data ( & ' a self , expansion_data : InvocationData < ' a > )
1124
+ -> & ' a InvocationData < ' a > {
1125
+ self . invocation_data . alloc ( expansion_data)
1126
+ }
1127
+ fn alloc_legacy_binding ( & ' a self , binding : LegacyBinding < ' a > ) -> & ' a LegacyBinding < ' a > {
1128
+ self . legacy_bindings . alloc ( binding)
1129
+ }
1123
1130
}
1124
1131
1125
1132
impl < ' a > ty:: NodeIdTree for Resolver < ' a > {
@@ -1205,8 +1212,9 @@ impl<'a> Resolver<'a> {
1205
1212
let mut definitions = Definitions :: new ( ) ;
1206
1213
DefCollector :: new ( & mut definitions) . collect_root ( ) ;
1207
1214
1208
- let mut expansion_data = FnvHashMap ( ) ;
1209
- expansion_data. insert ( Mark :: root ( ) , macros:: ExpansionData :: root ( graph_root) ) ;
1215
+ let mut invocations = FnvHashMap ( ) ;
1216
+ invocations. insert ( Mark :: root ( ) ,
1217
+ arenas. alloc_invocation_data ( InvocationData :: root ( graph_root) ) ) ;
1210
1218
1211
1219
Resolver {
1212
1220
session : session,
@@ -1252,7 +1260,7 @@ impl<'a> Resolver<'a> {
1252
1260
1253
1261
privacy_errors : Vec :: new ( ) ,
1254
1262
ambiguity_errors : Vec :: new ( ) ,
1255
- macro_shadowing_errors : FnvHashSet ( ) ,
1263
+ disallowed_shadowing : Vec :: new ( ) ,
1256
1264
1257
1265
arenas : arenas,
1258
1266
dummy_binding : arenas. alloc_name_binding ( NameBinding {
@@ -1266,7 +1274,8 @@ impl<'a> Resolver<'a> {
1266
1274
derive_modes : FnvHashMap ( ) ,
1267
1275
crate_loader : crate_loader,
1268
1276
macro_names : FnvHashSet ( ) ,
1269
- expansion_data : expansion_data,
1277
+ builtin_macros : FnvHashMap ( ) ,
1278
+ invocations : invocations,
1270
1279
}
1271
1280
}
1272
1281
@@ -1277,6 +1286,8 @@ impl<'a> Resolver<'a> {
1277
1286
name_bindings : arena:: TypedArena :: new ( ) ,
1278
1287
import_directives : arena:: TypedArena :: new ( ) ,
1279
1288
name_resolutions : arena:: TypedArena :: new ( ) ,
1289
+ invocation_data : arena:: TypedArena :: new ( ) ,
1290
+ legacy_bindings : arena:: TypedArena :: new ( ) ,
1280
1291
}
1281
1292
}
1282
1293
@@ -3342,7 +3353,8 @@ impl<'a> Resolver<'a> {
3342
3353
vis. is_accessible_from ( module. normal_ancestor_id . unwrap ( ) , self )
3343
3354
}
3344
3355
3345
- fn report_errors ( & self ) {
3356
+ fn report_errors ( & mut self ) {
3357
+ self . report_shadowing_errors ( ) ;
3346
3358
let mut reported_spans = FnvHashSet ( ) ;
3347
3359
3348
3360
for & AmbiguityError { span, name, b1, b2 } in & self . ambiguity_errors {
@@ -3370,6 +3382,20 @@ impl<'a> Resolver<'a> {
3370
3382
}
3371
3383
}
3372
3384
3385
+ fn report_shadowing_errors ( & mut self ) {
3386
+ let mut reported_errors = FnvHashSet ( ) ;
3387
+ for ( name, span, scope) in replace ( & mut self . disallowed_shadowing , Vec :: new ( ) ) {
3388
+ if self . resolve_macro_name ( scope, name, false ) . is_some ( ) &&
3389
+ reported_errors. insert ( ( name, span) ) {
3390
+ let msg = format ! ( "`{}` is already in scope" , name) ;
3391
+ self . session . struct_span_err ( span, & msg)
3392
+ . note ( "macro-expanded `macro_rules!`s and `#[macro_use]`s \
3393
+ may not shadow existing macros (see RFC 1560)")
3394
+ . emit ( ) ;
3395
+ }
3396
+ }
3397
+ }
3398
+
3373
3399
fn report_conflict ( & self ,
3374
3400
parent : Module ,
3375
3401
name : Name ,
0 commit comments