@@ -25,8 +25,8 @@ use crate::{
2525 derive_macro_as_call_id,
2626 item_scope:: { ImportType , PerNsGlobImports } ,
2727 item_tree:: {
28- self , FileItemTreeId , ItemTree , ItemTreeId , MacroCall , MacroRules , Mod , ModItem , ModKind ,
29- StructDefKind ,
28+ self , FileItemTreeId , ItemTree , ItemTreeId , MacroCall , MacroDef , MacroRules , Mod , ModItem ,
29+ ModKind , StructDefKind ,
3030 } ,
3131 macro_call_as_call_id,
3232 nameres:: {
@@ -395,7 +395,7 @@ impl DefCollector<'_> {
395395 /// macro_rules! foo { () => {} }
396396 /// use foo as bar;
397397 /// ```
398- fn define_macro (
398+ fn define_macro_rules (
399399 & mut self ,
400400 module_id : LocalModuleId ,
401401 name : Name ,
@@ -430,6 +430,21 @@ impl DefCollector<'_> {
430430 self . def_map . modules [ module_id] . scope . define_legacy_macro ( name, mac) ;
431431 }
432432
433+ /// Define a macro 2.0 macro
434+ ///
435+ /// The scoped of macro 2.0 macro is equal to normal function
436+ fn define_macro_def (
437+ & mut self ,
438+ module_id : LocalModuleId ,
439+ name : Name ,
440+ macro_ : MacroDefId ,
441+ vis : & RawVisibility ,
442+ ) {
443+ let vis =
444+ self . def_map . resolve_visibility ( self . db , module_id, vis) . unwrap_or ( Visibility :: Public ) ;
445+ self . update ( module_id, & [ ( Some ( name) , PerNs :: macros ( macro_, vis) ) ] , vis, ImportType :: Named ) ;
446+ }
447+
433448 /// Define a proc macro
434449 ///
435450 /// A proc macro is similar to normal macro scope, but it would not visible in legacy textual scoped.
@@ -1067,40 +1082,7 @@ impl ModCollector<'_, '_> {
10671082 }
10681083 ModItem :: MacroCall ( mac) => self . collect_macro_call ( & self . item_tree [ mac] ) ,
10691084 ModItem :: MacroRules ( id) => self . collect_macro_rules ( id) ,
1070- ModItem :: MacroDef ( id) => {
1071- let mac = & self . item_tree [ id] ;
1072- let ast_id = InFile :: new ( self . file_id , mac. ast_id . upcast ( ) ) ;
1073-
1074- // "Macro 2.0" is not currently supported by rust-analyzer, but libcore uses it
1075- // to define builtin macros, so we support at least that part.
1076- let attrs = self . item_tree . attrs (
1077- self . def_collector . db ,
1078- krate,
1079- ModItem :: from ( id) . into ( ) ,
1080- ) ;
1081- if attrs. by_key ( "rustc_builtin_macro" ) . exists ( ) {
1082- let krate = self . def_collector . def_map . krate ;
1083- let macro_id = find_builtin_macro ( & mac. name , krate, ast_id)
1084- . or_else ( || find_builtin_derive ( & mac. name , krate, ast_id) ) ;
1085- if let Some ( macro_id) = macro_id {
1086- let vis = self
1087- . def_collector
1088- . def_map
1089- . resolve_visibility (
1090- self . def_collector . db ,
1091- self . module_id ,
1092- & self . item_tree [ mac. visibility ] ,
1093- )
1094- . unwrap_or ( Visibility :: Public ) ;
1095- self . def_collector . update (
1096- self . module_id ,
1097- & [ ( Some ( mac. name . clone ( ) ) , PerNs :: macros ( macro_id, vis) ) ] ,
1098- vis,
1099- ImportType :: Named ,
1100- ) ;
1101- }
1102- }
1103- }
1085+ ModItem :: MacroDef ( id) => self . collect_macro_def ( id) ,
11041086 ModItem :: Impl ( imp) => {
11051087 let module = self . def_collector . def_map . module_id ( self . module_id ) ;
11061088 let impl_id =
@@ -1420,7 +1402,7 @@ impl ModCollector<'_, '_> {
14201402 if attrs. by_key ( "rustc_builtin_macro" ) . exists ( ) {
14211403 let krate = self . def_collector . def_map . krate ;
14221404 if let Some ( macro_id) = find_builtin_macro ( & mac. name , krate, ast_id) {
1423- self . def_collector . define_macro (
1405+ self . def_collector . define_macro_rules (
14241406 self . module_id ,
14251407 mac. name . clone ( ) ,
14261408 macro_id,
@@ -1436,7 +1418,49 @@ impl ModCollector<'_, '_> {
14361418 kind : MacroDefKind :: Declarative ( ast_id) ,
14371419 local_inner : is_local_inner,
14381420 } ;
1439- self . def_collector . define_macro ( self . module_id , mac. name . clone ( ) , macro_id, is_export) ;
1421+ self . def_collector . define_macro_rules (
1422+ self . module_id ,
1423+ mac. name . clone ( ) ,
1424+ macro_id,
1425+ is_export,
1426+ ) ;
1427+ }
1428+
1429+ fn collect_macro_def ( & mut self , id : FileItemTreeId < MacroDef > ) {
1430+ let krate = self . def_collector . def_map . krate ;
1431+ let mac = & self . item_tree [ id] ;
1432+ let ast_id = InFile :: new ( self . file_id , mac. ast_id . upcast ( ) ) ;
1433+
1434+ // Case 1: bulitin macros
1435+ let attrs = self . item_tree . attrs ( self . def_collector . db , krate, ModItem :: from ( id) . into ( ) ) ;
1436+ if attrs. by_key ( "rustc_builtin_macro" ) . exists ( ) {
1437+ let macro_id = find_builtin_macro ( & mac. name , krate, ast_id)
1438+ . or_else ( || find_builtin_derive ( & mac. name , krate, ast_id) ) ;
1439+
1440+ if let Some ( macro_id) = macro_id {
1441+ self . def_collector . define_macro_def (
1442+ self . module_id ,
1443+ mac. name . clone ( ) ,
1444+ macro_id,
1445+ & self . item_tree [ mac. visibility ] ,
1446+ ) ;
1447+ }
1448+ return ;
1449+ }
1450+
1451+ // Case 2: normal `macro`
1452+ let macro_id = MacroDefId {
1453+ krate : self . def_collector . def_map . krate ,
1454+ kind : MacroDefKind :: Declarative ( ast_id) ,
1455+ local_inner : false ,
1456+ } ;
1457+
1458+ self . def_collector . define_macro_def (
1459+ self . module_id ,
1460+ mac. name . clone ( ) ,
1461+ macro_id,
1462+ & self . item_tree [ mac. visibility ] ,
1463+ ) ;
14401464 }
14411465
14421466 fn collect_macro_call ( & mut self , mac : & MacroCall ) {
0 commit comments