Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correctly set and mark the proc-macro spans #16175

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions crates/hir-def/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {

match id {
MacroId::Macro2Id(it) => {
let loc = it.lookup(db);
let loc: Macro2Loc = it.lookup(db);

let item_tree = loc.id.item_tree(db);
let makro = &item_tree[loc.id.value];
Expand All @@ -335,10 +335,13 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
local_inner: false,
allow_internal_unsafe: loc.allow_internal_unsafe,
def_site: db
.span_map(loc.id.file_id())
.span_for_range(db.ast_id_map(loc.id.file_id()).get(makro.ast_id).text_range()),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slow down comes from this, which I assume misses the cache for the span maps in some occurrences unfortunately

}
}
MacroId::MacroRulesId(it) => {
let loc = it.lookup(db);
let loc: MacroRulesLoc = it.lookup(db);

let item_tree = loc.id.item_tree(db);
let makro = &item_tree[loc.id.value];
Expand All @@ -347,6 +350,9 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
local_inner: loc.local_inner,
allow_internal_unsafe: loc.allow_internal_unsafe,
def_site: db
.span_map(loc.id.file_id())
.span_for_range(db.ast_id_map(loc.id.file_id()).get(makro.ast_id).text_range()),
}
}
MacroId::ProcMacroId(it) => {
Expand All @@ -363,6 +369,9 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
),
local_inner: false,
allow_internal_unsafe: false,
def_site: db
.span_map(loc.id.file_id())
.span_for_range(db.ast_id_map(loc.id.file_id()).get(makro.ast_id).text_range()),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub struct MacroDefId {
pub kind: MacroDefKind,
pub local_inner: bool,
pub allow_internal_unsafe: bool,
// pub def_site: Span,
pub def_site: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
4 changes: 4 additions & 0 deletions crates/hir-expand/src/span_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ impl mbe::SpanMapper<Span> for SpanMapRef<'_> {
impl SpanMap {
pub fn span_for_range(&self, range: TextRange) -> Span {
match self {
// FIXME: Is it correct for us to only take the span at the start? This feels somewhat
// wrong. The context will be right, but the range could be considered wrong. See
// https://github.com/rust-lang/rust/issues/23480, we probably want to fetch the span at
// the start and end, then merge them like rustc does in `Span::to
Self::ExpansionSpanMap(span_map) => span_map.span_at(range.start()),
Self::RealSpanMap(span_map) => span_map.span_for_range(range),
}
Expand Down