Skip to content

Commit 3b1ad23

Browse files
committed
internal: Make def site span for proc-macro more invalidation resistant
1 parent c50c4f8 commit 3b1ad23

File tree

7 files changed

+38
-39
lines changed

7 files changed

+38
-39
lines changed

crates/hir-def/src/db.rs

-6
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
309309
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
310310
local_inner: false,
311311
allow_internal_unsafe: loc.allow_internal_unsafe,
312-
span: makro.def_site,
313312
edition: loc.edition,
314313
}
315314
}
@@ -325,7 +324,6 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
325324
allow_internal_unsafe: loc
326325
.flags
327326
.contains(MacroRulesLocFlags::ALLOW_INTERNAL_UNSAFE),
328-
span: makro.def_site,
329327
edition: loc.edition,
330328
}
331329
}
@@ -343,10 +341,6 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
343341
),
344342
local_inner: false,
345343
allow_internal_unsafe: false,
346-
// FIXME: This is wrong, this should point to the name
347-
span: db
348-
.span_map(loc.id.file_id())
349-
.span_for_range(db.ast_id_map(loc.id.file_id()).get(makro.ast_id).text_range()),
350344
edition: loc.edition,
351345
}
352346
}

crates/hir-def/src/item_tree.rs

-2
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,6 @@ pub struct MacroRules {
798798
/// The name of the declared macro.
799799
pub name: Name,
800800
pub ast_id: FileAstId<ast::MacroRules>,
801-
pub def_site: Span,
802801
}
803802

804803
/// "Macros 2.0" macro definition.
@@ -807,7 +806,6 @@ pub struct Macro2 {
807806
pub name: Name,
808807
pub visibility: RawVisibilityId,
809808
pub ast_id: FileAstId<ast::MacroDef>,
810-
pub def_site: Span,
811809
}
812810

813811
impl Use {

crates/hir-def/src/item_tree/lower.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -573,21 +573,19 @@ impl<'a> Ctx<'a> {
573573

574574
fn lower_macro_rules(&mut self, m: &ast::MacroRules) -> Option<FileItemTreeId<MacroRules>> {
575575
let name = m.name()?;
576-
let def_site = self.span_map().span_for_range(name.syntax().text_range());
577576
let ast_id = self.source_ast_id_map.ast_id(m);
578577

579-
let res = MacroRules { name: name.as_name(), ast_id, def_site };
578+
let res = MacroRules { name: name.as_name(), ast_id };
580579
Some(id(self.data().macro_rules.alloc(res)))
581580
}
582581

583582
fn lower_macro_def(&mut self, m: &ast::MacroDef) -> Option<FileItemTreeId<Macro2>> {
584583
let name = m.name()?;
585-
let def_site = self.span_map().span_for_range(name.syntax().text_range());
586584

587585
let ast_id = self.source_ast_id_map.ast_id(m);
588586
let visibility = self.lower_visibility(m);
589587

590-
let res = Macro2 { name: name.as_name(), ast_id, visibility, def_site };
588+
let res = Macro2 { name: name.as_name(), ast_id, visibility };
591589
Some(id(self.data().macro_defs.alloc(res)))
592590
}
593591

crates/hir-def/src/item_tree/pretty.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -498,23 +498,13 @@ impl Printer<'_> {
498498
wln!(self, "{}!(...);", path.display(self.db.upcast()));
499499
}
500500
ModItem::MacroRules(it) => {
501-
let MacroRules { name, ast_id, def_site } = &self.tree[it];
502-
let _ = writeln!(
503-
self,
504-
"// AstId: {:?}, Span: {}",
505-
ast_id.erase().into_raw(),
506-
def_site,
507-
);
501+
let MacroRules { name, ast_id } = &self.tree[it];
502+
self.print_ast_id(ast_id.erase());
508503
wln!(self, "macro_rules! {} {{ ... }}", name.display(self.db.upcast()));
509504
}
510505
ModItem::Macro2(it) => {
511-
let Macro2 { name, visibility, ast_id, def_site } = &self.tree[it];
512-
let _ = writeln!(
513-
self,
514-
"// AstId: {:?}, Span: {}",
515-
ast_id.erase().into_raw(),
516-
def_site,
517-
);
506+
let Macro2 { name, visibility, ast_id } = &self.tree[it];
507+
self.print_ast_id(ast_id.erase());
518508
self.print_visibility(*visibility);
519509
wln!(self, "macro {} {{ ... }}", name.display(self.db.upcast()));
520510
}

crates/hir-def/src/item_tree/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,10 @@ pub macro m2() {}
272272
m!();
273273
"#,
274274
expect![[r#"
275-
// AstId: 1, Span: 0:[email protected]#0
275+
// AstId: 1
276276
macro_rules! m { ... }
277277
278-
// AstId: 2, Span: 0:[email protected]#0
278+
// AstId: 2
279279
pub macro m2 { ... }
280280
281281
// AstId: 3, Span: 0:[email protected]#0, ExpandTo: Items

crates/hir-expand/src/db.rs

+30-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use either::Either;
55
use limit::Limit;
66
use mbe::{syntax_node_to_token_tree, ValueResult};
77
use rustc_hash::FxHashSet;
8-
use span::{AstIdMap, SyntaxContextData, SyntaxContextId};
8+
use span::{AstIdMap, Span, SyntaxContextData, SyntaxContextId};
99
use syntax::{ast, AstNode, Parse, SyntaxElement, SyntaxError, SyntaxNode, SyntaxToken, T};
1010
use 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+
613632
fn 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

crates/hir-expand/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ pub struct MacroDefId {
183183
pub kind: MacroDefKind,
184184
pub local_inner: bool,
185185
pub allow_internal_unsafe: bool,
186-
pub span: Span,
187186
}
188187

189188
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

0 commit comments

Comments
 (0)