@@ -11,7 +11,7 @@ use ra_ap_hir::{
11
11
} ;
12
12
use ra_ap_hir_def:: ModuleId ;
13
13
use ra_ap_hir_def:: type_ref:: Mutability ;
14
- use ra_ap_hir_expand:: { ExpandResult , ExpandTo } ;
14
+ use ra_ap_hir_expand:: { ExpandResult , ExpandTo , InFile } ;
15
15
use ra_ap_ide_db:: RootDatabase ;
16
16
use ra_ap_ide_db:: line_index:: { LineCol , LineIndex } ;
17
17
use ra_ap_parser:: SyntaxKind ;
@@ -23,7 +23,15 @@ use ra_ap_syntax::{
23
23
} ;
24
24
25
25
#[ macro_export]
26
- macro_rules! emit_detached {
26
+ macro_rules! pre_emit {
27
+ ( Item , $self: ident, $node: ident) => {
28
+ $self. setup_item_expansion( $node) ;
29
+ } ;
30
+ ( $( $_: tt) * ) => { } ;
31
+ }
32
+
33
+ #[ macro_export]
34
+ macro_rules! post_emit {
27
35
( MacroCall , $self: ident, $node: ident, $label: ident) => {
28
36
$self. extract_macro_call_expanded( $node, $label) ;
29
37
} ;
@@ -106,8 +114,9 @@ pub struct Translator<'a> {
106
114
line_index : LineIndex ,
107
115
file_id : Option < EditionedFileId > ,
108
116
pub semantics : Option < & ' a Semantics < ' a , RootDatabase > > ,
109
- resolve_paths : ResolvePaths ,
117
+ resolve_paths : bool ,
110
118
source_kind : SourceKind ,
119
+ macro_context_depth : usize ,
111
120
}
112
121
113
122
const UNKNOWN_LOCATION : ( LineCol , LineCol ) =
@@ -130,8 +139,9 @@ impl<'a> Translator<'a> {
130
139
line_index,
131
140
file_id : semantic_info. map ( |i| i. file_id ) ,
132
141
semantics : semantic_info. map ( |i| i. semantics ) ,
133
- resolve_paths,
142
+ resolve_paths : resolve_paths == ResolvePaths :: Yes ,
134
143
source_kind,
144
+ macro_context_depth : 0 ,
135
145
}
136
146
}
137
147
fn location ( & self , range : TextRange ) -> Option < ( LineCol , LineCol ) > {
@@ -337,6 +347,11 @@ impl<'a> Translator<'a> {
337
347
mcall : & ast:: MacroCall ,
338
348
label : Label < generated:: MacroCall > ,
339
349
) {
350
+ if self . macro_context_depth > 0 {
351
+ // we are in an attribute macro, don't emit anything: we would be failing to expand any
352
+ // way as from version 0.0.274 rust-analyser only expands in the context of an expansion
353
+ return ;
354
+ }
340
355
if let Some ( expanded) = self
341
356
. semantics
342
357
. as_ref ( )
@@ -537,7 +552,7 @@ impl<'a> Translator<'a> {
537
552
item : & T ,
538
553
label : Label < generated:: Addressable > ,
539
554
) {
540
- if self . resolve_paths == ResolvePaths :: No {
555
+ if ! self . resolve_paths {
541
556
return ;
542
557
}
543
558
( || {
@@ -560,7 +575,7 @@ impl<'a> Translator<'a> {
560
575
item : & ast:: Variant ,
561
576
label : Label < generated:: Variant > ,
562
577
) {
563
- if self . resolve_paths == ResolvePaths :: No {
578
+ if ! self . resolve_paths {
564
579
return ;
565
580
}
566
581
( || {
@@ -583,7 +598,7 @@ impl<'a> Translator<'a> {
583
598
item : & impl PathAst ,
584
599
label : Label < generated:: Resolvable > ,
585
600
) {
586
- if self . resolve_paths == ResolvePaths :: No {
601
+ if ! self . resolve_paths {
587
602
return ;
588
603
}
589
604
( || {
@@ -606,7 +621,7 @@ impl<'a> Translator<'a> {
606
621
item : & ast:: MethodCallExpr ,
607
622
label : Label < generated:: MethodCallExpr > ,
608
623
) {
609
- if self . resolve_paths == ResolvePaths :: No {
624
+ if ! self . resolve_paths {
610
625
return ;
611
626
}
612
627
( || {
@@ -708,13 +723,33 @@ impl<'a> Translator<'a> {
708
723
}
709
724
}
710
725
726
+ pub ( crate ) fn setup_item_expansion ( & mut self , node : & ast:: Item ) {
727
+ if self . semantics . is_some_and ( |s| {
728
+ let file = s. hir_file_for ( node. syntax ( ) ) ;
729
+ let node = InFile :: new ( file, node) ;
730
+ s. is_attr_macro_call ( node)
731
+ } ) {
732
+ self . macro_context_depth += 1 ;
733
+ }
734
+ }
735
+
711
736
pub ( crate ) fn emit_item_expansion ( & mut self , node : & ast:: Item , label : Label < generated:: Item > ) {
712
737
// TODO: remove this after fixing exponential expansion on libraries like funty-2.0.0
713
738
if self . source_kind == SourceKind :: Library {
714
739
return ;
715
740
}
716
741
( || {
717
742
let semantics = self . semantics ?;
743
+ let file = semantics. hir_file_for ( node. syntax ( ) ) ;
744
+ let infile_node = InFile :: new ( file, node) ;
745
+ if !semantics. is_attr_macro_call ( infile_node) {
746
+ return None ;
747
+ }
748
+ self . macro_context_depth -= 1 ;
749
+ if self . macro_context_depth > 0 {
750
+ // only expand the outermost attribute macro
751
+ return None ;
752
+ }
718
753
let ExpandResult {
719
754
value : expanded, ..
720
755
} = semantics. expand_attr_macro ( node) ?;
0 commit comments