aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand/src/builtin_derive.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_expand/src/builtin_derive.rs')
-rw-r--r--crates/hir_expand/src/builtin_derive.rs45
1 files changed, 29 insertions, 16 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 }