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

Commit 634d588

Browse files
committed
Simplify
1 parent 9cb13b6 commit 634d588

File tree

13 files changed

+136
-175
lines changed

13 files changed

+136
-175
lines changed

crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::{iter, ops::Range, sync};
1818

1919
use base_db::{fixture::WithFixture, span::SpanData, ProcMacro, SourceDatabase};
2020
use expect_test::Expect;
21-
use hir_expand::{db::ExpandDatabase, span::SpanMapRef, HirFileIdExt, InFile, MacroFileId};
21+
use hir_expand::{db::ExpandDatabase, span::SpanMapRef, InFile, MacroFileId, MacroFileIdExt};
2222
use stdx::format_to;
2323
use syntax::{
2424
ast::{self, edit::IndentLevel},
@@ -172,35 +172,41 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
172172
};
173173

174174
if let Some(src) = src {
175-
if src.file_id.is_attr_macro(&db) || src.file_id.is_custom_derive(&db) {
176-
let call = src.file_id.call_node(&db).expect("macro file");
177-
let mut show_spans = false;
178-
let mut show_ctxt = false;
179-
for comment in call.value.children_with_tokens().filter(|it| it.kind() == COMMENT) {
180-
show_spans |= comment.to_string().contains("+spans");
181-
show_ctxt |= comment.to_string().contains("+syntaxctxt");
175+
if let Some(file_id) = src.file_id.macro_file() {
176+
if file_id.is_attr_macro(&db) || file_id.is_custom_derive(&db) {
177+
let call = file_id.call_node(&db);
178+
let mut show_spans = false;
179+
let mut show_ctxt = false;
180+
for comment in
181+
call.value.children_with_tokens().filter(|it| it.kind() == COMMENT)
182+
{
183+
show_spans |= comment.to_string().contains("+spans");
184+
show_ctxt |= comment.to_string().contains("+syntaxctxt");
185+
}
186+
let pp = pretty_print_macro_expansion(
187+
src.value,
188+
db.span_map(src.file_id).as_ref(),
189+
show_spans,
190+
show_ctxt,
191+
);
192+
format_to!(expanded_text, "\n{}", pp)
182193
}
183-
let pp = pretty_print_macro_expansion(
184-
src.value,
185-
db.span_map(src.file_id).as_ref(),
186-
show_spans,
187-
show_ctxt,
188-
);
189-
format_to!(expanded_text, "\n{}", pp)
190194
}
191195
}
192196
}
193197

194198
for impl_id in def_map[local_id].scope.impls() {
195199
let src = impl_id.lookup(&db).source(&db);
196-
if src.file_id.is_builtin_derive(&db) {
197-
let pp = pretty_print_macro_expansion(
198-
src.value.syntax().clone(),
199-
db.span_map(src.file_id).as_ref(),
200-
false,
201-
false,
202-
);
203-
format_to!(expanded_text, "\n{}", pp)
200+
if let Some(macro_file) = src.file_id.macro_file() {
201+
if macro_file.is_builtin_derive(&db) {
202+
let pp = pretty_print_macro_expansion(
203+
src.value.syntax().clone(),
204+
db.span_map(macro_file.into()).as_ref(),
205+
false,
206+
false,
207+
);
208+
format_to!(expanded_text, "\n{}", pp)
209+
}
204210
}
205211
}
206212

crates/hir-def/src/nameres/mod_resolution.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This module resolves `mod foo;` declaration to file.
22
use arrayvec::ArrayVec;
33
use base_db::{AnchoredPath, FileId};
4-
use hir_expand::{name::Name, HirFileIdExt};
4+
use hir_expand::{name::Name, HirFileIdExt, MacroFileIdExt};
55
use limit::Limit;
66
use syntax::SmolStr;
77

@@ -73,7 +73,7 @@ impl ModDir {
7373
Some(attr_path) => {
7474
candidate_files.push(self.dir_path.join_attr(attr_path, self.root_non_dir_owner))
7575
}
76-
None if file_id.is_include_macro(db.upcast()) => {
76+
None if file_id.macro_file().map_or(false, |it| it.is_include_macro(db.upcast())) => {
7777
candidate_files.push(format!("{}.rs", name.display(db.upcast())));
7878
candidate_files.push(format!("{}/mod.rs", name.display(db.upcast())));
7979
}

crates/hir-expand/src/files.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use base_db::{
66
FileId, FileRange,
77
};
88
use either::Either;
9-
use syntax::{AstNode, SyntaxNode, SyntaxToken, TextRange};
9+
use syntax::{AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize};
1010

11-
use crate::{db, ExpansionInfo, HirFileIdExt as _};
11+
use crate::{db, ExpansionInfo, MacroFileIdExt};
1212

1313
/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
1414
///
@@ -119,16 +119,6 @@ impl<FileId: Copy, N: AstNode> InFileWrapper<FileId, N> {
119119
// region:specific impls
120120

121121
impl InFile<&SyntaxNode> {
122-
pub fn ancestors_with_macros(
123-
self,
124-
db: &dyn db::ExpandDatabase,
125-
) -> impl Iterator<Item = InFile<SyntaxNode>> + Clone + '_ {
126-
iter::successors(Some(self.cloned()), move |node| match node.value.parent() {
127-
Some(parent) => Some(node.with_value(parent)),
128-
None => node.file_id.call_node(db),
129-
})
130-
}
131-
132122
/// Skips the attributed item that caused the macro invocation we are climbing up
133123
pub fn ancestors_with_macros_skip_attr_item(
134124
self,
@@ -137,8 +127,9 @@ impl InFile<&SyntaxNode> {
137127
let succ = move |node: &InFile<SyntaxNode>| match node.value.parent() {
138128
Some(parent) => Some(node.with_value(parent)),
139129
None => {
140-
let parent_node = node.file_id.call_node(db)?;
141-
if node.file_id.is_attr_macro(db) {
130+
let macro_file_id = node.file_id.macro_file()?;
131+
let parent_node = macro_file_id.call_node(db);
132+
if macro_file_id.is_attr_macro(db) {
142133
// macro call was an attributed item, skip it
143134
// FIXME: does this fail if this is a direct expansion of another macro?
144135
parent_node.map(|node| node.parent()).transpose()
@@ -222,7 +213,7 @@ impl InFile<&SyntaxNode> {
222213
}
223214
HirFileIdRepr::MacroFile(m) => m,
224215
};
225-
if !self.file_id.is_attr_macro(db) {
216+
if !file_id.is_attr_macro(db) {
226217
return None;
227218
}
228219

@@ -243,21 +234,23 @@ impl InFile<&SyntaxNode> {
243234
}
244235
}
245236

246-
impl InFile<SyntaxToken> {
237+
impl InMacroFile<SyntaxToken> {
247238
pub fn upmap_once(
248239
self,
249240
db: &dyn db::ExpandDatabase,
250-
) -> Option<InFile<smallvec::SmallVec<[TextRange; 1]>>> {
251-
Some(self.file_id.expansion_info(db)?.map_range_up_once(db, self.value.text_range()))
241+
) -> InFile<smallvec::SmallVec<[TextRange; 1]>> {
242+
self.file_id.expansion_info(db).map_range_up_once(db, self.value.text_range())
252243
}
244+
}
253245

246+
impl InFile<SyntaxToken> {
254247
/// Falls back to the macro call range if the node cannot be mapped up fully.
255248
pub fn original_file_range(self, db: &dyn db::ExpandDatabase) -> FileRange {
256249
match self.file_id.repr() {
257250
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
258251
HirFileIdRepr::MacroFile(mac_file) => {
259252
let (range, ctxt) = ExpansionInfo::new(db, mac_file)
260-
.map_token_range_up(db, self.value.text_range());
253+
.span_for_offset(db, self.value.text_range().start());
261254

262255
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
263256
// keep pre-token map rewrite behaviour.
@@ -280,7 +273,7 @@ impl InFile<SyntaxToken> {
280273
}
281274
HirFileIdRepr::MacroFile(mac_file) => {
282275
let (range, ctxt) = ExpansionInfo::new(db, mac_file)
283-
.map_token_range_up(db, self.value.text_range());
276+
.span_for_offset(db, self.value.text_range().start());
284277

285278
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
286279
// keep pre-token map rewrite behaviour.
@@ -294,20 +287,13 @@ impl InFile<SyntaxToken> {
294287
}
295288
}
296289

297-
impl InFile<TextRange> {
298-
/// Attempts to map the syntax node back up its macro calls.
299-
pub fn original_file_range(self, db: &dyn db::ExpandDatabase) -> FileRange {
300-
let (range, _ctxt) = match self.file_id.repr() {
301-
HirFileIdRepr::FileId(file_id) => {
302-
(FileRange { file_id, range: self.value }, SyntaxContextId::ROOT)
303-
}
304-
HirFileIdRepr::MacroFile(m) => {
305-
ExpansionInfo::new(db, m).map_token_range_up(db, self.value)
306-
}
307-
};
308-
range
290+
impl InMacroFile<TextSize> {
291+
pub fn original_file_range(self, db: &dyn db::ExpandDatabase) -> (FileRange, SyntaxContextId) {
292+
ExpansionInfo::new(db, self.file_id).span_for_offset(db, self.value)
309293
}
294+
}
310295

296+
impl InFile<TextRange> {
311297
pub fn original_node_file_range(
312298
self,
313299
db: &dyn db::ExpandDatabase,
@@ -353,7 +339,7 @@ impl<N: AstNode> InFile<N> {
353339
}
354340
HirFileIdRepr::MacroFile(m) => m,
355341
};
356-
if !self.file_id.is_attr_macro(db) {
342+
if !file_id.is_attr_macro(db) {
357343
return None;
358344
}
359345

0 commit comments

Comments
 (0)