Skip to content

Commit aec29d9

Browse files
committed
🧹
1 parent 93ec7cd commit aec29d9

File tree

26 files changed

+102
-123
lines changed

26 files changed

+102
-123
lines changed

‎crates/hir-def/src/attr/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn assert_parse_result(input: &str, expected: DocExpr) {
1313
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
1414
let tt = syntax_node_to_token_tree(
1515
tt.syntax(),
16-
SpanMapRef::RealSpanMap(&RealSpanMap::empty(FileId(0))),
16+
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId(0))),
1717
);
1818
let cfg = DocExpr::parse(&tt);
1919
assert_eq!(cfg, expected);

‎crates/hir-def/src/expander.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl Expander {
131131

132132
pub(crate) fn parse_path(&mut self, db: &dyn DefDatabase, path: ast::Path) -> Option<Path> {
133133
let ctx = LowerCtx::new(db, self.span_map.clone(), self.current_file_id);
134-
Path::from_src(path, &ctx)
134+
Path::from_src(&ctx, path)
135135
}
136136

137137
fn within_limit<F, T: ast::AstNode>(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'a> LowerCtx<'a> {
4040
}
4141

4242
pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
43-
Path::from_src(ast, self)
43+
Path::from_src(self, ast)
4444
}
4545

4646
pub(crate) fn ast_id<N: AstIdNode>(&self, item: &N) -> Option<AstId<N>> {

‎crates/hir-def/src/nameres/tests/incremental.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ fn check_def_map_is_not_recomputed(ra_fixture_initial: &str, ra_fixture_change:
1111
let (mut db, pos) = TestDB::with_position(ra_fixture_initial);
1212
let krate = {
1313
let crate_graph = db.crate_graph();
14+
// Some of these tests use minicore/proc-macros which will be injected as the first crate
1415
crate_graph.iter().last().unwrap()
1516
};
1617
{

‎crates/hir-def/src/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ pub enum GenericArg {
9696
impl Path {
9797
/// Converts an `ast::Path` to `Path`. Works with use trees.
9898
/// It correctly handles `$crate` based path from macro call.
99-
pub fn from_src(path: ast::Path, ctx: &LowerCtx<'_>) -> Option<Path> {
100-
lower::lower_path(path, ctx)
99+
pub fn from_src(ctx: &LowerCtx<'_>, path: ast::Path) -> Option<Path> {
100+
lower::lower_path(ctx, path)
101101
}
102102

103103
/// Converts a known mod path to `Path`.

‎crates/hir-def/src/path/lower.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ use crate::{
1616
type_ref::{LifetimeRef, TypeBound, TypeRef},
1717
};
1818

19-
// fn resolve_crate_root
20-
2119
/// Converts an `ast::Path` to `Path`. Works with use trees.
2220
/// It correctly handles `$crate` based path from macro call.
23-
// FIXME: flip the params
24-
pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx<'_>) -> Option<Path> {
21+
pub(super) fn lower_path(ctx: &LowerCtx<'_>, mut path: ast::Path) -> Option<Path> {
2522
let mut kind = PathKind::Plain;
2623
let mut type_anchor = None;
2724
let mut segments = Vec::new();
@@ -36,18 +33,15 @@ pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx<'_>) -> Option<Path
3633

3734
match segment.kind()? {
3835
ast::PathSegmentKind::Name(name_ref) => {
39-
let name = if name_ref.text() == "$crate" {
40-
kind = resolve_crate_root(
36+
if name_ref.text() == "$crate" {
37+
break kind = resolve_crate_root(
4138
ctx.db.upcast(),
4239
span_map.span_for_range(name_ref.syntax().text_range()).ctx,
4340
)
4441
.map(PathKind::DollarCrate)
4542
.unwrap_or(PathKind::Crate);
46-
47-
break;
48-
} else {
49-
name_ref.as_name()
50-
};
43+
}
44+
let name = name_ref.as_name();
5145
let args = segment
5246
.generic_arg_list()
5347
.and_then(|it| lower_generic_args(ctx, it))
@@ -82,7 +76,7 @@ pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx<'_>) -> Option<Path
8276
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
8377
Some(trait_ref) => {
8478
let Path::Normal { mod_path, generic_args: path_generic_args, .. } =
85-
Path::from_src(trait_ref.path()?, ctx)?
79+
Path::from_src(ctx, trait_ref.path()?)?
8680
else {
8781
return None;
8882
};

‎crates/hir-expand/src/ast_id_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use profile::Count;
1717
use rustc_hash::FxHasher;
1818
use syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr};
1919

20-
pub use base_db::span::ErasedFileAstId;
21-
2220
use crate::db;
2321

22+
pub use base_db::span::ErasedFileAstId;
23+
2424
/// `AstId` points to an AST node in any file.
2525
///
2626
/// It is stable across reparses, and can be used as salsa key/value.

‎crates/hir-expand/src/attrs.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,8 @@ impl Attr {
293293
if tts.is_empty() {
294294
return None;
295295
}
296-
// FIXME: Absolutely wrong
297-
let call_site = match tts.first().unwrap() {
298-
tt::TokenTree::Leaf(l) => l.span().ctx,
299-
tt::TokenTree::Subtree(s) => s.delimiter.open.ctx,
300-
};
301296
// FIXME: This is necessarily a hack. It'd be nice if we could avoid allocation
302-
// here.
297+
// here or maybe just parse a mod path from a token tree directly
303298
let subtree = tt::Subtree {
304299
delimiter: tt::Delimiter::unspecified(),
305300
token_trees: tts.to_vec(),
@@ -313,6 +308,7 @@ impl Attr {
313308
return None;
314309
}
315310
let path = meta.path()?;
311+
let call_site = span_map.span_for_range(path.syntax().text_range()).ctx;
316312
Some((
317313
ModPath::from_src(db, path, SpanMapRef::ExpansionSpanMap(&span_map))?,
318314
call_site,

‎crates/hir-expand/src/builtin_fn_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ pub(crate) fn include_arg_to_tt(
553553
let Some(EagerCallInfo { arg, arg_id, .. }) = loc.eager.as_deref() else {
554554
panic!("include_arg_to_tt called on non include macro call: {:?}", &loc.eager);
555555
};
556-
let path = parse_string(&arg.0)?;
556+
let path = parse_string(&arg)?;
557557
let file_id = relative_file(db, *arg_id, &path, false)?;
558558

559559
// why are we not going through a SyntaxNode here?

‎crates/hir-expand/src/db.rs

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,16 @@ pub fn expand_speculative(
190190
speculative_args: &SyntaxNode,
191191
token_to_map: SyntaxToken,
192192
) -> Option<(SyntaxNode, SyntaxToken)> {
193-
// FIXME spanmaps
194193
let loc = db.lookup_intern_macro_call(actual_macro_call);
195194

196195
// Build the subtree and token mapping for the speculative args
197196
let _censor = censor_for_macro_input(&loc, speculative_args);
197+
let span_map = RealSpanMap::absolute(SpanAnchor::DUMMY.file_id);
198+
let span_map = SpanMapRef::RealSpanMap(&span_map);
198199
let mut tt = mbe::syntax_node_to_token_tree(
199200
speculative_args,
200201
// we don't leak these spans into any query so its fine to make them absolute
201-
SpanMapRef::RealSpanMap(&RealSpanMap::empty(SpanAnchor::DUMMY.file_id)),
202+
span_map,
202203
);
203204

204205
let attr_arg = match loc.kind {
@@ -216,10 +217,7 @@ pub fn expand_speculative(
216217
}?;
217218
match attr.token_tree() {
218219
Some(token_tree) => {
219-
let mut tree = syntax_node_to_token_tree(
220-
token_tree.syntax(),
221-
SpanMapRef::RealSpanMap(&RealSpanMap::empty(SpanAnchor::DUMMY.file_id)),
222-
);
220+
let mut tree = syntax_node_to_token_tree(token_tree.syntax(), span_map);
223221
tree.delimiter = tt::Delimiter::UNSPECIFIED;
224222

225223
Some(tree)
@@ -243,12 +241,7 @@ pub fn expand_speculative(
243241
MacroDefKind::BuiltInDerive(expander, ..) => {
244242
// this cast is a bit sus, can we avoid losing the typedness here?
245243
let adt = ast::Adt::cast(speculative_args.clone()).unwrap();
246-
expander.expand(
247-
db,
248-
actual_macro_call,
249-
&adt,
250-
SpanMapRef::RealSpanMap(&RealSpanMap::empty(SpanAnchor::DUMMY.file_id)),
251-
)
244+
expander.expand(db, actual_macro_call, &adt, span_map)
252245
}
253246
MacroDefKind::Declarative(it) => {
254247
db.decl_macro_expander(loc.krate, it).expand_unhygienic(tt)
@@ -305,6 +298,8 @@ fn parse_or_expand_with_err(
305298
}
306299
}
307300

301+
// FIXME: We should verify that the parsed node is one of the many macro node variants we expect
302+
// instead of having it be untyped
308303
fn parse_macro_expansion(
309304
db: &dyn ExpandDatabase,
310305
macro_file: MacroFileId,
@@ -330,6 +325,18 @@ fn parse_macro_expansion_error(
330325
.map(|it| it.0.errors().to_vec().into_boxed_slice())
331326
}
332327

328+
fn parse_with_map(db: &dyn ExpandDatabase, file_id: HirFileId) -> (Parse<SyntaxNode>, SpanMap) {
329+
match file_id.repr() {
330+
HirFileIdRepr::FileId(file_id) => {
331+
(db.parse(file_id).to_syntax(), SpanMap::RealSpanMap(db.real_span_map(file_id)))
332+
}
333+
HirFileIdRepr::MacroFile(macro_file) => {
334+
let (parse, map) = db.parse_macro_expansion(macro_file).value;
335+
(parse, SpanMap::ExpansionSpanMap(map))
336+
}
337+
}
338+
}
339+
333340
fn macro_arg(
334341
db: &dyn ExpandDatabase,
335342
id: MacroCallId,
@@ -361,32 +368,22 @@ fn macro_arg(
361368
.then(|| loc.eager.as_deref())
362369
.flatten()
363370
{
364-
ValueResult::ok(Some(Arc::new(arg.0.clone())))
371+
ValueResult::ok(Some(arg.clone()))
365372
} else {
366-
//FIXME: clean this up, the ast id map lookup is done twice here
367-
let (parse, map) = match loc.kind.file_id().repr() {
368-
HirFileIdRepr::FileId(file_id) => {
369-
let syntax = db.parse(file_id).to_syntax();
370-
371-
(syntax, SpanMap::RealSpanMap(db.real_span_map(file_id)))
372-
}
373-
HirFileIdRepr::MacroFile(macro_file) => {
374-
let (parse, map) = db.parse_macro_expansion(macro_file).value;
375-
(parse, SpanMap::ExpansionSpanMap(map))
376-
}
377-
};
373+
let (parse, map) = parse_with_map(db, loc.kind.file_id());
378374
let root = parse.syntax_node();
379375

380376
let syntax = match loc.kind {
381377
MacroCallKind::FnLike { ast_id, .. } => {
382378
let node = &ast_id.to_ptr(db).to_node(&root);
383379
let offset = node.syntax().text_range().start();
384-
match node.token_tree().map(|it| it.syntax().clone()) {
380+
match node.token_tree() {
385381
Some(tt) => {
386-
if let Some(e) = mismatched_delimiters(&tt) {
382+
let tt = tt.syntax();
383+
if let Some(e) = mismatched_delimiters(tt) {
387384
return ValueResult::only_err(e);
388385
}
389-
tt
386+
tt.clone()
390387
}
391388
None => {
392389
return ValueResult::only_err(Arc::new(Box::new([
@@ -479,17 +476,8 @@ fn decl_macro_expander(
479476
id: AstId<ast::Macro>,
480477
) -> Arc<DeclarativeMacroExpander> {
481478
let is_2021 = db.crate_graph()[def_crate].edition >= Edition::Edition2021;
482-
let (root, map) = match id.file_id.repr() {
483-
HirFileIdRepr::FileId(file_id) => {
484-
// FIXME: Arc
485-
// FIXME: id.to_ptr duplicated, expensive
486-
(db.parse(file_id).syntax_node(), SpanMap::RealSpanMap(db.real_span_map(file_id)))
487-
}
488-
HirFileIdRepr::MacroFile(macro_file) => {
489-
let (parse, map) = db.parse_macro_expansion(macro_file).value;
490-
(parse.syntax_node(), SpanMap::ExpansionSpanMap(map))
491-
}
492-
};
479+
let (root, map) = parse_with_map(db, id.file_id);
480+
let root = root.syntax_node();
493481

494482
let transparency = |node| {
495483
// ... would be nice to have the item tree here
@@ -568,21 +556,8 @@ fn macro_expand(
568556
let ExpandResult { value: tt, mut err } = match loc.def.kind {
569557
MacroDefKind::ProcMacro(..) => return db.expand_proc_macro(macro_call_id),
570558
MacroDefKind::BuiltInDerive(expander, ..) => {
571-
// FIXME: add firewall query for this?
572-
let hir_file_id = loc.kind.file_id();
573-
let (root, map) = match hir_file_id.repr() {
574-
HirFileIdRepr::FileId(file_id) => {
575-
// FIXME: query for span map
576-
(
577-
db.parse(file_id).syntax_node(),
578-
SpanMap::RealSpanMap(db.real_span_map(file_id)),
579-
)
580-
}
581-
HirFileIdRepr::MacroFile(macro_file) => {
582-
let (parse, map) = db.parse_macro_expansion(macro_file).value;
583-
(parse.syntax_node(), SpanMap::ExpansionSpanMap(map))
584-
}
585-
};
559+
let (root, map) = parse_with_map(db, loc.kind.file_id());
560+
let root = root.syntax_node();
586561
let MacroCallKind::Derive { ast_id, .. } = loc.kind else { unreachable!() };
587562
let node = ast_id.to_ptr(db).to_node(&root);
588563

@@ -710,9 +685,9 @@ fn token_tree_to_syntax_node(
710685
ExpandTo::Type => mbe::TopEntryPoint::Type,
711686
ExpandTo::Expr => mbe::TopEntryPoint::Expr,
712687
};
713-
let mut tm = mbe::token_tree_to_syntax_node(tt, entry_point);
688+
let (parse, mut span_map) = mbe::token_tree_to_syntax_node(tt, entry_point);
714689
// FIXME: now what the hell is going on here
715-
tm.1.span_map.sort_by(|(_, a), (_, b)| {
690+
span_map.span_map.sort_by(|(_, a), (_, b)| {
716691
a.anchor.file_id.cmp(&b.anchor.file_id).then_with(|| {
717692
let map = db.ast_id_map(a.anchor.file_id.into());
718693
map.get_erased(a.anchor.ast_id)
@@ -721,7 +696,7 @@ fn token_tree_to_syntax_node(
721696
.cmp(&map.get_erased(b.anchor.ast_id).text_range().start())
722697
})
723698
});
724-
tm
699+
(parse, span_map)
725700
}
726701

727702
fn check_tt_count(tt: &tt::Subtree) -> Result<(), ExpandResult<Arc<tt::Subtree>>> {

‎crates/hir-expand/src/eager.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,15 @@ pub fn expand_eager_macro_input(
8181
// FIXME: Spans!
8282
let mut subtree = mbe::syntax_node_to_token_tree(
8383
&expanded_eager_input,
84-
RealSpanMap::empty(<SpanAnchor as tt::SpanAnchor>::DUMMY.file_id),
84+
RealSpanMap::absolute(<SpanAnchor as tt::SpanAnchor>::DUMMY.file_id),
8585
);
8686

8787
subtree.delimiter = crate::tt::Delimiter::UNSPECIFIED;
8888

8989
let loc = MacroCallLoc {
9090
def,
9191
krate,
92-
eager: Some(Box::new(EagerCallInfo {
93-
arg: Arc::new((subtree,)),
94-
arg_id,
95-
error: err.clone(),
96-
})),
92+
eager: Some(Box::new(EagerCallInfo { arg: Arc::new(subtree), arg_id, error: err.clone() })),
9793
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
9894
call_site,
9995
};

‎crates/hir-expand/src/hygiene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use base_db::span::{MacroCallId, SpanData, SyntaxContextId};
88

99
use crate::db::ExpandDatabase;
1010

11-
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
11+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1212
pub struct SyntaxContextData {
1313
pub outer_expn: Option<MacroCallId>,
1414
pub outer_transparency: Transparency,

‎crates/hir-expand/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub enum MacroDefKind {
135135
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
136136
struct EagerCallInfo {
137137
/// The expanded argument of the eager macro.
138-
arg: Arc<(tt::Subtree,)>,
138+
arg: Arc<tt::Subtree>,
139139
/// Call id of the eager macro's input file (this is the macro file for its fully expanded input).
140140
arg_id: MacroCallId,
141141
error: Option<ExpandError>,
@@ -537,8 +537,6 @@ impl MacroCallKind {
537537
FileRange { range, file_id }
538538
}
539539

540-
// FIXME: -> InFile<SyntaxNode> it should be impossible for the token tree to be missing at
541-
// this point!
542540
fn arg(&self, db: &dyn db::ExpandDatabase) -> InFile<Option<SyntaxNode>> {
543541
match self {
544542
MacroCallKind::FnLike { ast_id, .. } => {
@@ -561,7 +559,6 @@ impl MacroCallKind {
561559
pub struct ExpansionInfo {
562560
pub expanded: InMacroFile<SyntaxNode>,
563561
/// The argument TokenTree or item for attributes
564-
// FIXME: Can this ever be `None`?
565562
arg: InFile<Option<SyntaxNode>>,
566563
/// The `macro_rules!` or attribute input.
567564
attr_input_or_mac_def: Option<InFile<ast::TokenTree>>,

‎crates/hir-expand/src/span.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ pub struct RealSpanMap {
7777
}
7878

7979
impl RealSpanMap {
80-
pub fn empty(file_id: FileId) -> Self {
80+
/// Creates a real file span map that returns absolute ranges (relative ranges to the root ast id).
81+
pub fn absolute(file_id: FileId) -> Self {
8182
RealSpanMap { file_id, pairs: Box::from([(TextSize::new(0), ROOT_ERASED_FILE_AST_ID)]) }
8283
}
8384

‎crates/hir-ty/src/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,7 @@ impl HirDisplay for TypeRef {
17381738
);
17391739
let macro_call = macro_call.to_node(f.db.upcast());
17401740
match macro_call.path() {
1741-
Some(path) => match Path::from_src(path, &ctx) {
1741+
Some(path) => match Path::from_src(&ctx, path) {
17421742
Some(path) => path.hir_fmt(f)?,
17431743
None => write!(f, "{{macro}}")?,
17441744
},

‎crates/hir/src/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn modpath_from_str(db: &dyn HirDatabase, link: &str) -> Option<ModPath> {
241241
ModPath::from_src(
242242
db.upcast(),
243243
ast_path,
244-
SpanMapRef::RealSpanMap(&RealSpanMap::empty(FileId(0))),
244+
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId(0))),
245245
)
246246
};
247247

0 commit comments

Comments
 (0)