@@ -20,7 +20,7 @@ use ra_ap_syntax::{
2020use ra_ap_syntax_bridge:: {
2121 DocCommentDesugarMode , syntax_node_to_token_tree, token_tree_to_syntax_node,
2222} ;
23- use std:: collections:: HashMap ;
23+ use std:: collections:: { HashMap , HashSet } ;
2424use std:: path:: { Path , PathBuf } ;
2525
2626impl Emission < ast:: Item > for Translator < ' _ > {
@@ -139,6 +139,11 @@ pub struct Translator<'db> {
139139 /// synthesized syntax tree. Those nodes are not registered in the semantics cache, so
140140 /// locations are resolved through this map instead of `Semantics::original_range`.
141141 builtin_derive_span_map : Option < ExpansionSpanMap > ,
142+ /// Nesting depth of macro expansions being emitted. At depth 0 nodes belong to the file
143+ /// being extracted, so their text range needs no mapping through `Semantics::original_range`.
144+ expansion_depth : usize ,
145+ /// Attribute macro targets entered in `item_pre_emit`, to be exited in `item_post_emit`.
146+ attribute_macro_targets : HashSet < SyntaxNode > ,
142147}
143148
144149const UNKNOWN_LOCATION : ( LineCol , LineCol ) =
@@ -167,6 +172,8 @@ impl<'db> Translator<'db> {
167172 parenthesized_expr_attrs : HashMap :: new ( ) ,
168173 diagnostic_count : 0 ,
169174 builtin_derive_span_map : None ,
175+ expansion_depth : 0 ,
176+ attribute_macro_targets : HashSet :: new ( ) ,
170177 }
171178 }
172179 fn location ( & self , range : TextRange ) -> Option < ( LineCol , LineCol ) > {
@@ -199,7 +206,9 @@ impl<'db> Translator<'db> {
199206 map_node_range_up_rooted ( semantics. db , span_map, node. syntax ( ) . text_range ( ) ) ?;
200207 return ( file_id == file_range. file_id ) . then_some ( file_range. range ) ;
201208 }
202- if let Some ( semantics) = self . semantics . as_ref ( ) {
209+ if let Some ( semantics) = self . semantics . as_ref ( )
210+ && self . expansion_depth > 0
211+ {
203212 let file_range = semantics. original_range ( node. syntax ( ) ) ;
204213 let file_id = self . file_id ?;
205214 ( file_id == file_range. file_id ) . then_some ( file_range. range )
@@ -394,6 +403,13 @@ impl<'db> Translator<'db> {
394403 }
395404 }
396405
406+ fn in_expansion < T > ( & mut self , f : impl FnOnce ( & mut Self ) -> T ) -> T {
407+ self . expansion_depth += 1 ;
408+ let ret = f ( self ) ;
409+ self . expansion_depth -= 1 ;
410+ ret
411+ }
412+
397413 fn emit_expanded_as (
398414 & mut self ,
399415 expand_to : ExpandTo ,
@@ -441,7 +457,9 @@ impl<'db> Translator<'db> {
441457 self . emit_macro_expansion_parse_errors ( mcall, & expanded. value ) ;
442458 let expand_to = ra_ap_hir_expand:: ExpandTo :: from_call_site ( mcall) ;
443459 let kind = expanded. kind ( ) ;
444- if let Some ( value) = self . emit_expanded_as ( expand_to, expanded. value ) {
460+ if let Some ( value) =
461+ self . in_expansion ( |this| this. emit_expanded_as ( expand_to, expanded. value ) )
462+ {
445463 generated:: MacroCall :: emit_macro_call_expansion (
446464 label,
447465 value,
@@ -661,6 +679,8 @@ impl<'db> Translator<'db> {
661679 // process that differently (in `extract_macro_call_expanded`)
662680 self . builtin_derive_span_map . is_none ( )
663681 && !matches ! ( node, ast:: Item :: MacroCall ( _) )
682+ // cheap syntactic pre-check: attribute macros need an attribute
683+ && ast:: attrs_including_inner ( node) . next ( ) . is_some ( )
664684 && self . semantics . is_some_and ( |semantics| {
665685 let file = semantics. hir_file_for ( node. syntax ( ) ) ;
666686 let node = InFile :: new ( file, node) ;
@@ -686,7 +706,7 @@ impl<'db> Translator<'db> {
686706 ) ;
687707 }
688708 if let Some ( items) = ast:: MacroItems :: cast ( value) {
689- self . emit_macro_items ( & items)
709+ self . in_expansion ( |this| this . emit_macro_items ( & items) )
690710 } else {
691711 let message =
692712 "attribute or derive macro expansion cannot be cast to MacroItems" . to_owned ( ) ;
@@ -741,20 +761,15 @@ impl<'db> Translator<'db> {
741761 }
742762 }
743763 self . macro_context_depth += 1 ;
764+ self . attribute_macro_targets . insert ( node. syntax ( ) . clone ( ) ) ;
744765 None
745766 }
746767
747768 pub ( crate ) fn item_post_emit ( & mut self , node : & ast:: Item , label : Label < generated:: Item > ) {
748- if !self . is_attribute_macro_target ( node) {
769+ // `item_pre_emit` records the targets for which it incremented `macro_context_depth`
770+ if !self . attribute_macro_targets . remove ( node. syntax ( ) ) {
749771 return ;
750772 }
751- // see `item_pre_emit`:
752- // if self.is_attribute_macro_target(node), then we either exited early with `Some(label)`
753- // and are not here, or we did self.macro_context_depth += 1
754- assert ! (
755- self . macro_context_depth > 0 ,
756- "macro_context_depth should be > 0 for an attribute macro target"
757- ) ;
758773 self . macro_context_depth -= 1 ;
759774 if let Some ( expanded) = self . emit_attribute_macro_expansion ( node) {
760775 generated:: Item :: emit_attribute_macro_expansion ( label, expanded, & mut self . trap . writer ) ;
0 commit comments