@@ -77,7 +77,7 @@ use tt::TextRange;
7777
7878use crate :: {
7979 AstId , BlockId , BlockLoc , CrateRootModuleId , ExternCrateId , FunctionId , FxIndexMap ,
80- LocalModuleId , Lookup , MacroExpander , MacroId , ModuleId , ProcMacroId , UseId ,
80+ LocalModuleId , Lookup , MacroCallStyles , MacroExpander , MacroId , ModuleId , ProcMacroId , UseId ,
8181 db:: DefDatabase ,
8282 item_scope:: { BuiltinShadowMode , ItemScope } ,
8383 item_tree:: TreeId ,
@@ -813,26 +813,25 @@ pub enum MacroSubNs {
813813 Attr ,
814814}
815815
816- impl MacroSubNs {
817- pub ( crate ) fn from_id ( db : & dyn DefDatabase , macro_id : MacroId ) -> Self {
818- let expander = match macro_id {
819- MacroId :: Macro2Id ( it) => it. lookup ( db) . expander ,
820- MacroId :: MacroRulesId ( it) => it . lookup ( db ) . expander ,
821- MacroId :: ProcMacroId ( it ) => {
822- return match it . lookup ( db ) . kind {
823- ProcMacroKind :: CustomDerive | ProcMacroKind :: Attr => Self :: Attr ,
824- ProcMacroKind :: Bang => Self :: Bang ,
825- } ;
826- }
827- } ;
816+ pub ( crate ) fn macro_styles_from_id ( db : & dyn DefDatabase , macro_id : MacroId ) -> MacroCallStyles {
817+ let expander = match macro_id {
818+ MacroId :: Macro2Id ( it ) => it . lookup ( db ) . expander ,
819+ MacroId :: MacroRulesId ( it) => it. lookup ( db) . expander ,
820+ MacroId :: ProcMacroId ( it) => {
821+ return match it . lookup ( db ) . kind {
822+ ProcMacroKind :: CustomDerive => MacroCallStyles :: DERIVE ,
823+ ProcMacroKind :: Bang => MacroCallStyles :: FN_LIKE ,
824+ ProcMacroKind :: Attr => MacroCallStyles :: ATTR ,
825+ } ;
826+ }
827+ } ;
828828
829+ match expander {
830+ MacroExpander :: Declarative { styles } => styles,
829831 // Eager macros aren't *guaranteed* to be bang macros, but they *are* all bang macros currently.
830- match expander {
831- MacroExpander :: Declarative
832- | MacroExpander :: BuiltIn ( _)
833- | MacroExpander :: BuiltInEager ( _) => Self :: Bang ,
834- MacroExpander :: BuiltInAttr ( _) | MacroExpander :: BuiltInDerive ( _) => Self :: Attr ,
835- }
832+ MacroExpander :: BuiltIn ( _) | MacroExpander :: BuiltInEager ( _) => MacroCallStyles :: FN_LIKE ,
833+ MacroExpander :: BuiltInAttr ( _) => MacroCallStyles :: ATTR ,
834+ MacroExpander :: BuiltInDerive ( _) => MacroCallStyles :: DERIVE ,
836835 }
837836}
838837
@@ -842,9 +841,19 @@ impl MacroSubNs {
842841/// We ignore resolutions from one sub-namespace when searching names in scope for another.
843842///
844843/// [rustc]: https://github.com/rust-lang/rust/blob/1.69.0/compiler/rustc_resolve/src/macros.rs#L75
845- fn sub_namespace_match ( candidate : Option < MacroSubNs > , expected : Option < MacroSubNs > ) -> bool {
846- match ( candidate, expected) {
847- ( Some ( candidate) , Some ( expected) ) => candidate == expected,
848- _ => true ,
844+ fn sub_namespace_match (
845+ db : & dyn DefDatabase ,
846+ macro_id : MacroId ,
847+ expected : Option < MacroSubNs > ,
848+ ) -> bool {
849+ let candidate = macro_styles_from_id ( db, macro_id) ;
850+ match expected {
851+ Some ( MacroSubNs :: Bang ) => candidate. contains ( MacroCallStyles :: FN_LIKE ) ,
852+ Some ( MacroSubNs :: Attr ) => {
853+ candidate. contains ( MacroCallStyles :: ATTR ) || candidate. contains ( MacroCallStyles :: DERIVE )
854+ }
855+ // If we aren't expecting a specific sub-namespace
856+ // (e.g. in `use` declarations), match any macro.
857+ None => true ,
849858 }
850859}
0 commit comments