@@ -214,24 +214,10 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
214
214
vis : ty:: Visibility :: Invisible ,
215
215
expansion : Mark :: root ( ) ,
216
216
} ) ;
217
- self . macro_prelude . insert ( ident. name , binding) ;
218
- }
219
-
220
- fn add_unshadowable_attr ( & mut self , ident : ast:: Ident , ext : Lrc < SyntaxExtension > ) {
221
- let def_id = DefId {
222
- krate : BUILTIN_MACROS_CRATE ,
223
- index : DefIndex :: from_array_index ( self . macro_map . len ( ) ,
224
- DefIndexAddressSpace :: Low ) ,
225
- } ;
226
- let kind = ext. kind ( ) ;
227
- self . macro_map . insert ( def_id, ext) ;
228
- let binding = self . arenas . alloc_name_binding ( NameBinding {
229
- kind : NameBindingKind :: Def ( Def :: Macro ( def_id, kind) , false ) ,
230
- span : DUMMY_SP ,
231
- vis : ty:: Visibility :: Invisible ,
232
- expansion : Mark :: root ( ) ,
233
- } ) ;
234
- self . unshadowable_attrs . insert ( ident. name , binding) ;
217
+ if self . builtin_macros . insert ( ident. name , binding) . is_some ( ) {
218
+ self . session . span_err ( ident. span ,
219
+ & format ! ( "built-in macro `{}` was already defined" , ident) ) ;
220
+ }
235
221
}
236
222
237
223
fn resolve_imports ( & mut self ) {
@@ -249,7 +235,7 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
249
235
attr:: mark_known ( & attrs[ i] ) ;
250
236
}
251
237
252
- match self . macro_prelude . get ( & name) . cloned ( ) {
238
+ match self . builtin_macros . get ( & name) . cloned ( ) {
253
239
Some ( binding) => match * binding. get_macro ( self ) {
254
240
MultiModifier ( ..) | MultiDecorator ( ..) | SyntaxExtension :: AttrProcMacro ( ..) => {
255
241
return Some ( attrs. remove ( i) )
@@ -285,7 +271,7 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
285
271
}
286
272
let trait_name = traits[ j] . segments [ 0 ] . ident . name ;
287
273
let legacy_name = Symbol :: intern ( & format ! ( "derive_{}" , trait_name) ) ;
288
- if !self . macro_prelude . contains_key ( & legacy_name) {
274
+ if !self . builtin_macros . contains_key ( & legacy_name) {
289
275
continue
290
276
}
291
277
let span = traits. remove ( j) . span ;
@@ -490,14 +476,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
490
476
return def;
491
477
}
492
478
493
- if kind == MacroKind :: Attr {
494
- if let Some ( ext) = self . unshadowable_attrs . get ( & path[ 0 ] . name ) {
495
- return Ok ( ext. def ( ) ) ;
496
- }
497
- }
498
-
499
479
let legacy_resolution = self . resolve_legacy_scope (
500
- path[ 0 ] , invoc_id, invocation. parent_legacy_scope . get ( ) , false
480
+ path[ 0 ] , invoc_id, invocation. parent_legacy_scope . get ( ) , false , kind == MacroKind :: Attr
501
481
) ;
502
482
let result = if let Some ( legacy_binding) = legacy_resolution {
503
483
Ok ( legacy_binding. def ( ) )
@@ -585,14 +565,12 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
585
565
// (Macro NS)
586
566
// 1. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
587
567
// (open, not controlled).
588
- // 2. Macro prelude (language, standard library, user-defined legacy plugins lumped into
589
- // one set) (open, the open part is from macro expansions, not controlled).
568
+ // 2. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
590
569
// 2a. User-defined prelude from macro-use
591
570
// (open, the open part is from macro expansions, not controlled).
592
- // 2b. Standard library prelude, currently just a macro-use (closed, controlled)
593
- // 2c. Language prelude, perhaps including builtin attributes
594
- // (closed, controlled, except for legacy plugins).
595
- // 3. Builtin attributes (closed, controlled).
571
+ // 2b. Standard library prelude is currently implemented as `macro-use` (closed, controlled)
572
+ // 3. Language prelude: builtin macros (closed, controlled, except for legacy plugins).
573
+ // 4. Language prelude: builtin attributes (closed, controlled).
596
574
597
575
assert ! ( ns == TypeNS || ns == MacroNS ) ;
598
576
assert ! ( force || !record_used) ; // `record_used` implies `force`
@@ -613,12 +591,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
613
591
614
592
enum WhereToResolve < ' a > {
615
593
Module ( Module < ' a > ) ,
616
- MacroPrelude ,
594
+ MacroUsePrelude ,
595
+ BuiltinMacros ,
617
596
BuiltinAttrs ,
618
597
ExternPrelude ,
619
598
ToolPrelude ,
620
599
StdLibPrelude ,
621
- PrimitiveTypes ,
600
+ BuiltinTypes ,
622
601
}
623
602
624
603
// Go through all the scopes and try to resolve the name.
@@ -639,8 +618,26 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
639
618
self . current_module = orig_current_module;
640
619
binding. map ( |binding| ( binding, FromPrelude ( false ) ) )
641
620
}
642
- WhereToResolve :: MacroPrelude => {
643
- match self . macro_prelude . get ( & ident. name ) . cloned ( ) {
621
+ WhereToResolve :: MacroUsePrelude => {
622
+ match self . macro_use_prelude . get ( & ident. name ) . cloned ( ) {
623
+ Some ( binding) => {
624
+ let mut result = Ok ( ( binding, FromPrelude ( true ) ) ) ;
625
+ // FIXME: Keep some built-in macros working even if they are
626
+ // shadowed by non-attribute macros imported with `macro_use`.
627
+ // We need to come up with some more principled approach instead.
628
+ if is_attr && ( ident. name == "test" || ident. name == "bench" ) {
629
+ if let Def :: Macro ( _, MacroKind :: Bang ) =
630
+ binding. def_ignoring_ambiguity ( ) {
631
+ result = Err ( Determinacy :: Determined ) ;
632
+ }
633
+ }
634
+ result
635
+ }
636
+ None => Err ( Determinacy :: Determined ) ,
637
+ }
638
+ }
639
+ WhereToResolve :: BuiltinMacros => {
640
+ match self . builtin_macros . get ( & ident. name ) . cloned ( ) {
644
641
Some ( binding) => Ok ( ( binding, FromPrelude ( true ) ) ) ,
645
642
None => Err ( Determinacy :: Determined ) ,
646
643
}
@@ -708,7 +705,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
708
705
}
709
706
result
710
707
}
711
- WhereToResolve :: PrimitiveTypes => {
708
+ WhereToResolve :: BuiltinTypes => {
712
709
if let Some ( prim_ty) =
713
710
self . primitive_type_table . primitive_types . get ( & ident. name ) . cloned ( ) {
714
711
let binding = ( Def :: PrimTy ( prim_ty) , ty:: Visibility :: Public ,
@@ -728,19 +725,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
728
725
None => {
729
726
use_prelude = !module. no_implicit_prelude;
730
727
if ns == MacroNS {
731
- WhereToResolve :: MacroPrelude
728
+ WhereToResolve :: MacroUsePrelude
732
729
} else {
733
730
WhereToResolve :: ExternPrelude
734
731
}
735
732
}
736
733
}
737
734
}
738
- WhereToResolve :: MacroPrelude => WhereToResolve :: BuiltinAttrs ,
735
+ WhereToResolve :: MacroUsePrelude => WhereToResolve :: BuiltinMacros ,
736
+ WhereToResolve :: BuiltinMacros => WhereToResolve :: BuiltinAttrs ,
739
737
WhereToResolve :: BuiltinAttrs => break , // nowhere else to search
740
738
WhereToResolve :: ExternPrelude => WhereToResolve :: ToolPrelude ,
741
739
WhereToResolve :: ToolPrelude => WhereToResolve :: StdLibPrelude ,
742
- WhereToResolve :: StdLibPrelude => WhereToResolve :: PrimitiveTypes ,
743
- WhereToResolve :: PrimitiveTypes => break , // nowhere else to search
740
+ WhereToResolve :: StdLibPrelude => WhereToResolve :: BuiltinTypes ,
741
+ WhereToResolve :: BuiltinTypes => break , // nowhere else to search
744
742
} ;
745
743
746
744
continue ;
@@ -802,8 +800,16 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
802
800
ident : Ident ,
803
801
invoc_id : Mark ,
804
802
invoc_parent_legacy_scope : LegacyScope < ' a > ,
805
- record_used : bool )
803
+ record_used : bool ,
804
+ is_attr : bool )
806
805
-> Option < & ' a NameBinding < ' a > > {
806
+ if is_attr && ( ident. name == "test" || ident. name == "bench" ) {
807
+ // FIXME: Keep some built-in macros working even if they are
808
+ // shadowed by user-defined `macro_rules`.
809
+ // We need to come up with some more principled approach instead.
810
+ return None ;
811
+ }
812
+
807
813
let ident = ident. modern ( ) ;
808
814
809
815
// This is *the* result, resolution from the scope closest to the resolved identifier.
@@ -889,7 +895,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
889
895
let span = ident. span ;
890
896
let invocation = self . invocations [ & invoc_id] ;
891
897
let legacy_resolution = self . resolve_legacy_scope (
892
- ident, invoc_id, invocation. parent_legacy_scope . get ( ) , true
898
+ ident, invoc_id, invocation. parent_legacy_scope . get ( ) , true , kind == MacroKind :: Attr
893
899
) ;
894
900
let resolution = self . resolve_lexical_macro_path_segment (
895
901
ident, MacroNS , invoc_id, true , true , kind == MacroKind :: Attr , span
@@ -958,14 +964,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
958
964
None
959
965
// Then check global macros.
960
966
} . or_else ( || {
961
- // FIXME: get_macro needs an &mut Resolver, can we do it without cloning?
962
- let macro_prelude = self . macro_prelude . clone ( ) ;
963
- let names = macro_prelude. iter ( ) . filter_map ( |( name, binding) | {
964
- if binding. get_macro ( self ) . kind ( ) == kind {
965
- Some ( name)
966
- } else {
967
- None
968
- }
967
+ let names = self . builtin_macros . iter ( ) . chain ( self . macro_use_prelude . iter ( ) )
968
+ . filter_map ( |( name, binding) | {
969
+ if binding. macro_kind ( ) == Some ( kind) { Some ( name) } else { None }
969
970
} ) ;
970
971
find_best_match_for_name ( names, name, None )
971
972
// Then check modules.
0 commit comments