@@ -5,7 +5,7 @@ use either::Either;
5
5
use limit:: Limit ;
6
6
use mbe:: { syntax_node_to_token_tree, ValueResult } ;
7
7
use rustc_hash:: FxHashSet ;
8
- use span:: { AstIdMap , SyntaxContextData , SyntaxContextId } ;
8
+ use span:: { AstIdMap , Span , SyntaxContextData , SyntaxContextId } ;
9
9
use syntax:: { ast, AstNode , Parse , SyntaxElement , SyntaxError , SyntaxNode , SyntaxToken , T } ;
10
10
use triomphe:: Arc ;
11
11
@@ -118,6 +118,12 @@ pub trait ExpandDatabase: SourceDatabase {
118
118
/// non-determinism breaks salsa in a very, very, very bad way.
119
119
/// @edwin0cheng heroically debugged this once! See #4315 for details
120
120
fn expand_proc_macro ( & self , call : MacroCallId ) -> ExpandResult < Arc < tt:: Subtree > > ;
121
+ /// Retrieves the span to be used for a proc-macro expansions spans.
122
+ /// This is a firewall query as it requires parsing the file, which we don't want proc-macros to
123
+ /// directly depend on as that would cause to frequent invalidations, mainly because of the
124
+ /// parse queries being LRU cached. If they weren't the invalidations would only happen if the
125
+ /// user wrote in the file that defines the proc-macro.
126
+ fn proc_macro_span ( & self , fun : AstId < ast:: Fn > ) -> Span ;
121
127
/// Firewall query that returns the errors from the `parse_macro_expansion` query.
122
128
fn parse_macro_expansion_error (
123
129
& self ,
@@ -137,6 +143,7 @@ pub fn expand_speculative(
137
143
) -> Option < ( SyntaxNode , SyntaxToken ) > {
138
144
let loc = db. lookup_intern_macro_call ( actual_macro_call) ;
139
145
146
+ // FIXME: This BOGUS here is dangerous once the proc-macro server can call back into the database!
140
147
let span_map = RealSpanMap :: absolute ( FileId :: BOGUS ) ;
141
148
let span_map = SpanMapRef :: RealSpanMap ( & span_map) ;
142
149
@@ -211,17 +218,18 @@ pub fn expand_speculative(
211
218
// Do the actual expansion, we need to directly expand the proc macro due to the attribute args
212
219
// Otherwise the expand query will fetch the non speculative attribute args and pass those instead.
213
220
let mut speculative_expansion = match loc. def . kind {
214
- MacroDefKind :: ProcMacro ( expander, .. ) => {
221
+ MacroDefKind :: ProcMacro ( expander, _ , ast ) => {
215
222
tt. delimiter = tt:: Delimiter :: invisible_spanned ( loc. call_site ) ;
223
+ let span = db. proc_macro_span ( ast) ;
216
224
expander. expand (
217
225
db,
218
226
loc. def . krate ,
219
227
loc. krate ,
220
228
& tt,
221
229
attr_arg. as_ref ( ) ,
222
- span_with_def_site_ctxt ( db, loc . def . span , actual_macro_call) ,
223
- span_with_call_site_ctxt ( db, loc . def . span , actual_macro_call) ,
224
- span_with_mixed_site_ctxt ( db, loc . def . span , actual_macro_call) ,
230
+ span_with_def_site_ctxt ( db, span, actual_macro_call) ,
231
+ span_with_call_site_ctxt ( db, span, actual_macro_call) ,
232
+ span_with_mixed_site_ctxt ( db, span, actual_macro_call) ,
225
233
)
226
234
}
227
235
MacroDefKind :: BuiltInAttr ( BuiltinAttrExpander :: Derive , _) => {
@@ -610,12 +618,23 @@ fn macro_expand(
610
618
ExpandResult { value : CowArc :: Owned ( tt) , err }
611
619
}
612
620
621
+ fn proc_macro_span ( db : & dyn ExpandDatabase , ast : AstId < ast:: Fn > ) -> Span {
622
+ let root = db. parse_or_expand ( ast. file_id ) ;
623
+ let ast_id_map = & db. ast_id_map ( ast. file_id ) ;
624
+ let span_map = & db. span_map ( ast. file_id ) ;
625
+
626
+ let node = ast_id_map. get ( ast. value ) . to_node ( & root) ;
627
+ let range = ast:: HasName :: name ( & node)
628
+ . map_or_else ( || node. syntax ( ) . text_range ( ) , |name| name. syntax ( ) . text_range ( ) ) ;
629
+ span_map. span_for_range ( range)
630
+ }
631
+
613
632
fn expand_proc_macro ( db : & dyn ExpandDatabase , id : MacroCallId ) -> ExpandResult < Arc < tt:: Subtree > > {
614
633
let loc = db. lookup_intern_macro_call ( id) ;
615
634
let ( macro_arg, undo_info) = db. macro_arg ( id) . value ;
616
635
617
- let expander = match loc. def . kind {
618
- MacroDefKind :: ProcMacro ( expander, .. ) => expander,
636
+ let ( expander, ast ) = match loc. def . kind {
637
+ MacroDefKind :: ProcMacro ( expander, _ , ast ) => ( expander, ast ) ,
619
638
_ => unreachable ! ( ) ,
620
639
} ;
621
640
@@ -624,15 +643,16 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
624
643
_ => None ,
625
644
} ;
626
645
646
+ let span = db. proc_macro_span ( ast) ;
627
647
let ExpandResult { value : mut tt, err } = expander. expand (
628
648
db,
629
649
loc. def . krate ,
630
650
loc. krate ,
631
651
& macro_arg,
632
652
attr_arg,
633
- span_with_def_site_ctxt ( db, loc . def . span , id) ,
634
- span_with_call_site_ctxt ( db, loc . def . span , id) ,
635
- span_with_mixed_site_ctxt ( db, loc . def . span , id) ,
653
+ span_with_def_site_ctxt ( db, span, id) ,
654
+ span_with_call_site_ctxt ( db, span, id) ,
655
+ span_with_mixed_site_ctxt ( db, span, id) ,
636
656
) ;
637
657
638
658
// Set a hard limit for the expanded tt
0 commit comments