aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2021-03-27 05:44:54 +0000
committerEdwin Cheng <[email protected]>2021-03-27 05:44:54 +0000
commita193666361f6ea9725b927a35f5baf77da713c0a (patch)
treefa916fcc7e5031df796f42521fb74d775ebdec59
parentc8066ebd1781a6f6f536abe3494477bd69df795a (diff)
Basic Support Macro 2.0
-rw-r--r--crates/hir_def/src/nameres/collector.rs102
-rw-r--r--crates/hir_def/src/nameres/tests/macros.rs22
-rw-r--r--crates/hir_expand/src/db.rs60
-rw-r--r--crates/hir_expand/src/hygiene.rs2
-rw-r--r--crates/hir_expand/src/lib.rs2
-rw-r--r--crates/hir_ty/src/tests/macros.rs83
-rw-r--r--crates/syntax/src/ast/node_ext.rs1
7 files changed, 209 insertions, 63 deletions
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs
index d8fabe49b..d58135ec9 100644
--- a/crates/hir_def/src/nameres/collector.rs
+++ b/crates/hir_def/src/nameres/collector.rs
@@ -25,8 +25,8 @@ use crate::{
25 derive_macro_as_call_id, 25 derive_macro_as_call_id,
26 item_scope::{ImportType, PerNsGlobImports}, 26 item_scope::{ImportType, PerNsGlobImports},
27 item_tree::{ 27 item_tree::{
28 self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, MacroRules, Mod, ModItem, ModKind, 28 self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, MacroDef, MacroRules, Mod, ModItem,
29 StructDefKind, 29 ModKind, StructDefKind,
30 }, 30 },
31 macro_call_as_call_id, 31 macro_call_as_call_id,
32 nameres::{ 32 nameres::{
@@ -395,7 +395,7 @@ impl DefCollector<'_> {
395 /// macro_rules! foo { () => {} } 395 /// macro_rules! foo { () => {} }
396 /// use foo as bar; 396 /// use foo as bar;
397 /// ``` 397 /// ```
398 fn define_macro( 398 fn define_macro_rules(
399 &mut self, 399 &mut self,
400 module_id: LocalModuleId, 400 module_id: LocalModuleId,
401 name: Name, 401 name: Name,
@@ -430,6 +430,21 @@ impl DefCollector<'_> {
430 self.def_map.modules[module_id].scope.define_legacy_macro(name, mac); 430 self.def_map.modules[module_id].scope.define_legacy_macro(name, mac);
431 } 431 }
432 432
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
433 /// Define a proc macro 448 /// Define a proc macro
434 /// 449 ///
435 /// A proc macro is similar to normal macro scope, but it would not visible in legacy textual scoped. 450 /// A proc macro is similar to normal macro scope, but it would not visible in legacy textual scoped.
@@ -1067,40 +1082,7 @@ impl ModCollector<'_, '_> {
1067 } 1082 }
1068 ModItem::MacroCall(mac) => self.collect_macro_call(&self.item_tree[mac]), 1083 ModItem::MacroCall(mac) => self.collect_macro_call(&self.item_tree[mac]),
1069 ModItem::MacroRules(id) => self.collect_macro_rules(id), 1084 ModItem::MacroRules(id) => self.collect_macro_rules(id),
1070 ModItem::MacroDef(id) => { 1085 ModItem::MacroDef(id) => self.collect_macro_def(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 }
1104 ModItem::Impl(imp) => { 1086 ModItem::Impl(imp) => {
1105 let module = self.def_collector.def_map.module_id(self.module_id); 1087 let module = self.def_collector.def_map.module_id(self.module_id);
1106 let impl_id = 1088 let impl_id =
@@ -1420,7 +1402,7 @@ impl ModCollector<'_, '_> {
1420 if attrs.by_key("rustc_builtin_macro").exists() { 1402 if attrs.by_key("rustc_builtin_macro").exists() {
1421 let krate = self.def_collector.def_map.krate; 1403 let krate = self.def_collector.def_map.krate;
1422 if let Some(macro_id) = find_builtin_macro(&mac.name, krate, ast_id) { 1404 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(
1424 self.module_id, 1406 self.module_id,
1425 mac.name.clone(), 1407 mac.name.clone(),
1426 macro_id, 1408 macro_id,
@@ -1436,7 +1418,49 @@ impl ModCollector<'_, '_> {
1436 kind: MacroDefKind::Declarative(ast_id), 1418 kind: MacroDefKind::Declarative(ast_id),
1437 local_inner: is_local_inner, 1419 local_inner: is_local_inner,
1438 }; 1420 };
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 );
1440 } 1464 }
1441 1465
1442 fn collect_macro_call(&mut self, mac: &MacroCall) { 1466 fn collect_macro_call(&mut self, mac: &MacroCall) {
diff --git a/crates/hir_def/src/nameres/tests/macros.rs b/crates/hir_def/src/nameres/tests/macros.rs
index 6d3cb8d7a..9986e99e4 100644
--- a/crates/hir_def/src/nameres/tests/macros.rs
+++ b/crates/hir_def/src/nameres/tests/macros.rs
@@ -837,3 +837,25 @@ fn collects_derive_helpers() {
837 _ => unreachable!(), 837 _ => unreachable!(),
838 } 838 }
839} 839}
840
841#[test]
842fn resolve_macro_def() {
843 check(
844 r#"
845//- /lib.rs
846pub macro structs($($i:ident),*) {
847 $(struct $i { field: u32 } )*
848}
849
850structs!(Foo);
851
852//- /nested.rs
853structs!(Bar, Baz);
854"#,
855 expect![[r#"
856 crate
857 Foo: t
858 structs: m
859 "#]],
860 );
861}
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index d672f6723..c0ab70b60 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -3,7 +3,7 @@
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use base_db::{salsa, SourceDatabase}; 5use base_db::{salsa, SourceDatabase};
6use mbe::{ExpandError, ExpandResult, MacroRules}; 6use mbe::{ExpandError, ExpandResult, MacroDef, MacroRules};
7use parser::FragmentKind; 7use parser::FragmentKind;
8use syntax::{ 8use syntax::{
9 algo::diff, 9 algo::diff,
@@ -28,6 +28,7 @@ const TOKEN_LIMIT: usize = 524288;
28#[derive(Debug, Clone, Eq, PartialEq)] 28#[derive(Debug, Clone, Eq, PartialEq)]
29pub enum TokenExpander { 29pub enum TokenExpander {
30 MacroRules(mbe::MacroRules), 30 MacroRules(mbe::MacroRules),
31 MacroDef(mbe::MacroDef),
31 Builtin(BuiltinFnLikeExpander), 32 Builtin(BuiltinFnLikeExpander),
32 BuiltinDerive(BuiltinDeriveExpander), 33 BuiltinDerive(BuiltinDeriveExpander),
33 ProcMacro(ProcMacroExpander), 34 ProcMacro(ProcMacroExpander),
@@ -42,6 +43,7 @@ impl TokenExpander {
42 ) -> mbe::ExpandResult<tt::Subtree> { 43 ) -> mbe::ExpandResult<tt::Subtree> {
43 match self { 44 match self {
44 TokenExpander::MacroRules(it) => it.expand(tt), 45 TokenExpander::MacroRules(it) => it.expand(tt),
46 TokenExpander::MacroDef(it) => it.expand(tt),
45 TokenExpander::Builtin(it) => it.expand(db, id, tt), 47 TokenExpander::Builtin(it) => it.expand(db, id, tt),
46 // FIXME switch these to ExpandResult as well 48 // FIXME switch these to ExpandResult as well
47 TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(), 49 TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(),
@@ -57,6 +59,7 @@ impl TokenExpander {
57 pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId { 59 pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
58 match self { 60 match self {
59 TokenExpander::MacroRules(it) => it.map_id_down(id), 61 TokenExpander::MacroRules(it) => it.map_id_down(id),
62 TokenExpander::MacroDef(it) => it.map_id_down(id),
60 TokenExpander::Builtin(..) => id, 63 TokenExpander::Builtin(..) => id,
61 TokenExpander::BuiltinDerive(..) => id, 64 TokenExpander::BuiltinDerive(..) => id,
62 TokenExpander::ProcMacro(..) => id, 65 TokenExpander::ProcMacro(..) => id,
@@ -66,6 +69,7 @@ impl TokenExpander {
66 pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) { 69 pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) {
67 match self { 70 match self {
68 TokenExpander::MacroRules(it) => it.map_id_up(id), 71 TokenExpander::MacroRules(it) => it.map_id_up(id),
72 TokenExpander::MacroDef(it) => it.map_id_up(id),
69 TokenExpander::Builtin(..) => (id, mbe::Origin::Call), 73 TokenExpander::Builtin(..) => (id, mbe::Origin::Call),
70 TokenExpander::BuiltinDerive(..) => (id, mbe::Origin::Call), 74 TokenExpander::BuiltinDerive(..) => (id, mbe::Origin::Call),
71 TokenExpander::ProcMacro(..) => (id, mbe::Origin::Call), 75 TokenExpander::ProcMacro(..) => (id, mbe::Origin::Call),
@@ -136,26 +140,40 @@ fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
136 140
137fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> { 141fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
138 match id.kind { 142 match id.kind {
139 MacroDefKind::Declarative(ast_id) => { 143 MacroDefKind::Declarative(ast_id) => match ast_id.to_node(db) {
140 let macro_rules = match ast_id.to_node(db) { 144 syntax::ast::Macro::MacroRules(macro_rules) => {
141 syntax::ast::Macro::MacroRules(mac) => mac, 145 let arg = macro_rules.token_tree()?;
142 syntax::ast::Macro::MacroDef(_) => return None, 146 let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
143 }; 147 log::warn!("fail on macro_rules to token tree: {:#?}", arg);
144 let arg = macro_rules.token_tree()?; 148 None
145 let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { 149 })?;
146 log::warn!("fail on macro_def to token tree: {:#?}", arg); 150 let rules = match MacroRules::parse(&tt) {
147 None 151 Ok(it) => it,
148 })?; 152 Err(err) => {
149 let rules = match MacroRules::parse(&tt) { 153 let name = macro_rules.name().map(|n| n.to_string()).unwrap_or_default();
150 Ok(it) => it, 154 log::warn!("fail on macro_def parse ({}): {:?} {:#?}", name, err, tt);
151 Err(err) => { 155 return None;
152 let name = macro_rules.name().map(|n| n.to_string()).unwrap_or_default(); 156 }
153 log::warn!("fail on macro_def parse ({}): {:?} {:#?}", name, err, tt); 157 };
154 return None; 158 Some(Arc::new((TokenExpander::MacroRules(rules), tmap)))
155 } 159 }
156 }; 160 syntax::ast::Macro::MacroDef(macro_def) => {
157 Some(Arc::new((TokenExpander::MacroRules(rules), tmap))) 161 let arg = macro_def.body()?;
158 } 162 let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
163 log::warn!("fail on macro_def to token tree: {:#?}", arg);
164 None
165 })?;
166 let rules = match MacroDef::parse(&tt) {
167 Ok(it) => it,
168 Err(err) => {
169 let name = macro_def.name().map(|n| n.to_string()).unwrap_or_default();
170 log::warn!("fail on macro_def parse ({}): {:?} {:#?}", name, err, tt);
171 return None;
172 }
173 };
174 Some(Arc::new((TokenExpander::MacroDef(rules), tmap)))
175 }
176 },
159 MacroDefKind::BuiltIn(expander, _) => { 177 MacroDefKind::BuiltIn(expander, _) => {
160 Some(Arc::new((TokenExpander::Builtin(expander), mbe::TokenMap::default()))) 178 Some(Arc::new((TokenExpander::Builtin(expander), mbe::TokenMap::default())))
161 } 179 }
diff --git a/crates/hir_expand/src/hygiene.rs b/crates/hir_expand/src/hygiene.rs
index 0e0f7214a..779725629 100644
--- a/crates/hir_expand/src/hygiene.rs
+++ b/crates/hir_expand/src/hygiene.rs
@@ -148,7 +148,7 @@ fn make_hygiene_info(
148 let def_offset = loc.def.ast_id().left().and_then(|id| { 148 let def_offset = loc.def.ast_id().left().and_then(|id| {
149 let def_tt = match id.to_node(db) { 149 let def_tt = match id.to_node(db) {
150 ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(), 150 ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(),
151 ast::Macro::MacroDef(_) => return None, 151 ast::Macro::MacroDef(mac) => mac.body()?.syntax().text_range().start(),
152 }; 152 };
153 Some(InFile::new(id.file_id, def_tt)) 153 Some(InFile::new(id.file_id, def_tt))
154 }); 154 });
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index b8045fda9..3e332ee47 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -151,7 +151,7 @@ impl HirFileId {
151 let def = loc.def.ast_id().left().and_then(|id| { 151 let def = loc.def.ast_id().left().and_then(|id| {
152 let def_tt = match id.to_node(db) { 152 let def_tt = match id.to_node(db) {
153 ast::Macro::MacroRules(mac) => mac.token_tree()?, 153 ast::Macro::MacroRules(mac) => mac.token_tree()?,
154 ast::Macro::MacroDef(_) => return None, 154 ast::Macro::MacroDef(mac) => mac.body()?,
155 }; 155 };
156 Some(InFile::new(id.file_id, def_tt)) 156 Some(InFile::new(id.file_id, def_tt))
157 }); 157 });
diff --git a/crates/hir_ty/src/tests/macros.rs b/crates/hir_ty/src/tests/macros.rs
index 01935ec99..3eb01dbd0 100644
--- a/crates/hir_ty/src/tests/macros.rs
+++ b/crates/hir_ty/src/tests/macros.rs
@@ -135,7 +135,88 @@ fn infer_path_qualified_macros_expanded() {
135} 135}
136 136
137#[test] 137#[test]
138fn expr_macro_expanded_in_various_places() { 138fn expr_macro_def_expanded_in_various_places() {
139 check_infer(
140 r#"
141 macro spam() {
142 1isize
143 }
144
145 fn spam() {
146 spam!();
147 (spam!());
148 spam!().spam(spam!());
149 for _ in spam!() {}
150 || spam!();
151 while spam!() {}
152 break spam!();
153 return spam!();
154 match spam!() {
155 _ if spam!() => spam!(),
156 }
157 spam!()(spam!());
158 Spam { spam: spam!() };
159 spam!()[spam!()];
160 await spam!();
161 spam!() as usize;
162 &spam!();
163 -spam!();
164 spam!()..spam!();
165 spam!() + spam!();
166 }
167 "#,
168 expect![[r#"
169 !0..6 '1isize': isize
170 !0..6 '1isize': isize
171 !0..6 '1isize': isize
172 !0..6 '1isize': isize
173 !0..6 '1isize': isize
174 !0..6 '1isize': isize
175 !0..6 '1isize': isize
176 !0..6 '1isize': isize
177 !0..6 '1isize': isize
178 !0..6 '1isize': isize
179 !0..6 '1isize': isize
180 !0..6 '1isize': isize
181 !0..6 '1isize': isize
182 !0..6 '1isize': isize
183 !0..6 '1isize': isize
184 !0..6 '1isize': isize
185 !0..6 '1isize': isize
186 !0..6 '1isize': isize
187 !0..6 '1isize': isize
188 !0..6 '1isize': isize
189 !0..6 '1isize': isize
190 !0..6 '1isize': isize
191 !0..6 '1isize': isize
192 !0..6 '1isize': isize
193 !0..6 '1isize': isize
194 39..442 '{ ...!(); }': ()
195 73..94 'spam!(...am!())': {unknown}
196 100..119 'for _ ...!() {}': ()
197 104..105 '_': {unknown}
198 117..119 '{}': ()
199 124..134 '|| spam!()': || -> isize
200 140..156 'while ...!() {}': ()
201 154..156 '{}': ()
202 161..174 'break spam!()': !
203 180..194 'return spam!()': !
204 200..254 'match ... }': isize
205 224..225 '_': isize
206 259..275 'spam!(...am!())': {unknown}
207 281..303 'Spam {...m!() }': {unknown}
208 309..325 'spam!(...am!()]': {unknown}
209 350..366 'spam!(... usize': usize
210 372..380 '&spam!()': &isize
211 386..394 '-spam!()': isize
212 400..416 'spam!(...pam!()': {unknown}
213 422..439 'spam!(...pam!()': isize
214 "#]],
215 );
216}
217
218#[test]
219fn expr_macro_rules_expanded_in_various_places() {
139 check_infer( 220 check_infer(
140 r#" 221 r#"
141 macro_rules! spam { 222 macro_rules! spam {
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index 2772d7364..ae98dbd26 100644
--- a/crates/syntax/src/ast/node_ext.rs
+++ b/crates/syntax/src/ast/node_ext.rs
@@ -40,6 +40,7 @@ fn text_of_first_token(node: &SyntaxNode) -> TokenText {
40 TokenText(first_token) 40 TokenText(first_token)
41} 41}
42 42
43#[derive(Debug, PartialEq, Eq, Clone)]
43pub enum Macro { 44pub enum Macro {
44 MacroRules(ast::MacroRules), 45 MacroRules(ast::MacroRules),
45 MacroDef(ast::MacroDef), 46 MacroDef(ast::MacroDef),