@@ -566,66 +566,52 @@ impl ExprCollector<'_> {
566
566
syntax_ptr : AstPtr < ast:: Expr > ,
567
567
mut collector : F ,
568
568
) {
569
- if let Some ( name) = e. is_macro_rules ( ) . map ( |it| it. as_name ( ) ) {
570
- let mac = MacroDefId {
571
- krate : Some ( self . expander . module . krate ) ,
572
- ast_id : Some ( self . expander . ast_id ( & e) ) ,
573
- kind : MacroDefKind :: Declarative ,
574
- local_inner : false ,
575
- } ;
576
- self . body . item_scope . define_legacy_macro ( name, mac) ;
569
+ // File containing the macro call. Expansion errors will be attached here.
570
+ let outer_file = self . expander . current_file_id ;
577
571
578
- // FIXME: do we still need to allocate this as missing ?
579
- collector ( self , None ) ;
580
- } else {
581
- // File containing the macro call. Expansion errors will be attached here.
582
- let outer_file = self . expander . current_file_id ;
583
-
584
- let macro_call = self . expander . to_source ( AstPtr :: new ( & e) ) ;
585
- let res = self . expander . enter_expand ( self . db , Some ( & self . body . item_scope ) , e) ;
586
-
587
- match & res. err {
588
- Some ( ExpandError :: UnresolvedProcMacro ) => {
589
- self . source_map . diagnostics . push ( BodyDiagnostic :: UnresolvedProcMacro (
590
- UnresolvedProcMacro {
591
- file : outer_file,
592
- node : syntax_ptr. into ( ) ,
593
- precise_location : None ,
594
- macro_name : None ,
595
- } ,
596
- ) ) ;
597
- }
598
- Some ( err) => {
599
- self . source_map . diagnostics . push ( BodyDiagnostic :: MacroError ( MacroError {
572
+ let macro_call = self . expander . to_source ( AstPtr :: new ( & e) ) ;
573
+ let res = self . expander . enter_expand ( self . db , Some ( & self . body . item_scope ) , e) ;
574
+
575
+ match & res. err {
576
+ Some ( ExpandError :: UnresolvedProcMacro ) => {
577
+ self . source_map . diagnostics . push ( BodyDiagnostic :: UnresolvedProcMacro (
578
+ UnresolvedProcMacro {
600
579
file : outer_file,
601
580
node : syntax_ptr. into ( ) ,
602
- message : err. to_string ( ) ,
603
- } ) ) ;
604
- }
605
- None => { }
581
+ precise_location : None ,
582
+ macro_name : None ,
583
+ } ,
584
+ ) ) ;
585
+ }
586
+ Some ( err) => {
587
+ self . source_map . diagnostics . push ( BodyDiagnostic :: MacroError ( MacroError {
588
+ file : outer_file,
589
+ node : syntax_ptr. into ( ) ,
590
+ message : err. to_string ( ) ,
591
+ } ) ) ;
606
592
}
593
+ None => { }
594
+ }
607
595
608
- match res. value {
609
- Some ( ( mark, expansion) ) => {
610
- // FIXME: Statements are too complicated to recover from error for now.
611
- // It is because we don't have any hygenine for local variable expansion right now.
612
- if T :: can_cast ( syntax:: SyntaxKind :: MACRO_STMTS ) && res. err . is_some ( ) {
613
- self . expander . exit ( self . db , mark) ;
614
- collector ( self , None ) ;
615
- } else {
616
- self . source_map
617
- . expansions
618
- . insert ( macro_call, self . expander . current_file_id ) ;
596
+ match res. value {
597
+ Some ( ( mark, expansion) ) => {
598
+ // FIXME: Statements are too complicated to recover from error for now.
599
+ // It is because we don't have any hygenine for local variable expansion right now.
600
+ if T :: can_cast ( syntax:: SyntaxKind :: MACRO_STMTS ) && res. err . is_some ( ) {
601
+ self . expander . exit ( self . db , mark) ;
602
+ collector ( self , None ) ;
603
+ } else {
604
+ self . source_map . expansions . insert ( macro_call, self . expander . current_file_id ) ;
619
605
620
- let item_tree = self . db . item_tree ( self . expander . current_file_id ) ;
621
- self . item_trees . insert ( self . expander . current_file_id , item_tree) ;
606
+ let item_tree = self . db . item_tree ( self . expander . current_file_id ) ;
607
+ self . item_trees . insert ( self . expander . current_file_id , item_tree) ;
622
608
623
- collector ( self , Some ( expansion) ) ;
624
- self . expander . exit ( self . db , mark) ;
625
- }
609
+ let id = collector ( self , Some ( expansion) ) ;
610
+ self . expander . exit ( self . db , mark) ;
611
+ id
626
612
}
627
- None => collector ( self , None ) ,
628
613
}
614
+ None => collector ( self , None ) ,
629
615
}
630
616
}
631
617
@@ -785,26 +771,44 @@ impl ExprCollector<'_> {
785
771
| ast:: Item :: ExternCrate ( _)
786
772
| ast:: Item :: Module ( _)
787
773
| ast:: Item :: MacroCall ( _) => return None ,
774
+ ast:: Item :: MacroRules ( def) => {
775
+ return Some ( Either :: Right ( def) ) ;
776
+ }
788
777
} ;
789
778
790
- Some ( ( def, name) )
779
+ Some ( Either :: Left ( ( def, name) ) )
791
780
} )
792
781
. collect :: < Vec < _ > > ( ) ;
793
782
794
- for ( def, name) in items {
795
- self . body . item_scope . define_def ( def) ;
796
- if let Some ( name) = name {
797
- let vis = crate :: visibility:: Visibility :: Public ; // FIXME determine correctly
798
- let has_constructor = match def {
799
- ModuleDefId :: AdtId ( AdtId :: StructId ( s) ) => {
800
- self . db . struct_data ( s) . variant_data . kind ( ) != StructKind :: Record
783
+ for either in items {
784
+ match either {
785
+ Either :: Left ( ( def, name) ) => {
786
+ self . body . item_scope . define_def ( def) ;
787
+ if let Some ( name) = name {
788
+ let vis = crate :: visibility:: Visibility :: Public ; // FIXME determine correctly
789
+ let has_constructor = match def {
790
+ ModuleDefId :: AdtId ( AdtId :: StructId ( s) ) => {
791
+ self . db . struct_data ( s) . variant_data . kind ( ) != StructKind :: Record
792
+ }
793
+ _ => true ,
794
+ } ;
795
+ self . body . item_scope . push_res (
796
+ name. as_name ( ) ,
797
+ crate :: per_ns:: PerNs :: from_def ( def, vis, has_constructor) ,
798
+ ) ;
801
799
}
802
- _ => true ,
803
- } ;
804
- self . body . item_scope . push_res (
805
- name. as_name ( ) ,
806
- crate :: per_ns:: PerNs :: from_def ( def, vis, has_constructor) ,
807
- ) ;
800
+ }
801
+ Either :: Right ( e) => {
802
+ let mac = MacroDefId {
803
+ krate : Some ( self . expander . module . krate ) ,
804
+ ast_id : Some ( self . expander . ast_id ( & e) ) ,
805
+ kind : MacroDefKind :: Declarative ,
806
+ local_inner : false ,
807
+ } ;
808
+ if let Some ( name) = e. name ( ) {
809
+ self . body . item_scope . define_legacy_macro ( name. as_name ( ) , mac) ;
810
+ }
811
+ }
808
812
}
809
813
}
810
814
}
0 commit comments