Skip to content

Commit 9ee71b4

Browse files
committed
Auto merge of #16175 - Veykril:dummy-spans, r=Veykril
fix: Correctly set and mark the proc-macro spans This slows down analysis by 2-3s on self for me unfortunately (~2.5% slowdown) Noisy diff due to two simple refactoring in the first 2 commits. Relevant changes are [7d762d1](7d762d1) and [1e1113c](1e1113c) which introduce def site spans and correct marking for proc-macros respectively.
2 parents 337e2ab + 1e1113c commit 9ee71b4

25 files changed

+333
-282
lines changed

crates/hir-def/src/attr.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,12 @@ impl<'attr> AttrQuery<'attr> {
637637
}
638638
}
639639

640-
fn any_has_attrs(
641-
db: &dyn DefDatabase,
642-
id: impl Lookup<Data = impl HasSource<Value = impl ast::HasAttrs>>,
640+
fn any_has_attrs<'db>(
641+
db: &(dyn DefDatabase + 'db),
642+
id: impl Lookup<
643+
Database<'db> = dyn DefDatabase + 'db,
644+
Data = impl HasSource<Value = impl ast::HasAttrs>,
645+
>,
643646
) -> InFile<ast::AnyHasAttrs> {
644647
id.lookup(db).source(db).map(ast::AnyHasAttrs::new)
645648
}
@@ -650,17 +653,17 @@ fn attrs_from_item_tree<N: ItemTreeNode>(db: &dyn DefDatabase, id: ItemTreeId<N>
650653
tree.raw_attrs(mod_item.into()).clone()
651654
}
652655

653-
fn attrs_from_item_tree_loc<N: ItemTreeNode>(
654-
db: &dyn DefDatabase,
655-
lookup: impl Lookup<Data = ItemLoc<N>>,
656+
fn attrs_from_item_tree_loc<'db, N: ItemTreeNode>(
657+
db: &(dyn DefDatabase + 'db),
658+
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>>,
656659
) -> RawAttrs {
657660
let id = lookup.lookup(db).id;
658661
attrs_from_item_tree(db, id)
659662
}
660663

661-
fn attrs_from_item_tree_assoc<N: ItemTreeNode>(
662-
db: &dyn DefDatabase,
663-
lookup: impl Lookup<Data = AssocItemLoc<N>>,
664+
fn attrs_from_item_tree_assoc<'db, N: ItemTreeNode>(
665+
db: &(dyn DefDatabase + 'db),
666+
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<N>>,
664667
) -> RawAttrs {
665668
let id = lookup.lookup(db).id;
666669
attrs_from_item_tree(db, id)

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -959,20 +959,29 @@ impl ExprCollector<'_> {
959959
// File containing the macro call. Expansion errors will be attached here.
960960
let outer_file = self.expander.current_file_id;
961961

962-
let macro_call_ptr = self.expander.to_source(AstPtr::new(&mcall));
962+
let macro_call_ptr = self.expander.to_source(syntax_ptr);
963963
let module = self.expander.module.local_id;
964-
let res = self.expander.enter_expand(self.db, mcall, |path| {
965-
self.def_map
966-
.resolve_path(
967-
self.db,
968-
module,
969-
&path,
970-
crate::item_scope::BuiltinShadowMode::Other,
971-
Some(MacroSubNs::Bang),
972-
)
973-
.0
974-
.take_macros()
975-
});
964+
965+
let res = match self.def_map.modules[module]
966+
.scope
967+
.macro_invocations
968+
.get(&InFile::new(outer_file, self.ast_id_map.ast_id_for_ptr(syntax_ptr)))
969+
{
970+
// fast path, macro call is in a block module
971+
Some(&call) => Ok(self.expander.enter_expand_id(self.db, call)),
972+
None => self.expander.enter_expand(self.db, mcall, |path| {
973+
self.def_map
974+
.resolve_path(
975+
self.db,
976+
module,
977+
&path,
978+
crate::item_scope::BuiltinShadowMode::Other,
979+
Some(MacroSubNs::Bang),
980+
)
981+
.0
982+
.take_macros()
983+
}),
984+
};
976985

977986
let res = match res {
978987
Ok(res) => res,
@@ -986,7 +995,6 @@ impl ExprCollector<'_> {
986995
return collector(self, None);
987996
}
988997
};
989-
990998
if record_diagnostics {
991999
match &res.err {
9921000
Some(ExpandError::UnresolvedProcMacro(krate)) => {

crates/hir-def/src/data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
db::DefDatabase,
1717
expander::{Expander, Mark},
1818
item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, MacroCall, ModItem, TreeId},
19-
macro_call_as_call_id, macro_id_to_def_id,
19+
macro_call_as_call_id,
2020
nameres::{
2121
attr_resolution::ResolvedAttr,
2222
diagnostics::DefDiagnostic,
@@ -720,7 +720,7 @@ impl<'a> AssocItemCollector<'a> {
720720
)
721721
.0
722722
.take_macros()
723-
.map(|it| macro_id_to_def_id(self.db, it))
723+
.map(|it| self.db.macro_def(it))
724724
};
725725
match macro_call_as_call_id(
726726
self.db.upcast(),

crates/hir-def/src/db.rs

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Defines database & queries for name resolution.
22
use base_db::{salsa, CrateId, SourceDatabase, Upcast};
33
use either::Either;
4-
use hir_expand::{db::ExpandDatabase, HirFileId};
4+
use hir_expand::{db::ExpandDatabase, HirFileId, MacroDefId};
55
use intern::Interned;
66
use la_arena::ArenaMap;
77
use syntax::{ast, AstPtr};
@@ -24,9 +24,9 @@ use crate::{
2424
AttrDefId, BlockId, BlockLoc, ConstBlockId, ConstBlockLoc, ConstId, ConstLoc, DefWithBodyId,
2525
EnumId, EnumLoc, ExternBlockId, ExternBlockLoc, ExternCrateId, ExternCrateLoc, FunctionId,
2626
FunctionLoc, GenericDefId, ImplId, ImplLoc, InTypeConstId, InTypeConstLoc, LocalEnumVariantId,
27-
LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId, MacroRulesLoc, ProcMacroId, ProcMacroLoc,
28-
StaticId, StaticLoc, StructId, StructLoc, TraitAliasId, TraitAliasLoc, TraitId, TraitLoc,
29-
TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc, VariantId,
27+
LocalFieldId, Macro2Id, Macro2Loc, MacroId, MacroRulesId, MacroRulesLoc, ProcMacroId,
28+
ProcMacroLoc, StaticId, StaticLoc, StructId, StructLoc, TraitAliasId, TraitAliasLoc, TraitId,
29+
TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc, VariantId,
3030
};
3131

3232
#[salsa::query_group(InternDatabaseStorage)]
@@ -110,6 +110,8 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
110110
#[salsa::invoke(DefMap::block_def_map_query)]
111111
fn block_def_map(&self, block: BlockId) -> Arc<DefMap>;
112112

113+
fn macro_def(&self, m: MacroId) -> MacroDefId;
114+
113115
// region:data
114116

115117
#[salsa::invoke(StructData::struct_data_query)]
@@ -305,3 +307,72 @@ fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: CrateId) -> bool {
305307

306308
false
307309
}
310+
311+
fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
312+
use hir_expand::InFile;
313+
314+
use crate::{Lookup, MacroDefKind, MacroExpander};
315+
316+
let kind = |expander, file_id, m| {
317+
let in_file = InFile::new(file_id, m);
318+
match expander {
319+
MacroExpander::Declarative => MacroDefKind::Declarative(in_file),
320+
MacroExpander::BuiltIn(it) => MacroDefKind::BuiltIn(it, in_file),
321+
MacroExpander::BuiltInAttr(it) => MacroDefKind::BuiltInAttr(it, in_file),
322+
MacroExpander::BuiltInDerive(it) => MacroDefKind::BuiltInDerive(it, in_file),
323+
MacroExpander::BuiltInEager(it) => MacroDefKind::BuiltInEager(it, in_file),
324+
}
325+
};
326+
327+
match id {
328+
MacroId::Macro2Id(it) => {
329+
let loc: Macro2Loc = it.lookup(db);
330+
331+
let item_tree = loc.id.item_tree(db);
332+
let makro = &item_tree[loc.id.value];
333+
MacroDefId {
334+
krate: loc.container.krate,
335+
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
336+
local_inner: false,
337+
allow_internal_unsafe: loc.allow_internal_unsafe,
338+
span: db
339+
.span_map(loc.id.file_id())
340+
.span_for_range(db.ast_id_map(loc.id.file_id()).get(makro.ast_id).text_range()),
341+
}
342+
}
343+
MacroId::MacroRulesId(it) => {
344+
let loc: MacroRulesLoc = it.lookup(db);
345+
346+
let item_tree = loc.id.item_tree(db);
347+
let makro = &item_tree[loc.id.value];
348+
MacroDefId {
349+
krate: loc.container.krate,
350+
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
351+
local_inner: loc.local_inner,
352+
allow_internal_unsafe: loc.allow_internal_unsafe,
353+
span: db
354+
.span_map(loc.id.file_id())
355+
.span_for_range(db.ast_id_map(loc.id.file_id()).get(makro.ast_id).text_range()),
356+
}
357+
}
358+
MacroId::ProcMacroId(it) => {
359+
let loc = it.lookup(db);
360+
361+
let item_tree = loc.id.item_tree(db);
362+
let makro = &item_tree[loc.id.value];
363+
MacroDefId {
364+
krate: loc.container.krate,
365+
kind: MacroDefKind::ProcMacro(
366+
loc.expander,
367+
loc.kind,
368+
InFile::new(loc.id.file_id(), makro.ast_id),
369+
),
370+
local_inner: false,
371+
allow_internal_unsafe: false,
372+
span: db
373+
.span_map(loc.id.file_id())
374+
.span_for_range(db.ast_id_map(loc.id.file_id()).get(makro.ast_id).text_range()),
375+
}
376+
}
377+
}
378+
}

crates/hir-def/src/expander.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use limit::Limit;
1111
use syntax::{ast, Parse, SyntaxNode};
1212

1313
use crate::{
14-
attr::Attrs, db::DefDatabase, lower::LowerCtx, macro_id_to_def_id, path::Path, AsMacroCall,
15-
MacroId, ModuleId, UnresolvedMacro,
14+
attr::Attrs, db::DefDatabase, lower::LowerCtx, path::Path, AsMacroCall, MacroId, ModuleId,
15+
UnresolvedMacro,
1616
};
1717

1818
#[derive(Debug)]
@@ -58,7 +58,7 @@ impl Expander {
5858
let result = self.within_limit(db, |this| {
5959
let macro_call = InFile::new(this.current_file_id, &macro_call);
6060
match macro_call.as_call_id_with_errors(db.upcast(), this.module.krate(), |path| {
61-
resolver(path).map(|it| macro_id_to_def_id(db, it))
61+
resolver(path).map(|it| db.macro_def(it))
6262
}) {
6363
Ok(call_id) => call_id,
6464
Err(resolve_err) => {

crates/hir-def/src/item_scope.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ pub struct ItemScope {
102102
// FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
103103
// be all resolved to the last one defined if shadowing happens.
104104
legacy_macros: FxHashMap<Name, SmallVec<[MacroId; 1]>>,
105-
/// The derive macro invocations in this scope.
105+
/// The attribute macro invocations in this scope.
106106
attr_macros: FxHashMap<AstId<ast::Item>, MacroCallId>,
107+
/// The macro invocations in this scope.
108+
pub macro_invocations: FxHashMap<AstId<ast::MacroCall>, MacroCallId>,
107109
/// The derive macro invocations in this scope, keyed by the owner item over the actual derive attributes
108110
/// paired with the derive macro invocations for the specific attribute.
109111
derive_macros: FxHashMap<AstId<ast::Adt>, SmallVec<[DeriveMacroInvocation; 1]>>,
@@ -345,6 +347,10 @@ impl ItemScope {
345347
self.attr_macros.insert(item, call);
346348
}
347349

350+
pub(crate) fn add_macro_invoc(&mut self, call: AstId<ast::MacroCall>, call_id: MacroCallId) {
351+
self.macro_invocations.insert(call, call_id);
352+
}
353+
348354
pub(crate) fn attr_macro_invocs(
349355
&self,
350356
) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
@@ -692,6 +698,7 @@ impl ItemScope {
692698
use_imports_values,
693699
use_imports_types,
694700
use_imports_macros,
701+
macro_invocations,
695702
} = self;
696703
types.shrink_to_fit();
697704
values.shrink_to_fit();
@@ -709,6 +716,7 @@ impl ItemScope {
709716
derive_macros.shrink_to_fit();
710717
extern_crate_decls.shrink_to_fit();
711718
use_decls.shrink_to_fit();
719+
macro_invocations.shrink_to_fit();
712720
}
713721
}
714722

crates/hir-def/src/item_tree.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
//!
3030
//! In general, any item in the `ItemTree` stores its `AstId`, which allows mapping it back to its
3131
//! surface syntax.
32+
//!
33+
//! Note that we cannot store [`span::Span`]s inside of this, as typing in an item invalidates its
34+
//! encompassing span!
3235
3336
mod lower;
3437
mod pretty;
@@ -281,7 +284,7 @@ struct ItemTreeData {
281284
mods: Arena<Mod>,
282285
macro_calls: Arena<MacroCall>,
283286
macro_rules: Arena<MacroRules>,
284-
macro_defs: Arena<MacroDef>,
287+
macro_defs: Arena<Macro2>,
285288

286289
vis: ItemVisibilities,
287290
}
@@ -514,7 +517,7 @@ mod_items! {
514517
Mod in mods -> ast::Module,
515518
MacroCall in macro_calls -> ast::MacroCall,
516519
MacroRules in macro_rules -> ast::MacroRules,
517-
MacroDef in macro_defs -> ast::MacroDef,
520+
Macro2 in macro_defs -> ast::MacroDef,
518521
}
519522

520523
macro_rules! impl_index {
@@ -747,6 +750,7 @@ pub struct MacroCall {
747750
pub path: Interned<ModPath>,
748751
pub ast_id: FileAstId<ast::MacroCall>,
749752
pub expand_to: ExpandTo,
753+
// FIXME: We need to move this out. It invalidates the item tree when typing inside the macro call.
750754
pub call_site: Span,
751755
}
752756

@@ -759,7 +763,7 @@ pub struct MacroRules {
759763

760764
/// "Macros 2.0" macro definition.
761765
#[derive(Debug, Clone, Eq, PartialEq)]
762-
pub struct MacroDef {
766+
pub struct Macro2 {
763767
pub name: Name,
764768
pub visibility: RawVisibilityId,
765769
pub ast_id: FileAstId<ast::MacroDef>,
@@ -918,7 +922,7 @@ impl ModItem {
918922
| ModItem::Impl(_)
919923
| ModItem::Mod(_)
920924
| ModItem::MacroRules(_)
921-
| ModItem::MacroDef(_) => None,
925+
| ModItem::Macro2(_) => None,
922926
ModItem::MacroCall(call) => Some(AssocItem::MacroCall(*call)),
923927
ModItem::Const(konst) => Some(AssocItem::Const(*konst)),
924928
ModItem::TypeAlias(alias) => Some(AssocItem::TypeAlias(*alias)),
@@ -944,7 +948,7 @@ impl ModItem {
944948
ModItem::Mod(it) => tree[it.index()].ast_id().upcast(),
945949
ModItem::MacroCall(it) => tree[it.index()].ast_id().upcast(),
946950
ModItem::MacroRules(it) => tree[it.index()].ast_id().upcast(),
947-
ModItem::MacroDef(it) => tree[it.index()].ast_id().upcast(),
951+
ModItem::Macro2(it) => tree[it.index()].ast_id().upcast(),
948952
}
949953
}
950954
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,13 @@ impl<'a> Ctx<'a> {
562562
Some(id(self.data().macro_rules.alloc(res)))
563563
}
564564

565-
fn lower_macro_def(&mut self, m: &ast::MacroDef) -> Option<FileItemTreeId<MacroDef>> {
565+
fn lower_macro_def(&mut self, m: &ast::MacroDef) -> Option<FileItemTreeId<Macro2>> {
566566
let name = m.name().map(|it| it.as_name())?;
567567

568568
let ast_id = self.source_ast_id_map.ast_id(m);
569569
let visibility = self.lower_visibility(m);
570570

571-
let res = MacroDef { name, ast_id, visibility };
571+
let res = Macro2 { name, ast_id, visibility };
572572
Some(id(self.data().macro_defs.alloc(res)))
573573
}
574574

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,8 @@ impl Printer<'_> {
464464
let MacroRules { name, ast_id: _ } = &self.tree[it];
465465
wln!(self, "macro_rules! {} {{ ... }}", name.display(self.db.upcast()));
466466
}
467-
ModItem::MacroDef(it) => {
468-
let MacroDef { name, visibility, ast_id: _ } = &self.tree[it];
467+
ModItem::Macro2(it) => {
468+
let Macro2 { name, visibility, ast_id: _ } = &self.tree[it];
469469
self.print_visibility(*visibility);
470470
wln!(self, "macro {} {{ ... }}", name.display(self.db.upcast()));
471471
}

0 commit comments

Comments
 (0)