@@ -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
} ;
@@ -101,7 +109,8 @@ pub struct Translator<'a> {
101
109
line_index : LineIndex ,
102
110
file_id : Option < EditionedFileId > ,
103
111
pub semantics : Option < & ' a Semantics < ' a , RootDatabase > > ,
104
- resolve_paths : ResolvePaths ,
112
+ resolve_paths : bool ,
113
+ macro_context_depth : usize ,
105
114
}
106
115
107
116
const UNKNOWN_LOCATION : ( LineCol , LineCol ) =
@@ -123,7 +132,8 @@ impl<'a> Translator<'a> {
123
132
line_index,
124
133
file_id : semantic_info. map ( |i| i. file_id ) ,
125
134
semantics : semantic_info. map ( |i| i. semantics ) ,
126
- resolve_paths,
135
+ resolve_paths : resolve_paths == ResolvePaths :: Yes ,
136
+ macro_context_depth : 0 ,
127
137
}
128
138
}
129
139
fn location ( & self , range : TextRange ) -> Option < ( LineCol , LineCol ) > {
@@ -321,6 +331,11 @@ impl<'a> Translator<'a> {
321
331
mcall : & ast:: MacroCall ,
322
332
label : Label < generated:: MacroCall > ,
323
333
) {
334
+ if self . macro_context_depth > 0 {
335
+ // we are in an attribute macro, don't emit anything: we would be failing to expand any
336
+ // way as rust-analyser now only expands in the context of an expansion
337
+ return ;
338
+ }
324
339
if let Some ( expanded) = self
325
340
. semantics
326
341
. as_ref ( )
@@ -521,7 +536,7 @@ impl<'a> Translator<'a> {
521
536
item : & T ,
522
537
label : Label < generated:: Addressable > ,
523
538
) {
524
- if self . resolve_paths == ResolvePaths :: No {
539
+ if ! self . resolve_paths {
525
540
return ;
526
541
}
527
542
( || {
@@ -544,7 +559,7 @@ impl<'a> Translator<'a> {
544
559
item : & ast:: Variant ,
545
560
label : Label < generated:: Variant > ,
546
561
) {
547
- if self . resolve_paths == ResolvePaths :: No {
562
+ if ! self . resolve_paths {
548
563
return ;
549
564
}
550
565
( || {
@@ -567,7 +582,7 @@ impl<'a> Translator<'a> {
567
582
item : & impl PathAst ,
568
583
label : Label < generated:: Resolvable > ,
569
584
) {
570
- if self . resolve_paths == ResolvePaths :: No {
585
+ if ! self . resolve_paths {
571
586
return ;
572
587
}
573
588
( || {
@@ -590,7 +605,7 @@ impl<'a> Translator<'a> {
590
605
item : & ast:: MethodCallExpr ,
591
606
label : Label < generated:: MethodCallExpr > ,
592
607
) {
593
- if self . resolve_paths == ResolvePaths :: No {
608
+ if ! self . resolve_paths {
594
609
return ;
595
610
}
596
611
( || {
@@ -653,9 +668,29 @@ impl<'a> Translator<'a> {
653
668
}
654
669
}
655
670
671
+ pub ( crate ) fn setup_item_expansion ( & mut self , node : & ast:: Item ) {
672
+ if self . semantics . is_some_and ( |s| {
673
+ let file = s. hir_file_for ( node. syntax ( ) ) ;
674
+ let node = InFile :: new ( file, node) ;
675
+ s. is_attr_macro_call ( node)
676
+ } ) {
677
+ self . macro_context_depth += 1 ;
678
+ }
679
+ }
680
+
656
681
pub ( crate ) fn emit_item_expansion ( & mut self , node : & ast:: Item , label : Label < generated:: Item > ) {
657
682
( || {
658
683
let semantics = self . semantics ?;
684
+ let file = semantics. hir_file_for ( node. syntax ( ) ) ;
685
+ let infile_node = InFile :: new ( file, node) ;
686
+ if !semantics. is_attr_macro_call ( infile_node) {
687
+ return None ;
688
+ }
689
+ self . macro_context_depth -= 1 ;
690
+ if self . macro_context_depth > 0 {
691
+ // only expand the outermost attribute macro
692
+ return None ;
693
+ }
659
694
let ExpandResult {
660
695
value : expanded, ..
661
696
} = semantics. expand_attr_macro ( node) ?;
0 commit comments