@@ -268,14 +268,13 @@ fn partial_ord_expand(
268268mod tests {
269269 use base_db:: { fixture:: WithFixture , CrateId , SourceDatabase } ;
270270 use expect_test:: { expect, Expect } ;
271- use name:: { known , Name } ;
271+ use name:: AsName ;
272272
273273 use crate :: { test_db:: TestDB , AstId , MacroCallId , MacroCallKind , MacroCallLoc } ;
274274
275275 use super :: * ;
276276
277- fn expand_builtin_derive ( ra_fixture : & str , name : Name ) -> String {
278- let expander = BuiltinDeriveExpander :: find_by_name ( & name) . unwrap ( ) ;
277+ fn expand_builtin_derive ( ra_fixture : & str ) -> String {
279278 let fixture = format ! (
280279 r#"//- /main.rs crate:main deps:core
281280$0
288287
289288 let ( db, file_pos) = TestDB :: with_position ( & fixture) ;
290289 let file_id = file_pos. file_id ;
290+ let ast_id_map = db. ast_id_map ( file_id. into ( ) ) ;
291291 let parsed = db. parse ( file_id) ;
292- let items: Vec < _ > =
293- parsed. syntax_node ( ) . descendants ( ) . filter_map ( ast:: Item :: cast) . collect ( ) ;
292+ let macros: Vec < _ > =
293+ parsed. syntax_node ( ) . descendants ( ) . filter_map ( ast:: Macro :: cast) . collect ( ) ;
294+ let items: Vec < _ > = parsed
295+ . syntax_node ( )
296+ . descendants ( )
297+ . filter ( |node| !ast:: Macro :: can_cast ( node. kind ( ) ) )
298+ . filter_map ( ast:: Item :: cast)
299+ . collect ( ) ;
300+
301+ assert_eq ! ( macros. len( ) , 1 , "test must contain exactly 1 macro definition" ) ;
302+ assert_eq ! ( items. len( ) , 1 , "test must contain exactly 1 item" ) ;
303+
304+ let macro_ast_id = AstId :: new ( file_id. into ( ) , ast_id_map. ast_id ( & macros[ 0 ] ) ) ;
305+ let name = match & macros[ 0 ] {
306+ ast:: Macro :: MacroRules ( rules) => rules. name ( ) . unwrap ( ) . as_name ( ) ,
307+ ast:: Macro :: MacroDef ( def) => def. name ( ) . unwrap ( ) . as_name ( ) ,
308+ } ;
294309
295- let ast_id_map = db . ast_id_map ( file_id . into ( ) ) ;
310+ let expander = BuiltinDeriveExpander :: find_by_name ( & name ) . unwrap ( ) ;
296311
297312 let attr_id = AstId :: new ( file_id. into ( ) , ast_id_map. ast_id ( & items[ 0 ] ) ) ;
298313
299314 let loc = MacroCallLoc {
300315 def : MacroDefId {
301316 krate : CrateId ( 0 ) ,
302- ast_id : None ,
317+ ast_id : Some ( macro_ast_id ) ,
303318 kind : MacroDefKind :: BuiltInDerive ( expander) ,
304319 local_inner : false ,
305320 } ,
315330 parsed. text ( ) . to_string ( )
316331 }
317332
318- fn check_derive ( ra_fixture : & str , name : Name , expected : Expect ) {
319- let expanded = expand_builtin_derive ( ra_fixture, name ) ;
333+ fn check_derive ( ra_fixture : & str , expected : Expect ) {
334+ let expanded = expand_builtin_derive ( ra_fixture) ;
320335 expected. assert_eq ( & expanded) ;
321336 }
322337
323338 #[ test]
324339 fn test_copy_expand_simple ( ) {
325340 check_derive (
326341 r#"
342+ macro Copy {}
327343 #[derive(Copy)]
328344 struct Foo;
329345 "# ,
330- known:: Copy ,
331346 expect ! [ [ "impl< >core::marker::CopyforFoo< >{}" ] ] ,
332347 ) ;
333348 }
336351 fn test_copy_expand_with_type_params ( ) {
337352 check_derive (
338353 r#"
354+ macro Copy {}
339355 #[derive(Copy)]
340356 struct Foo<A, B>;
341357 "# ,
342- known:: Copy ,
343358 expect ! [ [ "impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}" ] ] ,
344359 ) ;
345360 }
348363 fn test_copy_expand_with_lifetimes ( ) {
349364 check_derive (
350365 r#"
366+ macro Copy {}
351367 #[derive(Copy)]
352368 struct Foo<A, B, 'a, 'b>;
353369 "# ,
354- known:: Copy ,
355370 // We currently just ignore lifetimes
356371 expect ! [ [ "impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}" ] ] ,
357372 ) ;
361376 fn test_clone_expand ( ) {
362377 check_derive (
363378 r#"
379+ macro Clone {}
364380 #[derive(Clone)]
365381 struct Foo<A, B>;
366382 "# ,
367- known:: Clone ,
368383 expect ! [ [ "impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}" ] ] ,
369384 ) ;
370385 }
0 commit comments