@@ -17,6 +17,7 @@ use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, PResult};
1717use rustc_feature:: Features ;
1818use rustc_hir as hir;
1919use rustc_hir:: attrs:: { AttributeKind , CfgEntry , Deprecation } ;
20+ use rustc_hir:: def:: MacroKinds ;
2021use rustc_hir:: { Stability , find_attr} ;
2122use rustc_lint_defs:: { BufferedEarlyLint , RegisteredTools } ;
2223use rustc_parse:: MACRO_ARGUMENTS ;
@@ -718,6 +719,9 @@ impl MacResult for DummyResult {
718719/// A syntax extension kind.
719720#[ derive( Clone ) ]
720721pub enum SyntaxExtensionKind {
722+ /// A `macro_rules!` macro that can work as any `MacroKind`
723+ MacroRules ( Arc < crate :: MacroRulesMacroExpander > ) ,
724+
721725 /// A token-based function-like macro.
722726 Bang (
723727 /// An expander with signature TokenStream -> TokenStream.
@@ -772,9 +776,39 @@ pub enum SyntaxExtensionKind {
772776 ) ,
773777
774778 /// A glob delegation.
779+ ///
780+ /// This is for delegated function implementations, and has nothing to do with glob imports.
775781 GlobDelegation ( Arc < dyn GlobDelegationExpander + sync:: DynSync + sync:: DynSend > ) ,
776782}
777783
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+
778812/// A struct representing a macro definition in "lowered" form ready for expansion.
779813pub struct SyntaxExtension {
780814 /// A syntax extension kind.
@@ -804,18 +838,19 @@ pub struct SyntaxExtension {
804838}
805839
806840impl 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 {
809843 match self . kind {
810844 SyntaxExtensionKind :: Bang ( ..)
811845 | SyntaxExtensionKind :: LegacyBang ( ..)
812- | SyntaxExtensionKind :: GlobDelegation ( ..) => MacroKind :: Bang ,
846+ | SyntaxExtensionKind :: GlobDelegation ( ..) => MacroKinds :: BANG ,
813847 SyntaxExtensionKind :: Attr ( ..)
814848 | SyntaxExtensionKind :: LegacyAttr ( ..)
815- | SyntaxExtensionKind :: NonMacroAttr => MacroKind :: Attr ,
849+ | SyntaxExtensionKind :: NonMacroAttr => MacroKinds :: ATTR ,
816850 SyntaxExtensionKind :: Derive ( ..) | SyntaxExtensionKind :: LegacyDerive ( ..) => {
817- MacroKind :: Derive
851+ MacroKinds :: DERIVE
818852 }
853+ SyntaxExtensionKind :: MacroRules ( ref m) => m. kinds ( ) ,
819854 }
820855 }
821856
@@ -1024,11 +1059,12 @@ impl SyntaxExtension {
10241059 parent : LocalExpnId ,
10251060 call_site : Span ,
10261061 descr : Symbol ,
1062+ kind : MacroKind ,
10271063 macro_def_id : Option < DefId > ,
10281064 parent_module : Option < DefId > ,
10291065 ) -> ExpnData {
10301066 ExpnData :: new (
1031- ExpnKind :: Macro ( self . macro_kind ( ) , descr) ,
1067+ ExpnKind :: Macro ( kind , descr) ,
10321068 parent. to_expn_id ( ) ,
10331069 call_site,
10341070 self . span ,
0 commit comments