aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_expand/src')
-rw-r--r--crates/hir_expand/src/builtin_derive.rs45
-rw-r--r--crates/hir_expand/src/builtin_macro.rs13
-rw-r--r--crates/hir_expand/src/db.rs14
-rw-r--r--crates/hir_expand/src/eager.rs12
-rw-r--r--crates/hir_expand/src/hygiene.rs12
-rw-r--r--crates/hir_expand/src/lib.rs26
6 files changed, 71 insertions, 51 deletions
diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs
index 5e908b223..60fd2ebdd 100644
--- a/crates/hir_expand/src/builtin_derive.rs
+++ b/crates/hir_expand/src/builtin_derive.rs
@@ -61,8 +61,7 @@ pub fn find_builtin_derive(
61 let expander = BuiltinDeriveExpander::find_by_name(ident)?; 61 let expander = BuiltinDeriveExpander::find_by_name(ident)?;
62 Some(MacroDefId { 62 Some(MacroDefId {
63 krate, 63 krate,
64 ast_id: Some(ast_id), 64 kind: MacroDefKind::BuiltInDerive(expander, ast_id),
65 kind: MacroDefKind::BuiltInDerive(expander),
66 local_inner: false, 65 local_inner: false,
67 }) 66 })
68} 67}
@@ -268,14 +267,13 @@ fn partial_ord_expand(
268mod tests { 267mod tests {
269 use base_db::{fixture::WithFixture, CrateId, SourceDatabase}; 268 use base_db::{fixture::WithFixture, CrateId, SourceDatabase};
270 use expect_test::{expect, Expect}; 269 use expect_test::{expect, Expect};
271 use name::{known, Name}; 270 use name::AsName;
272 271
273 use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc}; 272 use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
274 273
275 use super::*; 274 use super::*;
276 275
277 fn expand_builtin_derive(ra_fixture: &str, name: Name) -> String { 276 fn expand_builtin_derive(ra_fixture: &str) -> String {
278 let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap();
279 let fixture = format!( 277 let fixture = format!(
280 r#"//- /main.rs crate:main deps:core 278 r#"//- /main.rs crate:main deps:core
281$0 279$0
@@ -288,19 +286,34 @@ $0
288 286
289 let (db, file_pos) = TestDB::with_position(&fixture); 287 let (db, file_pos) = TestDB::with_position(&fixture);
290 let file_id = file_pos.file_id; 288 let file_id = file_pos.file_id;
289 let ast_id_map = db.ast_id_map(file_id.into());
291 let parsed = db.parse(file_id); 290 let parsed = db.parse(file_id);
292 let items: Vec<_> = 291 let macros: Vec<_> =
293 parsed.syntax_node().descendants().filter_map(ast::Item::cast).collect(); 292 parsed.syntax_node().descendants().filter_map(ast::Macro::cast).collect();
293 let items: Vec<_> = parsed
294 .syntax_node()
295 .descendants()
296 .filter(|node| !ast::Macro::can_cast(node.kind()))
297 .filter_map(ast::Item::cast)
298 .collect();
299
300 assert_eq!(macros.len(), 1, "test must contain exactly 1 macro definition");
301 assert_eq!(items.len(), 1, "test must contain exactly 1 item");
302
303 let macro_ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macros[0]));
304 let name = match &macros[0] {
305 ast::Macro::MacroRules(rules) => rules.name().unwrap().as_name(),
306 ast::Macro::MacroDef(def) => def.name().unwrap().as_name(),
307 };
294 308
295 let ast_id_map = db.ast_id_map(file_id.into()); 309 let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap();
296 310
297 let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0])); 311 let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]));
298 312
299 let loc = MacroCallLoc { 313 let loc = MacroCallLoc {
300 def: MacroDefId { 314 def: MacroDefId {
301 krate: CrateId(0), 315 krate: CrateId(0),
302 ast_id: None, 316 kind: MacroDefKind::BuiltInDerive(expander, macro_ast_id),
303 kind: MacroDefKind::BuiltInDerive(expander),
304 local_inner: false, 317 local_inner: false,
305 }, 318 },
306 krate: CrateId(0), 319 krate: CrateId(0),
@@ -315,8 +328,8 @@ $0
315 parsed.text().to_string() 328 parsed.text().to_string()
316 } 329 }
317 330
318 fn check_derive(ra_fixture: &str, name: Name, expected: Expect) { 331 fn check_derive(ra_fixture: &str, expected: Expect) {
319 let expanded = expand_builtin_derive(ra_fixture, name); 332 let expanded = expand_builtin_derive(ra_fixture);
320 expected.assert_eq(&expanded); 333 expected.assert_eq(&expanded);
321 } 334 }
322 335
@@ -324,10 +337,10 @@ $0
324 fn test_copy_expand_simple() { 337 fn test_copy_expand_simple() {
325 check_derive( 338 check_derive(
326 r#" 339 r#"
340 macro Copy {}
327 #[derive(Copy)] 341 #[derive(Copy)]
328 struct Foo; 342 struct Foo;
329 "#, 343 "#,
330 known::Copy,
331 expect![["impl< >core::marker::CopyforFoo< >{}"]], 344 expect![["impl< >core::marker::CopyforFoo< >{}"]],
332 ); 345 );
333 } 346 }
@@ -336,10 +349,10 @@ $0
336 fn test_copy_expand_with_type_params() { 349 fn test_copy_expand_with_type_params() {
337 check_derive( 350 check_derive(
338 r#" 351 r#"
352 macro Copy {}
339 #[derive(Copy)] 353 #[derive(Copy)]
340 struct Foo<A, B>; 354 struct Foo<A, B>;
341 "#, 355 "#,
342 known::Copy,
343 expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]], 356 expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
344 ); 357 );
345 } 358 }
@@ -348,10 +361,10 @@ $0
348 fn test_copy_expand_with_lifetimes() { 361 fn test_copy_expand_with_lifetimes() {
349 check_derive( 362 check_derive(
350 r#" 363 r#"
364 macro Copy {}
351 #[derive(Copy)] 365 #[derive(Copy)]
352 struct Foo<A, B, 'a, 'b>; 366 struct Foo<A, B, 'a, 'b>;
353 "#, 367 "#,
354 known::Copy,
355 // We currently just ignore lifetimes 368 // We currently just ignore lifetimes
356 expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]], 369 expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
357 ); 370 );
@@ -361,10 +374,10 @@ $0
361 fn test_clone_expand() { 374 fn test_clone_expand() {
362 check_derive( 375 check_derive(
363 r#" 376 r#"
377 macro Clone {}
364 #[derive(Clone)] 378 #[derive(Clone)]
365 struct Foo<A, B>; 379 struct Foo<A, B>;
366 "#, 380 "#,
367 known::Clone,
368 expect![["impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"]], 381 expect![["impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"]],
369 ); 382 );
370 } 383 }
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs
index fce09a9e7..8529b43b6 100644
--- a/crates/hir_expand/src/builtin_macro.rs
+++ b/crates/hir_expand/src/builtin_macro.rs
@@ -71,14 +71,12 @@ pub fn find_builtin_macro(
71 match kind { 71 match kind {
72 Either::Left(kind) => Some(MacroDefId { 72 Either::Left(kind) => Some(MacroDefId {
73 krate, 73 krate,
74 ast_id: Some(ast_id), 74 kind: MacroDefKind::BuiltIn(kind, ast_id),
75 kind: MacroDefKind::BuiltIn(kind),
76 local_inner: false, 75 local_inner: false,
77 }), 76 }),
78 Either::Right(kind) => Some(MacroDefId { 77 Either::Right(kind) => Some(MacroDefId {
79 krate, 78 krate,
80 ast_id: Some(ast_id), 79 kind: MacroDefKind::BuiltInEager(kind, ast_id),
81 kind: MacroDefKind::BuiltInEager(kind),
82 local_inner: false, 80 local_inner: false,
83 }), 81 }),
84 } 82 }
@@ -512,6 +510,7 @@ mod tests {
512 let macro_call = macro_calls.pop().unwrap(); 510 let macro_call = macro_calls.pop().unwrap();
513 511
514 let expander = find_by_name(&macro_rules.name().unwrap().as_name()).unwrap(); 512 let expander = find_by_name(&macro_rules.name().unwrap().as_name()).unwrap();
513 let ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_rules));
515 514
516 let krate = CrateId(0); 515 let krate = CrateId(0);
517 let file_id = match expander { 516 let file_id = match expander {
@@ -519,8 +518,7 @@ mod tests {
519 // the first one should be a macro_rules 518 // the first one should be a macro_rules
520 let def = MacroDefId { 519 let def = MacroDefId {
521 krate: CrateId(0), 520 krate: CrateId(0),
522 ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_rules))), 521 kind: MacroDefKind::BuiltIn(expander, ast_id),
523 kind: MacroDefKind::BuiltIn(expander),
524 local_inner: false, 522 local_inner: false,
525 }; 523 };
526 524
@@ -540,8 +538,7 @@ mod tests {
540 // the first one should be a macro_rules 538 // the first one should be a macro_rules
541 let def = MacroDefId { 539 let def = MacroDefId {
542 krate, 540 krate,
543 ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_rules))), 541 kind: MacroDefKind::BuiltInEager(expander, ast_id),
544 kind: MacroDefKind::BuiltInEager(expander),
545 local_inner: false, 542 local_inner: false,
546 }; 543 };
547 544
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index a3070f1f9..2748e25cf 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -130,8 +130,8 @@ fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
130 130
131fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> { 131fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
132 match id.kind { 132 match id.kind {
133 MacroDefKind::Declarative => { 133 MacroDefKind::Declarative(ast_id) => {
134 let macro_rules = match id.ast_id?.to_node(db) { 134 let macro_rules = match ast_id.to_node(db) {
135 syntax::ast::Macro::MacroRules(mac) => mac, 135 syntax::ast::Macro::MacroRules(mac) => mac,
136 syntax::ast::Macro::MacroDef(_) => return None, 136 syntax::ast::Macro::MacroDef(_) => return None,
137 }; 137 };
@@ -150,14 +150,14 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander,
150 }; 150 };
151 Some(Arc::new((TokenExpander::MacroRules(rules), tmap))) 151 Some(Arc::new((TokenExpander::MacroRules(rules), tmap)))
152 } 152 }
153 MacroDefKind::BuiltIn(expander) => { 153 MacroDefKind::BuiltIn(expander, _) => {
154 Some(Arc::new((TokenExpander::Builtin(expander), mbe::TokenMap::default()))) 154 Some(Arc::new((TokenExpander::Builtin(expander), mbe::TokenMap::default())))
155 } 155 }
156 MacroDefKind::BuiltInDerive(expander) => { 156 MacroDefKind::BuiltInDerive(expander, _) => {
157 Some(Arc::new((TokenExpander::BuiltinDerive(expander), mbe::TokenMap::default()))) 157 Some(Arc::new((TokenExpander::BuiltinDerive(expander), mbe::TokenMap::default())))
158 } 158 }
159 MacroDefKind::BuiltInEager(_) => None, 159 MacroDefKind::BuiltInEager(..) => None,
160 MacroDefKind::ProcMacro(expander) => { 160 MacroDefKind::ProcMacro(expander, ..) => {
161 Some(Arc::new((TokenExpander::ProcMacro(expander), mbe::TokenMap::default()))) 161 Some(Arc::new((TokenExpander::ProcMacro(expander), mbe::TokenMap::default())))
162 } 162 }
163 } 163 }
@@ -269,7 +269,7 @@ fn expand_proc_macro(
269 }; 269 };
270 270
271 let expander = match loc.def.kind { 271 let expander = match loc.def.kind {
272 MacroDefKind::ProcMacro(expander) => expander, 272 MacroDefKind::ProcMacro(expander, ..) => expander,
273 _ => unreachable!(), 273 _ => unreachable!(),
274 }; 274 };
275 275
diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs
index dc618a9ee..04f374a29 100644
--- a/crates/hir_expand/src/eager.rs
+++ b/crates/hir_expand/src/eager.rs
@@ -140,7 +140,7 @@ pub fn expand_eager_macro(
140 let subtree = 140 let subtree =
141 diagnostic_sink.option(to_subtree(&result), || err("failed to parse macro result"))?; 141 diagnostic_sink.option(to_subtree(&result), || err("failed to parse macro result"))?;
142 142
143 if let MacroDefKind::BuiltInEager(eager) = def.kind { 143 if let MacroDefKind::BuiltInEager(eager, _) = def.kind {
144 let res = eager.expand(db, arg_id, &subtree); 144 let res = eager.expand(db, arg_id, &subtree);
145 145
146 let (subtree, fragment) = diagnostic_sink.expand_result_option(res)?; 146 let (subtree, fragment) = diagnostic_sink.expand_result_option(res)?;
@@ -193,7 +193,7 @@ fn eager_macro_recur(
193 let def = diagnostic_sink 193 let def = diagnostic_sink
194 .option_with(|| macro_resolver(child.path()?), || err("failed to resolve macro"))?; 194 .option_with(|| macro_resolver(child.path()?), || err("failed to resolve macro"))?;
195 let insert = match def.kind { 195 let insert = match def.kind {
196 MacroDefKind::BuiltInEager(_) => { 196 MacroDefKind::BuiltInEager(..) => {
197 let id: MacroCallId = expand_eager_macro( 197 let id: MacroCallId = expand_eager_macro(
198 db, 198 db,
199 krate, 199 krate,
@@ -206,10 +206,10 @@ fn eager_macro_recur(
206 db.parse_or_expand(id.as_file()) 206 db.parse_or_expand(id.as_file())
207 .expect("successful macro expansion should be parseable") 207 .expect("successful macro expansion should be parseable")
208 } 208 }
209 MacroDefKind::Declarative 209 MacroDefKind::Declarative(_)
210 | MacroDefKind::BuiltIn(_) 210 | MacroDefKind::BuiltIn(..)
211 | MacroDefKind::BuiltInDerive(_) 211 | MacroDefKind::BuiltInDerive(..)
212 | MacroDefKind::ProcMacro(_) => { 212 | MacroDefKind::ProcMacro(..) => {
213 let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate); 213 let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate);
214 let val = diagnostic_sink.expand_result_option(res)?; 214 let val = diagnostic_sink.expand_result_option(res)?;
215 215
diff --git a/crates/hir_expand/src/hygiene.rs b/crates/hir_expand/src/hygiene.rs
index 87cad326d..20cda1683 100644
--- a/crates/hir_expand/src/hygiene.rs
+++ b/crates/hir_expand/src/hygiene.rs
@@ -145,7 +145,7 @@ fn make_hygiene_info(
145) -> Option<HygieneInfo> { 145) -> Option<HygieneInfo> {
146 let arg_tt = loc.kind.arg(db)?; 146 let arg_tt = loc.kind.arg(db)?;
147 147
148 let def_offset = loc.def.ast_id.and_then(|id| { 148 let def_offset = loc.def.ast_id().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(_) => return None,
@@ -176,13 +176,13 @@ impl HygieneFrame {
176 let loc = db.lookup_intern_macro(id); 176 let loc = db.lookup_intern_macro(id);
177 let info = make_hygiene_info(db, macro_file, &loc); 177 let info = make_hygiene_info(db, macro_file, &loc);
178 match loc.def.kind { 178 match loc.def.kind {
179 MacroDefKind::Declarative => { 179 MacroDefKind::Declarative(_) => {
180 (info, Some(loc.def.krate), loc.def.local_inner) 180 (info, Some(loc.def.krate), loc.def.local_inner)
181 } 181 }
182 MacroDefKind::BuiltIn(_) => (info, Some(loc.def.krate), false), 182 MacroDefKind::BuiltIn(..) => (info, Some(loc.def.krate), false),
183 MacroDefKind::BuiltInDerive(_) => (info, None, false), 183 MacroDefKind::BuiltInDerive(..) => (info, None, false),
184 MacroDefKind::BuiltInEager(_) => (info, None, false), 184 MacroDefKind::BuiltInEager(..) => (info, None, false),
185 MacroDefKind::ProcMacro(_) => (info, None, false), 185 MacroDefKind::ProcMacro(..) => (info, None, false),
186 } 186 }
187 } 187 }
188 }, 188 },
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index 7532d00b8..0a379651f 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -143,7 +143,7 @@ impl HirFileId {
143 143
144 let arg_tt = loc.kind.arg(db)?; 144 let arg_tt = loc.kind.arg(db)?;
145 145
146 let def = loc.def.ast_id.and_then(|id| { 146 let def = loc.def.ast_id().and_then(|id| {
147 let def_tt = match id.to_node(db) { 147 let def_tt = match id.to_node(db) {
148 ast::Macro::MacroRules(mac) => mac.token_tree()?, 148 ast::Macro::MacroRules(mac) => mac.token_tree()?,
149 ast::Macro::MacroDef(_) => return None, 149 ast::Macro::MacroDef(_) => return None,
@@ -180,7 +180,7 @@ impl HirFileId {
180 }; 180 };
181 let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); 181 let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id);
182 let item = match loc.def.kind { 182 let item = match loc.def.kind {
183 MacroDefKind::BuiltInDerive(_) => loc.kind.node(db), 183 MacroDefKind::BuiltInDerive(..) => loc.kind.node(db),
184 _ => return None, 184 _ => return None,
185 }; 185 };
186 Some(item.with_value(ast::Item::cast(item.value.clone())?)) 186 Some(item.with_value(ast::Item::cast(item.value.clone())?))
@@ -224,7 +224,6 @@ impl From<EagerMacroId> for MacroCallId {
224#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 224#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
225pub struct MacroDefId { 225pub struct MacroDefId {
226 pub krate: CrateId, 226 pub krate: CrateId,
227 pub ast_id: Option<AstId<ast::Macro>>,
228 pub kind: MacroDefKind, 227 pub kind: MacroDefKind,
229 228
230 pub local_inner: bool, 229 pub local_inner: bool,
@@ -239,16 +238,27 @@ impl MacroDefId {
239 ) -> LazyMacroId { 238 ) -> LazyMacroId {
240 db.intern_macro(MacroCallLoc { def: self, krate, kind }) 239 db.intern_macro(MacroCallLoc { def: self, krate, kind })
241 } 240 }
241
242 pub fn ast_id(&self) -> Option<AstId<ast::Macro>> {
243 let id = match &self.kind {
244 MacroDefKind::Declarative(id) => id,
245 MacroDefKind::BuiltIn(_, id) => id,
246 MacroDefKind::BuiltInDerive(_, id) => id,
247 MacroDefKind::BuiltInEager(_, id) => id,
248 MacroDefKind::ProcMacro(..) => return None,
249 };
250 Some(*id)
251 }
242} 252}
243 253
244#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 254#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
245pub enum MacroDefKind { 255pub enum MacroDefKind {
246 Declarative, 256 Declarative(AstId<ast::Macro>),
247 BuiltIn(BuiltinFnLikeExpander), 257 BuiltIn(BuiltinFnLikeExpander, AstId<ast::Macro>),
248 // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander 258 // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander
249 BuiltInDerive(BuiltinDeriveExpander), 259 BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>),
250 BuiltInEager(EagerExpander), 260 BuiltInEager(EagerExpander, AstId<ast::Macro>),
251 ProcMacro(ProcMacroExpander), 261 ProcMacro(ProcMacroExpander, AstId<ast::Fn>),
252} 262}
253 263
254#[derive(Debug, Clone, PartialEq, Eq, Hash)] 264#[derive(Debug, Clone, PartialEq, Eq, Hash)]