@@ -17,6 +17,7 @@ use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, PResult};
17
17
use rustc_feature:: Features ;
18
18
use rustc_hir as hir;
19
19
use rustc_hir:: attrs:: { AttributeKind , CfgEntry , Deprecation } ;
20
+ use rustc_hir:: def:: MacroKinds ;
20
21
use rustc_hir:: { Stability , find_attr} ;
21
22
use rustc_lint_defs:: { BufferedEarlyLint , RegisteredTools } ;
22
23
use rustc_parse:: MACRO_ARGUMENTS ;
@@ -718,6 +719,9 @@ impl MacResult for DummyResult {
718
719
/// A syntax extension kind.
719
720
#[ derive( Clone ) ]
720
721
pub enum SyntaxExtensionKind {
722
+ /// A `macro_rules!` macro that can work as any `MacroKind`
723
+ MacroRules ( Arc < crate :: MacroRulesMacroExpander > ) ,
724
+
721
725
/// A token-based function-like macro.
722
726
Bang (
723
727
/// An expander with signature TokenStream -> TokenStream.
@@ -772,9 +776,39 @@ pub enum SyntaxExtensionKind {
772
776
) ,
773
777
774
778
/// A glob delegation.
779
+ ///
780
+ /// This is for delegated function implementations, and has nothing to do with glob imports.
775
781
GlobDelegation ( Arc < dyn GlobDelegationExpander + sync:: DynSync + sync:: DynSend > ) ,
776
782
}
777
783
784
+ impl SyntaxExtensionKind {
785
+ /// Returns `Some(expander)` for a macro usable as a `LegacyBang`; otherwise returns `None`
786
+ ///
787
+ /// This includes a `MacroRules` with function-like rules.
788
+ pub fn as_legacy_bang ( & self ) -> Option < & ( dyn TTMacroExpander + sync:: DynSync + sync:: DynSend ) > {
789
+ match self {
790
+ SyntaxExtensionKind :: LegacyBang ( exp) => Some ( exp. as_ref ( ) ) ,
791
+ SyntaxExtensionKind :: MacroRules ( exp) if exp. kinds ( ) . contains ( MacroKinds :: BANG ) => {
792
+ Some ( exp. as_ref ( ) )
793
+ }
794
+ _ => None ,
795
+ }
796
+ }
797
+
798
+ /// Returns `Some(expander)` for a macro usable as an `Attr`; otherwise returns `None`
799
+ ///
800
+ /// This includes a `MacroRules` with `attr` rules.
801
+ pub fn as_attr ( & self ) -> Option < & ( dyn AttrProcMacro + sync:: DynSync + sync:: DynSend ) > {
802
+ match self {
803
+ SyntaxExtensionKind :: Attr ( exp) => Some ( exp. as_ref ( ) ) ,
804
+ SyntaxExtensionKind :: MacroRules ( exp) if exp. kinds ( ) . contains ( MacroKinds :: ATTR ) => {
805
+ Some ( exp. as_ref ( ) )
806
+ }
807
+ _ => None ,
808
+ }
809
+ }
810
+ }
811
+
778
812
/// A struct representing a macro definition in "lowered" form ready for expansion.
779
813
pub struct SyntaxExtension {
780
814
/// A syntax extension kind.
@@ -804,18 +838,19 @@ pub struct SyntaxExtension {
804
838
}
805
839
806
840
impl SyntaxExtension {
807
- /// Returns which kind of macro calls this syntax extension.
808
- pub fn macro_kind ( & self ) -> MacroKind {
841
+ /// Returns which kinds of macro call this syntax extension.
842
+ pub fn macro_kinds ( & self ) -> MacroKinds {
809
843
match self . kind {
810
844
SyntaxExtensionKind :: Bang ( ..)
811
845
| SyntaxExtensionKind :: LegacyBang ( ..)
812
- | SyntaxExtensionKind :: GlobDelegation ( ..) => MacroKind :: Bang ,
846
+ | SyntaxExtensionKind :: GlobDelegation ( ..) => MacroKinds :: BANG ,
813
847
SyntaxExtensionKind :: Attr ( ..)
814
848
| SyntaxExtensionKind :: LegacyAttr ( ..)
815
- | SyntaxExtensionKind :: NonMacroAttr => MacroKind :: Attr ,
849
+ | SyntaxExtensionKind :: NonMacroAttr => MacroKinds :: ATTR ,
816
850
SyntaxExtensionKind :: Derive ( ..) | SyntaxExtensionKind :: LegacyDerive ( ..) => {
817
- MacroKind :: Derive
851
+ MacroKinds :: DERIVE
818
852
}
853
+ SyntaxExtensionKind :: MacroRules ( ref m) => m. kinds ( ) ,
819
854
}
820
855
}
821
856
@@ -1024,11 +1059,12 @@ impl SyntaxExtension {
1024
1059
parent : LocalExpnId ,
1025
1060
call_site : Span ,
1026
1061
descr : Symbol ,
1062
+ kind : MacroKind ,
1027
1063
macro_def_id : Option < DefId > ,
1028
1064
parent_module : Option < DefId > ,
1029
1065
) -> ExpnData {
1030
1066
ExpnData :: new (
1031
- ExpnKind :: Macro ( self . macro_kind ( ) , descr) ,
1067
+ ExpnKind :: Macro ( kind , descr) ,
1032
1068
parent. to_expn_id ( ) ,
1033
1069
call_site,
1034
1070
self . span ,
0 commit comments