@@ -10,7 +10,7 @@ use hir_def::{
10
10
resolver:: { self , HasResolver , Resolver , TypeNs } ,
11
11
AsMacroCall , FunctionId , TraitId , VariantId ,
12
12
} ;
13
- use hir_expand:: { name:: AsName , ExpansionInfo , MacroCallLoc } ;
13
+ use hir_expand:: { name:: AsName , ExpansionInfo , MacroCallId , MacroCallLoc } ;
14
14
use hir_ty:: { associated_type_shorthand_candidates, Interner } ;
15
15
use itertools:: Itertools ;
16
16
use rustc_hash:: { FxHashMap , FxHashSet } ;
@@ -448,46 +448,45 @@ impl<'db> SemanticsImpl<'db> {
448
448
}
449
449
450
450
fn resolve_derive_macro ( & self , attr : & ast:: Attr ) -> Option < Vec < MacroDef > > {
451
- let item = attr. syntax ( ) . parent ( ) . and_then ( ast:: Item :: cast) ?;
452
- let file_id = self . find_file ( item. syntax ( ) ) . file_id ;
453
- let item = InFile :: new ( file_id, & item) ;
454
- let src = InFile :: new ( file_id, attr. clone ( ) ) ;
455
- self . with_ctx ( |ctx| {
456
- let macro_call_ids = ctx. attr_to_derive_macro_call ( item, src) ?;
457
-
458
- let res = macro_call_ids
459
- . iter ( )
460
- . map ( |& call| {
461
- let loc: MacroCallLoc = self . db . lookup_intern_macro_call ( call) ;
462
- MacroDef { id : loc. def }
463
- } )
464
- . collect ( ) ;
465
- Some ( res)
466
- } )
451
+ let macro_call_ids = self . derive_macro_calls ( attr) ?;
452
+ let res = macro_call_ids
453
+ . iter ( )
454
+ . map ( |& call| {
455
+ let loc: MacroCallLoc = self . db . lookup_intern_macro_call ( call) ;
456
+ MacroDef { id : loc. def }
457
+ } )
458
+ . collect ( ) ;
459
+ Some ( res)
467
460
}
468
461
469
462
fn expand_derive_macro ( & self , attr : & ast:: Attr ) -> Option < Vec < SyntaxNode > > {
463
+ let macro_call_ids = self . derive_macro_calls ( attr) ?;
464
+
465
+ let expansions: Vec < _ > = macro_call_ids
466
+ . iter ( )
467
+ . map ( |call| call. as_file ( ) )
468
+ . flat_map ( |file_id| {
469
+ let node = self . db . parse_or_expand ( file_id) ?;
470
+ self . cache ( node. clone ( ) , file_id) ;
471
+ Some ( node)
472
+ } )
473
+ . collect ( ) ;
474
+
475
+ if expansions. is_empty ( ) {
476
+ None
477
+ } else {
478
+ Some ( expansions)
479
+ }
480
+ }
481
+
482
+ fn derive_macro_calls ( & self , attr : & ast:: Attr ) -> Option < Vec < MacroCallId > > {
470
483
let item = attr. syntax ( ) . parent ( ) . and_then ( ast:: Item :: cast) ?;
471
484
let file_id = self . find_file ( item. syntax ( ) ) . file_id ;
472
485
let item = InFile :: new ( file_id, & item) ;
473
486
let src = InFile :: new ( file_id, attr. clone ( ) ) ;
474
487
self . with_ctx ( |ctx| {
475
- let macro_call_ids = ctx. attr_to_derive_macro_call ( item, src) ?;
476
-
477
- let expansions: Vec < _ > = macro_call_ids
478
- . iter ( )
479
- . map ( |call| call. as_file ( ) )
480
- . flat_map ( |file_id| {
481
- let node = self . db . parse_or_expand ( file_id) ?;
482
- self . cache ( node. clone ( ) , file_id) ;
483
- Some ( node)
484
- } )
485
- . collect ( ) ;
486
- if expansions. is_empty ( ) {
487
- None
488
- } else {
489
- Some ( expansions)
490
- }
488
+ let res = ctx. attr_to_derive_macro_call ( item, src) ?;
489
+ Some ( res. to_vec ( ) )
491
490
} )
492
491
}
493
492
0 commit comments