@@ -24,33 +24,31 @@ use ra_ap_syntax::{
2424
2525impl Emission < ast:: Item > for Translator < ' _ > {
2626 fn pre_emit ( & mut self , node : & ast:: Item ) -> Option < Label < generated:: Item > > {
27- self . prepare_item_expansion ( node) . map ( Into :: into)
27+ self . item_pre_emit ( node) . map ( Into :: into)
2828 }
2929
3030 fn post_emit ( & mut self , node : & ast:: Item , label : Label < generated:: Item > ) {
31- self . emit_item_expansion ( node, label) ;
31+ self . item_post_emit ( node, label) ;
3232 }
3333}
3434
3535impl Emission < ast:: AssocItem > for Translator < ' _ > {
3636 fn pre_emit ( & mut self , node : & ast:: AssocItem ) -> Option < Label < generated:: AssocItem > > {
37- self . prepare_item_expansion ( & node. clone ( ) . into ( ) )
38- . map ( Into :: into)
37+ self . item_pre_emit ( & node. clone ( ) . into ( ) ) . map ( Into :: into)
3938 }
4039
4140 fn post_emit ( & mut self , node : & ast:: AssocItem , label : Label < generated:: AssocItem > ) {
42- self . emit_item_expansion ( & node. clone ( ) . into ( ) , label. into ( ) ) ;
41+ self . item_post_emit ( & node. clone ( ) . into ( ) , label. into ( ) ) ;
4342 }
4443}
4544
4645impl Emission < ast:: ExternItem > for Translator < ' _ > {
4746 fn pre_emit ( & mut self , node : & ast:: ExternItem ) -> Option < Label < generated:: ExternItem > > {
48- self . prepare_item_expansion ( & node. clone ( ) . into ( ) )
49- . map ( Into :: into)
47+ self . item_pre_emit ( & node. clone ( ) . into ( ) ) . map ( Into :: into)
5048 }
5149
5250 fn post_emit ( & mut self , node : & ast:: ExternItem , label : Label < generated:: ExternItem > ) {
53- self . emit_item_expansion ( & node. clone ( ) . into ( ) , label. into ( ) ) ;
51+ self . item_post_emit ( & node. clone ( ) . into ( ) , label. into ( ) ) ;
5452 }
5553}
5654
@@ -849,35 +847,6 @@ impl<'a> Translator<'a> {
849847 } )
850848 }
851849
852- pub ( crate ) fn prepare_item_expansion (
853- & mut self ,
854- node : & ast:: Item ,
855- ) -> Option < Label < generated:: MacroCall > > {
856- if self . source_kind == SourceKind :: Library {
857- // if the item expands via an attribute macro, we want to only emit the expansion
858- if let Some ( expanded) = self . emit_attribute_macro_expansion ( node) {
859- // we wrap it in a dummy MacroCall to get a single Item label that can replace
860- // the original Item
861- let label = self . trap . emit ( generated:: MacroCall {
862- id : TrapId :: Star ,
863- attrs : vec ! [ ] ,
864- path : None ,
865- token_tree : None ,
866- } ) ;
867- generated:: MacroCall :: emit_macro_call_expansion (
868- label,
869- expanded. into ( ) ,
870- & mut self . trap . writer ,
871- ) ;
872- return Some ( label) ;
873- }
874- }
875- if self . is_attribute_macro_target ( node) {
876- self . macro_context_depth += 1 ;
877- }
878- None
879- }
880-
881850 fn process_item_macro_expansion (
882851 & mut self ,
883852 node : & impl ast:: AstNode ,
@@ -915,10 +884,6 @@ impl<'a> Translator<'a> {
915884 & mut self ,
916885 node : & ast:: Item ,
917886 ) -> Option < Label < generated:: MacroItems > > {
918- if !self . is_attribute_macro_target ( node) {
919- return None ;
920- }
921- self . macro_context_depth -= 1 ;
922887 if self . macro_context_depth > 0 {
923888 // only expand the outermost attribute macro
924889 return None ;
@@ -927,7 +892,49 @@ impl<'a> Translator<'a> {
927892 self . process_item_macro_expansion ( node, expansion. map ( |x| x. value ) )
928893 }
929894
930- pub ( crate ) fn emit_item_expansion ( & mut self , node : & ast:: Item , label : Label < generated:: Item > ) {
895+ pub ( crate ) fn item_pre_emit (
896+ & mut self ,
897+ node : & ast:: Item ,
898+ ) -> Option < Label < generated:: MacroCall > > {
899+ if !self . is_attribute_macro_target ( node) {
900+ return None ;
901+ }
902+ if self . source_kind == SourceKind :: Library {
903+ // if the item expands via an attribute macro, we want to only emit the expansion
904+ if let Some ( expanded) = self . emit_attribute_macro_expansion ( node) {
905+ // we wrap it in a dummy MacroCall to get a single Item label that can replace
906+ // the original Item
907+ let label = self . trap . emit ( generated:: MacroCall {
908+ id : TrapId :: Star ,
909+ attrs : vec ! [ ] ,
910+ path : None ,
911+ token_tree : None ,
912+ } ) ;
913+ generated:: Item :: emit_attribute_macro_expansion (
914+ label. into ( ) ,
915+ expanded,
916+ & mut self . trap . writer ,
917+ ) ;
918+ self . emit_location ( label, node) ;
919+ return Some ( label) ;
920+ }
921+ }
922+ self . macro_context_depth += 1 ;
923+ None
924+ }
925+
926+ pub ( crate ) fn item_post_emit ( & mut self , node : & ast:: Item , label : Label < generated:: Item > ) {
927+ if !self . is_attribute_macro_target ( node) {
928+ return ;
929+ }
930+ // see `item_pre_emit`:
931+ // if self.is_attribute_macro_target(node), then we either exited early with `Some(label)`
932+ // and are not here, or we did self.macro_context_depth += 1
933+ assert ! (
934+ self . macro_context_depth > 0 ,
935+ "macro_context_depth should be > 0 for an attribute macro target"
936+ ) ;
937+ self . macro_context_depth -= 1 ;
931938 if let Some ( expanded) = self . emit_attribute_macro_expansion ( node) {
932939 generated:: Item :: emit_attribute_macro_expansion ( label, expanded, & mut self . trap . writer ) ;
933940 }
0 commit comments