Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7d762d1

Browse files
committed
Record macro def site spans
1 parent 51a9e78 commit 7d762d1

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

crates/hir-def/src/db.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
326326

327327
match id {
328328
MacroId::Macro2Id(it) => {
329-
let loc = it.lookup(db);
329+
let loc: Macro2Loc = it.lookup(db);
330330

331331
let item_tree = loc.id.item_tree(db);
332332
let makro = &item_tree[loc.id.value];
@@ -335,10 +335,13 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
335335
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
336336
local_inner: false,
337337
allow_internal_unsafe: loc.allow_internal_unsafe,
338+
def_site: 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()),
338341
}
339342
}
340343
MacroId::MacroRulesId(it) => {
341-
let loc = it.lookup(db);
344+
let loc: MacroRulesLoc = it.lookup(db);
342345

343346
let item_tree = loc.id.item_tree(db);
344347
let makro = &item_tree[loc.id.value];
@@ -347,6 +350,9 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
347350
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
348351
local_inner: loc.local_inner,
349352
allow_internal_unsafe: loc.allow_internal_unsafe,
353+
def_site: 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()),
350356
}
351357
}
352358
MacroId::ProcMacroId(it) => {
@@ -363,6 +369,9 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
363369
),
364370
local_inner: false,
365371
allow_internal_unsafe: false,
372+
def_site: 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()),
366375
}
367376
}
368377
}

crates/hir-expand/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub struct MacroDefId {
179179
pub kind: MacroDefKind,
180180
pub local_inner: bool,
181181
pub allow_internal_unsafe: bool,
182-
// pub def_site: Span,
182+
pub def_site: Span,
183183
}
184184

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

crates/hir-expand/src/span_map.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ impl mbe::SpanMapper<Span> for SpanMapRef<'_> {
3838
impl SpanMap {
3939
pub fn span_for_range(&self, range: TextRange) -> Span {
4040
match self {
41+
// FIXME: Is it correct for us to only take the span at the start? This feels somewhat
42+
// wrong. The context will be right, but the range could be considered wrong. See
43+
// https://github.com/rust-lang/rust/issues/23480, we probably want to fetch the span at
44+
// the start and end, then merge them like rustc does in `Span::to
4145
Self::ExpansionSpanMap(span_map) => span_map.span_at(range.start()),
4246
Self::RealSpanMap(span_map) => span_map.span_for_range(range),
4347
}

0 commit comments

Comments
 (0)