From 4cf36545e65c91a92f7a29681fb7f4346475e81b Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 18 Mar 2021 15:14:52 +0100 Subject: Create AstId for builtin_derive macro in tests --- crates/hir_expand/src/builtin_derive.rs | 41 ++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'crates') diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs index 5e908b223..a8d267c30 100644 --- a/crates/hir_expand/src/builtin_derive.rs +++ b/crates/hir_expand/src/builtin_derive.rs @@ -268,14 +268,13 @@ fn partial_ord_expand( mod tests { use base_db::{fixture::WithFixture, CrateId, SourceDatabase}; use expect_test::{expect, Expect}; - use name::{known, Name}; + use name::AsName; use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc}; use super::*; - fn expand_builtin_derive(ra_fixture: &str, name: Name) -> String { - let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap(); + fn expand_builtin_derive(ra_fixture: &str) -> String { let fixture = format!( r#"//- /main.rs crate:main deps:core $0 @@ -288,18 +287,34 @@ $0 let (db, file_pos) = TestDB::with_position(&fixture); let file_id = file_pos.file_id; + let ast_id_map = db.ast_id_map(file_id.into()); let parsed = db.parse(file_id); - let items: Vec<_> = - parsed.syntax_node().descendants().filter_map(ast::Item::cast).collect(); + let macros: Vec<_> = + parsed.syntax_node().descendants().filter_map(ast::Macro::cast).collect(); + let items: Vec<_> = parsed + .syntax_node() + .descendants() + .filter(|node| !ast::Macro::can_cast(node.kind())) + .filter_map(ast::Item::cast) + .collect(); + + assert_eq!(macros.len(), 1, "test must contain exactly 1 macro definition"); + assert_eq!(items.len(), 1, "test must contain exactly 1 item"); + + let macro_ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(¯os[0])); + let name = match ¯os[0] { + ast::Macro::MacroRules(rules) => rules.name().unwrap().as_name(), + ast::Macro::MacroDef(def) => def.name().unwrap().as_name(), + }; - let ast_id_map = db.ast_id_map(file_id.into()); + let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap(); let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0])); let loc = MacroCallLoc { def: MacroDefId { krate: CrateId(0), - ast_id: None, + ast_id: Some(macro_ast_id), kind: MacroDefKind::BuiltInDerive(expander), local_inner: false, }, @@ -315,8 +330,8 @@ $0 parsed.text().to_string() } - fn check_derive(ra_fixture: &str, name: Name, expected: Expect) { - let expanded = expand_builtin_derive(ra_fixture, name); + fn check_derive(ra_fixture: &str, expected: Expect) { + let expanded = expand_builtin_derive(ra_fixture); expected.assert_eq(&expanded); } @@ -324,10 +339,10 @@ $0 fn test_copy_expand_simple() { check_derive( r#" + macro Copy {} #[derive(Copy)] struct Foo; "#, - known::Copy, expect![["impl< >core::marker::CopyforFoo< >{}"]], ); } @@ -336,10 +351,10 @@ $0 fn test_copy_expand_with_type_params() { check_derive( r#" + macro Copy {} #[derive(Copy)] struct Foo; "#, - known::Copy, expect![["implcore::marker::CopyforFoo{}"]], ); } @@ -348,10 +363,10 @@ $0 fn test_copy_expand_with_lifetimes() { check_derive( r#" + macro Copy {} #[derive(Copy)] struct Foo; "#, - known::Copy, // We currently just ignore lifetimes expect![["implcore::marker::CopyforFoo{}"]], ); @@ -361,10 +376,10 @@ $0 fn test_clone_expand() { check_derive( r#" + macro Clone {} #[derive(Clone)] struct Foo; "#, - known::Clone, expect![["implcore::clone::CloneforFoo{}"]], ); } -- cgit v1.2.3