@@ -5,7 +5,7 @@ use either::Either;
55use limit:: Limit ;
66use mbe:: { syntax_node_to_token_tree, ValueResult } ;
77use rustc_hash:: FxHashSet ;
8- use span:: { AstIdMap , SyntaxContextData , SyntaxContextId } ;
8+ use span:: { AstIdMap , Span , SyntaxContextData , SyntaxContextId } ;
99use syntax:: { ast, AstNode , Parse , SyntaxElement , SyntaxError , SyntaxNode , SyntaxToken , T } ;
1010use triomphe:: Arc ;
1111
@@ -118,6 +118,12 @@ pub trait ExpandDatabase: SourceDatabase {
118118 /// non-determinism breaks salsa in a very, very, very bad way.
119119 /// @edwin0cheng heroically debugged this once! See #4315 for details
120120 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 ;
121127 /// Firewall query that returns the errors from the `parse_macro_expansion` query.
122128 fn parse_macro_expansion_error (
123129 & self ,
@@ -137,6 +143,7 @@ pub fn expand_speculative(
137143) -> Option < ( SyntaxNode , SyntaxToken ) > {
138144 let loc = db. lookup_intern_macro_call ( actual_macro_call) ;
139145
146+ // FIXME: This BOGUS here is dangerous once the proc-macro server can call back into the database!
140147 let span_map = RealSpanMap :: absolute ( FileId :: BOGUS ) ;
141148 let span_map = SpanMapRef :: RealSpanMap ( & span_map) ;
142149
@@ -211,17 +218,18 @@ pub fn expand_speculative(
211218 // Do the actual expansion, we need to directly expand the proc macro due to the attribute args
212219 // Otherwise the expand query will fetch the non speculative attribute args and pass those instead.
213220 let mut speculative_expansion = match loc. def . kind {
214- MacroDefKind :: ProcMacro ( expander, .. ) => {
221+ MacroDefKind :: ProcMacro ( expander, _ , ast ) => {
215222 tt. delimiter = tt:: Delimiter :: invisible_spanned ( loc. call_site ) ;
223+ let span = db. proc_macro_span ( ast) ;
216224 expander. expand (
217225 db,
218226 loc. def . krate ,
219227 loc. krate ,
220228 & tt,
221229 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) ,
225233 )
226234 }
227235 MacroDefKind :: BuiltInAttr ( BuiltinAttrExpander :: Derive , _) => {
@@ -610,12 +618,23 @@ fn macro_expand(
610618 ExpandResult { value : CowArc :: Owned ( tt) , err }
611619}
612620
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+
613632fn expand_proc_macro ( db : & dyn ExpandDatabase , id : MacroCallId ) -> ExpandResult < Arc < tt:: Subtree > > {
614633 let loc = db. lookup_intern_macro_call ( id) ;
615634 let ( macro_arg, undo_info) = db. macro_arg ( id) . value ;
616635
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 ) ,
619638 _ => unreachable ! ( ) ,
620639 } ;
621640
@@ -624,15 +643,16 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
624643 _ => None ,
625644 } ;
626645
646+ let span = db. proc_macro_span ( ast) ;
627647 let ExpandResult { value : mut tt, err } = expander. expand (
628648 db,
629649 loc. def . krate ,
630650 loc. krate ,
631651 & macro_arg,
632652 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) ,
636656 ) ;
637657
638658 // Set a hard limit for the expanded tt
0 commit comments