@@ -109,6 +109,13 @@ pub struct ProcMacError {
109
109
warn_msg : & ' static str ,
110
110
}
111
111
112
+ // For compatibility bang macros are skipped when resolving potentially built-in attributes.
113
+ fn macro_kind_mismatch ( name : Name , requirement : Option < MacroKind > , candidate : Option < MacroKind > )
114
+ -> bool {
115
+ requirement == Some ( MacroKind :: Attr ) && candidate == Some ( MacroKind :: Bang ) &&
116
+ ( name == "test" || name == "bench" || is_builtin_attr_name ( name) )
117
+ }
118
+
112
119
impl < ' a , ' crateloader : ' a > base:: Resolver for Resolver < ' a , ' crateloader > {
113
120
fn next_node_id ( & mut self ) -> ast:: NodeId {
114
121
self . session . next_node_id ( )
@@ -479,13 +486,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
479
486
}
480
487
481
488
let legacy_resolution = self . resolve_legacy_scope (
482
- path[ 0 ] , parent_expansion, parent_legacy_scope, false , kind == MacroKind :: Attr
489
+ path[ 0 ] , Some ( kind ) , parent_expansion, parent_legacy_scope, false
483
490
) ;
484
491
let result = if let Some ( legacy_binding) = legacy_resolution {
485
492
Ok ( legacy_binding. def ( ) )
486
493
} else {
487
- match self . resolve_lexical_macro_path_segment ( path[ 0 ] , MacroNS , parent_expansion , false ,
488
- force , kind == MacroKind :: Attr , span) {
494
+ match self . resolve_lexical_macro_path_segment ( path[ 0 ] , MacroNS , Some ( kind ) ,
495
+ parent_expansion , false , force , span) {
489
496
Ok ( ( binding, _) ) => Ok ( binding. def_ignoring_ambiguity ( ) ) ,
490
497
Err ( Determinacy :: Undetermined ) => return Err ( Determinacy :: Undetermined ) ,
491
498
Err ( Determinacy :: Determined ) => {
@@ -543,10 +550,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
543
550
& mut self ,
544
551
mut ident : Ident ,
545
552
ns : Namespace ,
553
+ kind : Option < MacroKind > ,
546
554
parent_expansion : Mark ,
547
555
record_used : bool ,
548
556
force : bool ,
549
- is_attr : bool ,
550
557
path_span : Span ,
551
558
) -> Result < ( & ' a NameBinding < ' a > , FromPrelude ) , Determinacy > {
552
559
// General principles:
@@ -622,19 +629,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
622
629
}
623
630
WhereToResolve :: MacroUsePrelude => {
624
631
match self . macro_use_prelude . get ( & ident. name ) . cloned ( ) {
625
- Some ( binding) => {
626
- let mut result = Ok ( ( binding, FromPrelude ( true ) ) ) ;
627
- // FIXME: Keep some built-in macros working even if they are
628
- // shadowed by non-attribute macros imported with `macro_use`.
629
- // We need to come up with some more principled approach instead.
630
- if is_attr && ( ident. name == "test" || ident. name == "bench" ) {
631
- if let Def :: Macro ( _, MacroKind :: Bang ) =
632
- binding. def_ignoring_ambiguity ( ) {
633
- result = Err ( Determinacy :: Determined ) ;
634
- }
635
- }
636
- result
637
- }
632
+ Some ( binding) => Ok ( ( binding, FromPrelude ( true ) ) ) ,
638
633
None => Err ( Determinacy :: Determined ) ,
639
634
}
640
635
}
@@ -648,7 +643,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
648
643
// FIXME: Only built-in attributes are not considered as candidates for
649
644
// non-attributes to fight off regressions on stable channel (#53205).
650
645
// We need to come up with some more principled approach instead.
651
- if is_attr && is_builtin_attr_name ( ident. name ) {
646
+ if kind == Some ( MacroKind :: Attr ) && is_builtin_attr_name ( ident. name ) {
652
647
let binding = ( Def :: NonMacroAttr ( NonMacroAttrKind :: Builtin ) ,
653
648
ty:: Visibility :: Public , ident. span , Mark :: root ( ) )
654
649
. to_name_binding ( self . arenas ) ;
@@ -748,6 +743,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
748
743
749
744
match result {
750
745
Ok ( result) => {
746
+ if macro_kind_mismatch ( ident. name , kind, result. 0 . macro_kind ( ) ) {
747
+ continue_search ! ( ) ;
748
+ }
749
+
751
750
if !record_used {
752
751
return Ok ( result) ;
753
752
}
@@ -784,9 +783,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
784
783
}
785
784
786
785
let determinacy = Determinacy :: determined ( force) ;
787
- if determinacy == Determinacy :: Determined && is_attr {
786
+ if determinacy == Determinacy :: Determined && kind == Some ( MacroKind :: Attr ) {
788
787
// For single-segment attributes interpret determinate "no resolution" as a custom
789
- // attribute. (Lexical resolution implies the first segment and is_attr should imply
788
+ // attribute. (Lexical resolution implies the first segment and attr kind should imply
790
789
// the last segment, so we are certainly working with a single-segment attribute here.)
791
790
assert ! ( ns == MacroNS ) ;
792
791
let binding = ( Def :: NonMacroAttr ( NonMacroAttrKind :: Custom ) ,
@@ -800,15 +799,12 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
800
799
801
800
fn resolve_legacy_scope ( & mut self ,
802
801
ident : Ident ,
802
+ kind : Option < MacroKind > ,
803
803
parent_expansion : Mark ,
804
804
parent_legacy_scope : LegacyScope < ' a > ,
805
- record_used : bool ,
806
- is_attr : bool )
805
+ record_used : bool )
807
806
-> Option < & ' a NameBinding < ' a > > {
808
- if is_attr && ( ident. name == "test" || ident. name == "bench" ) {
809
- // FIXME: Keep some built-in macros working even if they are
810
- // shadowed by user-defined `macro_rules`.
811
- // We need to come up with some more principled approach instead.
807
+ if macro_kind_mismatch ( ident. name , kind, Some ( MacroKind :: Bang ) ) {
812
808
return None ;
813
809
}
814
810
@@ -897,10 +893,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
897
893
in module. legacy_macro_resolutions . borrow ( ) . iter ( ) {
898
894
let span = ident. span ;
899
895
let legacy_resolution = self . resolve_legacy_scope (
900
- ident, parent_expansion, parent_legacy_scope, true , kind == MacroKind :: Attr
896
+ ident, Some ( kind ) , parent_expansion, parent_legacy_scope, true
901
897
) ;
902
898
let resolution = self . resolve_lexical_macro_path_segment (
903
- ident, MacroNS , parent_expansion , true , true , kind == MacroKind :: Attr , span
899
+ ident, MacroNS , Some ( kind ) , parent_expansion , true , true , span
904
900
) ;
905
901
906
902
let check_consistency = |this : & Self , new_def : Def | {
@@ -960,10 +956,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
960
956
for & ( ident, parent_expansion, parent_legacy_scope)
961
957
in module. builtin_attrs . borrow ( ) . iter ( ) {
962
958
let resolve_legacy = |this : & mut Self | this. resolve_legacy_scope (
963
- ident, parent_expansion , parent_legacy_scope , true , true
959
+ ident, Some ( MacroKind :: Attr ) , parent_expansion , parent_legacy_scope , true
964
960
) ;
965
961
let resolve_modern = |this : & mut Self | this. resolve_lexical_macro_path_segment (
966
- ident, MacroNS , parent_expansion , true , true , true , ident. span
962
+ ident, MacroNS , Some ( MacroKind :: Attr ) , parent_expansion , true , true , ident. span
967
963
) . map ( |( binding, _) | binding) . ok ( ) ;
968
964
969
965
if let Some ( binding) = resolve_legacy ( self ) . or_else ( || resolve_modern ( self ) ) {
0 commit comments