Skip to content

Commit 2661c27

Browse files
committed
Auto merge of rust-lang#16461 - Veykril:expansion-info, r=Veykril
internal: Remove unnecessary usages of ExpansionInfo And some follow up simplifications to rust-lang/rust-analyzer#16439
2 parents e48bc04 + d252247 commit 2661c27

File tree

7 files changed

+175
-106
lines changed

7 files changed

+175
-106
lines changed

crates/hir-expand/src/db.rs

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ pub trait ExpandDatabase: SourceDatabase {
8080
#[salsa::invoke(SpanMap::new)]
8181
fn span_map(&self, file_id: HirFileId) -> SpanMap;
8282

83+
#[salsa::transparent]
84+
#[salsa::invoke(crate::span_map::expansion_span_map)]
85+
fn expansion_span_map(&self, file_id: MacroFileId) -> Arc<ExpansionSpanMap>;
8386
#[salsa::invoke(crate::span_map::real_span_map)]
8487
fn real_span_map(&self, file_id: FileId) -> Arc<RealSpanMap>;
8588

crates/hir-expand/src/files.rs

+28-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use either::Either;
55
use span::{FileId, FileRange, HirFileId, HirFileIdRepr, MacroFileId, SyntaxContextId};
66
use syntax::{AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize};
77

8-
use crate::{db, ExpansionInfo, MacroFileIdExt};
8+
use crate::{db, map_node_range_up, span_for_offset, MacroFileIdExt};
99

1010
/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
1111
///
@@ -147,7 +147,7 @@ impl InFile<&SyntaxNode> {
147147
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
148148
HirFileIdRepr::MacroFile(mac_file) => {
149149
if let Some((res, ctxt)) =
150-
ExpansionInfo::new(db, mac_file).map_node_range_up(db, self.value.text_range())
150+
map_node_range_up(db, &db.expansion_span_map(mac_file), self.value.text_range())
151151
{
152152
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
153153
// keep pre-token map rewrite behaviour.
@@ -163,12 +163,15 @@ impl InFile<&SyntaxNode> {
163163
}
164164

165165
/// Falls back to the macro call range if the node cannot be mapped up fully.
166-
pub fn original_file_range_full(self, db: &dyn db::ExpandDatabase) -> FileRange {
166+
pub fn original_file_range_with_macro_call_body(
167+
self,
168+
db: &dyn db::ExpandDatabase,
169+
) -> FileRange {
167170
match self.file_id.repr() {
168171
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
169172
HirFileIdRepr::MacroFile(mac_file) => {
170173
if let Some((res, ctxt)) =
171-
ExpansionInfo::new(db, mac_file).map_node_range_up(db, self.value.text_range())
174+
map_node_range_up(db, &db.expansion_span_map(mac_file), self.value.text_range())
172175
{
173176
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
174177
// keep pre-token map rewrite behaviour.
@@ -193,7 +196,7 @@ impl InFile<&SyntaxNode> {
193196
Some((FileRange { file_id, range: self.value.text_range() }, SyntaxContextId::ROOT))
194197
}
195198
HirFileIdRepr::MacroFile(mac_file) => {
196-
ExpansionInfo::new(db, mac_file).map_node_range_up(db, self.value.text_range())
199+
map_node_range_up(db, &db.expansion_span_map(mac_file), self.value.text_range())
197200
}
198201
}
199202
}
@@ -215,7 +218,7 @@ impl InFile<&SyntaxNode> {
215218
}
216219

217220
let (FileRange { file_id, range }, ctx) =
218-
ExpansionInfo::new(db, file_id).map_node_range_up(db, self.value.text_range())?;
221+
map_node_range_up(db, &db.expansion_span_map(file_id), self.value.text_range())?;
219222

220223
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
221224
// keep pre-token map rewrite behaviour.
@@ -246,8 +249,11 @@ impl InFile<SyntaxToken> {
246249
match self.file_id.repr() {
247250
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
248251
HirFileIdRepr::MacroFile(mac_file) => {
249-
let (range, ctxt) = ExpansionInfo::new(db, mac_file)
250-
.span_for_offset(db, self.value.text_range().start());
252+
let (range, ctxt) = span_for_offset(
253+
db,
254+
&db.expansion_span_map(mac_file),
255+
self.value.text_range().start(),
256+
);
251257

252258
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
253259
// keep pre-token map rewrite behaviour.
@@ -269,8 +275,11 @@ impl InFile<SyntaxToken> {
269275
Some(FileRange { file_id, range: self.value.text_range() })
270276
}
271277
HirFileIdRepr::MacroFile(mac_file) => {
272-
let (range, ctxt) = ExpansionInfo::new(db, mac_file)
273-
.span_for_offset(db, self.value.text_range().start());
278+
let (range, ctxt) = span_for_offset(
279+
db,
280+
&db.expansion_span_map(mac_file),
281+
self.value.text_range().start(),
282+
);
274283

275284
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
276285
// keep pre-token map rewrite behaviour.
@@ -286,7 +295,7 @@ impl InFile<SyntaxToken> {
286295

287296
impl InMacroFile<TextSize> {
288297
pub fn original_file_range(self, db: &dyn db::ExpandDatabase) -> (FileRange, SyntaxContextId) {
289-
ExpansionInfo::new(db, self.file_id).span_for_offset(db, self.value)
298+
span_for_offset(db, &db.expansion_span_map(self.file_id), self.value)
290299
}
291300
}
292301

@@ -300,7 +309,7 @@ impl InFile<TextRange> {
300309
(FileRange { file_id, range: self.value }, SyntaxContextId::ROOT)
301310
}
302311
HirFileIdRepr::MacroFile(mac_file) => {
303-
match ExpansionInfo::new(db, mac_file).map_node_range_up(db, self.value) {
312+
match map_node_range_up(db, &db.expansion_span_map(mac_file), self.value) {
304313
Some(it) => it,
305314
None => {
306315
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
@@ -315,7 +324,7 @@ impl InFile<TextRange> {
315324
match self.file_id.repr() {
316325
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value },
317326
HirFileIdRepr::MacroFile(mac_file) => {
318-
match ExpansionInfo::new(db, mac_file).map_node_range_up(db, self.value) {
327+
match map_node_range_up(db, &db.expansion_span_map(mac_file), self.value) {
319328
Some((it, SyntaxContextId::ROOT)) => it,
320329
_ => {
321330
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
@@ -335,7 +344,7 @@ impl InFile<TextRange> {
335344
Some((FileRange { file_id, range: self.value }, SyntaxContextId::ROOT))
336345
}
337346
HirFileIdRepr::MacroFile(mac_file) => {
338-
ExpansionInfo::new(db, mac_file).map_node_range_up(db, self.value)
347+
map_node_range_up(db, &db.expansion_span_map(mac_file), self.value)
339348
}
340349
}
341350
}
@@ -355,8 +364,11 @@ impl<N: AstNode> InFile<N> {
355364
return None;
356365
}
357366

358-
let (FileRange { file_id, range }, ctx) = ExpansionInfo::new(db, file_id)
359-
.map_node_range_up(db, self.value.syntax().text_range())?;
367+
let (FileRange { file_id, range }, ctx) = map_node_range_up(
368+
db,
369+
&db.expansion_span_map(file_id),
370+
self.value.syntax().text_range(),
371+
)?;
360372

361373
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
362374
// keep pre-token map rewrite behaviour.

crates/hir-expand/src/lib.rs

+47-28
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,10 @@ impl ExpansionInfo {
677677
Some(self.arg.with_value(self.arg.value.as_ref()?.parent()?))
678678
}
679679

680+
pub fn call_file(&self) -> HirFileId {
681+
self.arg.file_id
682+
}
683+
680684
/// Maps the passed in file range down into a macro expansion if it is the input to a macro call.
681685
pub fn map_range_down(
682686
&self,
@@ -697,13 +701,7 @@ impl ExpansionInfo {
697701
offset: TextSize,
698702
) -> (FileRange, SyntaxContextId) {
699703
debug_assert!(self.expanded.value.text_range().contains(offset));
700-
let span = self.exp_map.span_at(offset);
701-
let anchor_offset = db
702-
.ast_id_map(span.anchor.file_id.into())
703-
.get_erased(span.anchor.ast_id)
704-
.text_range()
705-
.start();
706-
(FileRange { file_id: span.anchor.file_id, range: span.range + anchor_offset }, span.ctx)
704+
span_for_offset(db, &self.exp_map, offset)
707705
}
708706

709707
/// Maps up the text range out of the expansion hierarchy back into the original file its from.
@@ -713,27 +711,7 @@ impl ExpansionInfo {
713711
range: TextRange,
714712
) -> Option<(FileRange, SyntaxContextId)> {
715713
debug_assert!(self.expanded.value.text_range().contains_range(range));
716-
let mut spans = self.exp_map.spans_for_range(range);
717-
let Span { range, anchor, ctx } = spans.next()?;
718-
let mut start = range.start();
719-
let mut end = range.end();
720-
721-
for span in spans {
722-
if span.anchor != anchor || span.ctx != ctx {
723-
return None;
724-
}
725-
start = start.min(span.range.start());
726-
end = end.max(span.range.end());
727-
}
728-
let anchor_offset =
729-
db.ast_id_map(anchor.file_id.into()).get_erased(anchor.ast_id).text_range().start();
730-
Some((
731-
FileRange {
732-
file_id: anchor.file_id,
733-
range: TextRange::new(start, end) + anchor_offset,
734-
},
735-
ctx,
736-
))
714+
map_node_range_up(db, &self.exp_map, range)
737715
}
738716

739717
/// Maps up the text range out of the expansion into is macro call.
@@ -822,6 +800,47 @@ impl ExpansionInfo {
822800
}
823801
}
824802

803+
/// Maps up the text range out of the expansion hierarchy back into the original file its from.
804+
pub fn map_node_range_up(
805+
db: &dyn ExpandDatabase,
806+
exp_map: &ExpansionSpanMap,
807+
range: TextRange,
808+
) -> Option<(FileRange, SyntaxContextId)> {
809+
let mut spans = exp_map.spans_for_range(range);
810+
let Span { range, anchor, ctx } = spans.next()?;
811+
let mut start = range.start();
812+
let mut end = range.end();
813+
814+
for span in spans {
815+
if span.anchor != anchor || span.ctx != ctx {
816+
return None;
817+
}
818+
start = start.min(span.range.start());
819+
end = end.max(span.range.end());
820+
}
821+
let anchor_offset =
822+
db.ast_id_map(anchor.file_id.into()).get_erased(anchor.ast_id).text_range().start();
823+
Some((
824+
FileRange { file_id: anchor.file_id, range: TextRange::new(start, end) + anchor_offset },
825+
ctx,
826+
))
827+
}
828+
829+
/// Looks up the span at the given offset.
830+
pub fn span_for_offset(
831+
db: &dyn ExpandDatabase,
832+
exp_map: &ExpansionSpanMap,
833+
offset: TextSize,
834+
) -> (FileRange, SyntaxContextId) {
835+
let span = exp_map.span_at(offset);
836+
let anchor_offset = db
837+
.ast_id_map(span.anchor.file_id.into())
838+
.get_erased(span.anchor.ast_id)
839+
.text_range()
840+
.start();
841+
(FileRange { file_id: span.anchor.file_id, range: span.range + anchor_offset }, span.ctx)
842+
}
843+
825844
/// In Rust, macros expand token trees to token trees. When we want to turn a
826845
/// token tree into an AST node, we need to figure out what kind of AST node we
827846
/// want: something like `foo` can be a type, an expression, or a pattern.

crates/hir-expand/src/span_map.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Span maps for real files and macro expansions.
2-
use span::{FileId, HirFileId, HirFileIdRepr, Span};
2+
use span::{FileId, HirFileId, HirFileIdRepr, MacroFileId, Span};
33
use syntax::{AstNode, TextRange};
44
use triomphe::Arc;
55

@@ -94,3 +94,10 @@ pub(crate) fn real_span_map(db: &dyn ExpandDatabase, file_id: FileId) -> Arc<Rea
9494
tree.syntax().text_range().end(),
9595
))
9696
}
97+
98+
pub(crate) fn expansion_span_map(
99+
db: &dyn ExpandDatabase,
100+
file_id: MacroFileId,
101+
) -> Arc<ExpansionSpanMap> {
102+
db.parse_macro_expansion(file_id).value.1
103+
}

0 commit comments

Comments
 (0)