Skip to content

Commit 4bcf03e

Browse files
Use stable AST IDs
Instead of simple numbering, we hash important bits, like the name of the item. This will allow for much better incrementality, e.g. when you add an item. Currently, this invalidates the IDs of all following items, which invalidates pretty much everything.
1 parent 5b2c8bc commit 4bcf03e

File tree

22 files changed

+1230
-556
lines changed

22 files changed

+1230
-556
lines changed

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,26 +2141,10 @@ impl ExprCollector<'_> {
21412141
block: ast::BlockExpr,
21422142
mk_block: impl FnOnce(Option<BlockId>, Box<[Statement]>, Option<ExprId>) -> Expr,
21432143
) -> ExprId {
2144-
let block_has_items = {
2145-
let statement_has_item = block.statements().any(|stmt| match stmt {
2146-
ast::Stmt::Item(_) => true,
2147-
// Macro calls can be both items and expressions. The syntax library always treats
2148-
// them as expressions here, so we undo that.
2149-
ast::Stmt::ExprStmt(es) => matches!(es.expr(), Some(ast::Expr::MacroExpr(_))),
2150-
_ => false,
2151-
});
2152-
statement_has_item
2153-
|| matches!(block.tail_expr(), Some(ast::Expr::MacroExpr(_)))
2154-
|| (block.may_carry_attributes() && block.attrs().next().is_some())
2155-
};
2156-
2157-
let block_id = if block_has_items {
2158-
let file_local_id = self.expander.ast_id_map().ast_id(&block);
2144+
let block_id = self.expander.ast_id_map().ast_id_for_block(&block).map(|file_local_id| {
21592145
let ast_id = self.expander.in_file(file_local_id);
2160-
Some(self.db.intern_block(BlockLoc { ast_id, module: self.module }))
2161-
} else {
2162-
None
2163-
};
2146+
self.db.intern_block(BlockLoc { ast_id, module: self.module })
2147+
});
21642148

21652149
let (module, def_map) =
21662150
match block_id.map(|block_id| (block_def_map(self.db, block_id), block_id)) {

crates/hir-def/src/item_tree/pretty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ impl Printer<'_> {
353353
let MacroCall { path, ast_id, expand_to, ctxt } = &self.tree[it];
354354
let _ = writeln!(
355355
self,
356-
"// AstId: {:?}, SyntaxContextId: {}, ExpandTo: {:?}",
357-
ast_id.erase().into_raw(),
356+
"// AstId: {:#?}, SyntaxContextId: {}, ExpandTo: {:?}",
357+
ast_id.erase(),
358358
ctxt,
359359
expand_to
360360
);
@@ -377,7 +377,7 @@ impl Printer<'_> {
377377
}
378378

379379
fn print_ast_id(&mut self, ast_id: ErasedFileAstId) {
380-
wln!(self, "// AstId: {:?}", ast_id.into_raw());
380+
wln!(self, "// AstId: {ast_id:#?}");
381381
}
382382
}
383383

crates/hir-def/src/item_tree/tests.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ use a::{c, d::{e}};
3535
#![no_std]
3636
#![doc = " another file comment"]
3737
38-
// AstId: 1
38+
// AstId: ExternCrate[5A82, 0]
3939
pub(self) extern crate self as renamed;
4040
41-
// AstId: 2
41+
// AstId: ExternCrate[7E1C, 0]
4242
pub(super) extern crate bli;
4343
44-
// AstId: 3
44+
// AstId: Use[0000, 0]
4545
pub use crate::path::{nested, items as renamed, Trait as _};
4646
47-
// AstId: 4
47+
// AstId: Use[0000, 1]
4848
pub(self) use globs::*;
4949
5050
#[doc = " docs on import"]
51-
// AstId: 5
51+
// AstId: Use[0000, 2]
5252
pub(self) use crate::{A, B};
5353
54-
// AstId: 6
54+
// AstId: Use[0000, 3]
5555
pub(self) use a::{c, d::{e}};
5656
"##]],
5757
);
@@ -75,18 +75,18 @@ extern "C" {
7575
"#,
7676
expect![[r##"
7777
#[on_extern_block]
78-
// AstId: 1
78+
// AstId: ExternBlock[0000, 0]
7979
extern "C" {
8080
#[on_extern_type]
81-
// AstId: 2
81+
// AstId: TypeAlias[9FDF, 0]
8282
pub(self) type ExType;
8383
8484
#[on_extern_static]
85-
// AstId: 3
85+
// AstId: Static[43C1, 0]
8686
pub(self) static EX_STATIC = _;
8787
8888
#[on_extern_fn]
89-
// AstId: 4
89+
// AstId: Fn[452D, 0]
9090
pub(self) fn ex_fn;
9191
}
9292
"##]],
@@ -124,39 +124,39 @@ enum E {
124124
}
125125
"#,
126126
expect![[r#"
127-
// AstId: 1
127+
// AstId: Struct[DFF3, 0]
128128
pub(self) struct Unit;
129129
130130
#[derive(Debug)]
131-
// AstId: 2
131+
// AstId: Struct[C7A1, 0]
132132
pub(self) struct Struct {
133133
#[doc = " fld docs"]
134134
pub(self) fld,
135135
}
136136
137-
// AstId: 3
137+
// AstId: Struct[DAC2, 0]
138138
pub(self) struct Tuple(
139139
#[attr]
140140
pub(self) 0,
141141
);
142142
143-
// AstId: 4
143+
// AstId: Union[2DBB, 0]
144144
pub(self) union Ize {
145145
pub(self) a,
146146
pub(self) b,
147147
}
148148
149-
// AstId: 5
149+
// AstId: Enum[7FF8, 0]
150150
pub(self) enum E
151-
// AstId: 6
151+
// AstId: Variant[C717, 0]
152152
#[doc = " comment on Unit"]
153153
Unit,
154-
// AstId: 7
154+
// AstId: Variant[AEAB, 0]
155155
#[doc = " comment on Tuple"]
156156
Tuple(
157157
pub(self) 0,
158158
),
159-
// AstId: 8
159+
// AstId: Variant[4B1B, 0]
160160
Struct {
161161
#[doc = " comment on a: u8"]
162162
pub(self) a,
@@ -185,23 +185,23 @@ trait Tr: SuperTrait + 'lifetime {
185185
}
186186
"#,
187187
expect![[r#"
188-
// AstId: 1
188+
// AstId: Static[B393, 0]
189189
pub static ST = _;
190190
191-
// AstId: 2
191+
// AstId: Const[B309, 0]
192192
pub(self) const _ = _;
193193
194194
#[attr]
195195
#[inner_attr_in_fn]
196-
// AstId: 3
196+
// AstId: Fn[75E3, 0]
197197
pub(self) fn f;
198198
199-
// AstId: 4
199+
// AstId: Trait[2998, 0]
200200
pub(self) trait Tr {
201-
// AstId: 6
201+
// AstId: TypeAlias[9F08, 0]
202202
pub(self) type Assoc;
203203
204-
// AstId: 7
204+
// AstId: Fn[6C0C, 0]
205205
pub(self) fn method;
206206
}
207207
"#]],
@@ -226,16 +226,16 @@ mod outline;
226226
expect![[r##"
227227
#[doc = " outer"]
228228
#[doc = " inner"]
229-
// AstId: 1
229+
// AstId: Module[CF93, 0]
230230
pub(self) mod inline {
231-
// AstId: 3
231+
// AstId: Use[0000, 0]
232232
pub(self) use super::*;
233233
234-
// AstId: 4
234+
// AstId: Fn[1B26, 0]
235235
pub(self) fn fn_in_module;
236236
}
237237
238-
// AstId: 2
238+
// AstId: Module[8994, 0]
239239
pub(self) mod outline;
240240
"##]],
241241
);
@@ -254,13 +254,13 @@ pub macro m2() {}
254254
m!();
255255
"#,
256256
expect![[r#"
257-
// AstId: 1
257+
// AstId: MacroRules[88CE, 0]
258258
macro_rules! m { ... }
259259
260-
// AstId: 2
260+
// AstId: MacroDef[DC34, 0]
261261
pub macro m2 { ... }
262262
263-
// AstId: 3, SyntaxContextId: ROOT2024, ExpandTo: Items
263+
// AstId: MacroCall[612F, 0], SyntaxContextId: ROOT2024, ExpandTo: Items
264264
m!(...);
265265
"#]],
266266
);
@@ -273,7 +273,7 @@ fn pub_self() {
273273
pub(self) struct S;
274274
"#,
275275
expect![[r#"
276-
// AstId: 1
276+
// AstId: Struct[42E2, 0]
277277
pub(self) struct S;
278278
"#]],
279279
)

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ macro_rules! f {
3535
};
3636
}
3737
38-
struct#0:1@58..64#14336# MyTraitMap2#0:2@31..42#ROOT2024# {#0:1@72..73#14336#
39-
map#0:1@86..89#14336#:#0:1@89..90#14336# #0:1@89..90#14336#::#0:1@91..93#14336#std#0:1@93..96#14336#::#0:1@96..98#14336#collections#0:1@98..109#14336#::#0:1@109..111#14336#HashSet#0:1@111..118#14336#<#0:1@118..119#14336#(#0:1@119..120#14336#)#0:1@120..121#14336#>#0:1@121..122#14336#,#0:1@122..123#14336#
40-
}#0:1@132..133#14336#
38+
struct#0:MacroRules[8C8E, 0]@58..64#14336# MyTraitMap2#0:MacroCall[D499, 0]@31..42#ROOT2024# {#0:MacroRules[8C8E, 0]@72..73#14336#
39+
map#0:MacroRules[8C8E, 0]@86..89#14336#:#0:MacroRules[8C8E, 0]@89..90#14336# #0:MacroRules[8C8E, 0]@89..90#14336#::#0:MacroRules[8C8E, 0]@91..93#14336#std#0:MacroRules[8C8E, 0]@93..96#14336#::#0:MacroRules[8C8E, 0]@96..98#14336#collections#0:MacroRules[8C8E, 0]@98..109#14336#::#0:MacroRules[8C8E, 0]@109..111#14336#HashSet#0:MacroRules[8C8E, 0]@111..118#14336#<#0:MacroRules[8C8E, 0]@118..119#14336#(#0:MacroRules[8C8E, 0]@119..120#14336#)#0:MacroRules[8C8E, 0]@120..121#14336#>#0:MacroRules[8C8E, 0]@121..122#14336#,#0:MacroRules[8C8E, 0]@122..123#14336#
40+
}#0:MacroRules[8C8E, 0]@132..133#14336#
4141
"#]],
4242
);
4343
}
@@ -75,12 +75,12 @@ macro_rules! f {
7575
};
7676
}
7777
78-
fn#0:2@30..32#ROOT2024# main#0:2@33..37#ROOT2024#(#0:2@37..38#ROOT2024#)#0:2@38..39#ROOT2024# {#0:2@40..41#ROOT2024#
79-
1#0:2@50..51#ROOT2024#;#0:2@51..52#ROOT2024#
80-
1.0#0:2@61..64#ROOT2024#;#0:2@64..65#ROOT2024#
81-
(#0:2@74..75#ROOT2024#(#0:2@75..76#ROOT2024#1#0:2@76..77#ROOT2024#,#0:2@77..78#ROOT2024# )#0:2@78..79#ROOT2024#,#0:2@79..80#ROOT2024# )#0:2@80..81#ROOT2024#.#0:2@81..82#ROOT2024#0#0:2@82..85#ROOT2024#.#0:2@82..85#ROOT2024#0#0:2@82..85#ROOT2024#;#0:2@85..86#ROOT2024#
82-
let#0:2@95..98#ROOT2024# x#0:2@99..100#ROOT2024# =#0:2@101..102#ROOT2024# 1#0:2@103..104#ROOT2024#;#0:2@104..105#ROOT2024#
83-
}#0:2@110..111#ROOT2024#
78+
fn#0:MacroCall[D499, 0]@30..32#ROOT2024# main#0:MacroCall[D499, 0]@33..37#ROOT2024#(#0:MacroCall[D499, 0]@37..38#ROOT2024#)#0:MacroCall[D499, 0]@38..39#ROOT2024# {#0:MacroCall[D499, 0]@40..41#ROOT2024#
79+
1#0:MacroCall[D499, 0]@50..51#ROOT2024#;#0:MacroCall[D499, 0]@51..52#ROOT2024#
80+
1.0#0:MacroCall[D499, 0]@61..64#ROOT2024#;#0:MacroCall[D499, 0]@64..65#ROOT2024#
81+
(#0:MacroCall[D499, 0]@74..75#ROOT2024#(#0:MacroCall[D499, 0]@75..76#ROOT2024#1#0:MacroCall[D499, 0]@76..77#ROOT2024#,#0:MacroCall[D499, 0]@77..78#ROOT2024# )#0:MacroCall[D499, 0]@78..79#ROOT2024#,#0:MacroCall[D499, 0]@79..80#ROOT2024# )#0:MacroCall[D499, 0]@80..81#ROOT2024#.#0:MacroCall[D499, 0]@81..82#ROOT2024#0#0:MacroCall[D499, 0]@82..85#ROOT2024#.#0:MacroCall[D499, 0]@82..85#ROOT2024#0#0:MacroCall[D499, 0]@82..85#ROOT2024#;#0:MacroCall[D499, 0]@85..86#ROOT2024#
82+
let#0:MacroCall[D499, 0]@95..98#ROOT2024# x#0:MacroCall[D499, 0]@99..100#ROOT2024# =#0:MacroCall[D499, 0]@101..102#ROOT2024# 1#0:MacroCall[D499, 0]@103..104#ROOT2024#;#0:MacroCall[D499, 0]@104..105#ROOT2024#
83+
}#0:MacroCall[D499, 0]@110..111#ROOT2024#
8484
8585
8686
"#]],
@@ -171,7 +171,7 @@ fn main(foo: ()) {
171171
}
172172
173173
fn main(foo: ()) {
174-
/* error: unresolved macro unresolved */"helloworld!"#0:3@236..321#ROOT2024#;
174+
/* error: unresolved macro unresolved */"helloworld!"#0:Fn[B9C7, 0]@236..321#ROOT2024#;
175175
}
176176
}
177177
@@ -197,7 +197,7 @@ macro_rules! mk_struct {
197197
#[macro_use]
198198
mod foo;
199199
200-
struct#1:1@59..65#14336# Foo#0:2@32..35#ROOT2024#(#1:1@70..71#14336#u32#0:2@41..44#ROOT2024#)#1:1@74..75#14336#;#1:1@75..76#14336#
200+
struct#1:MacroRules[E572, 0]@59..65#14336# Foo#0:MacroCall[BDD3, 0]@32..35#ROOT2024#(#1:MacroRules[E572, 0]@70..71#14336#u32#0:MacroCall[BDD3, 0]@41..44#ROOT2024#)#1:MacroRules[E572, 0]@74..75#14336#;#1:MacroRules[E572, 0]@75..76#14336#
201201
"#]],
202202
);
203203
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ fn foo(&self) {
181181
self.0. 1;
182182
}
183183
184-
fn#0:1@45..47#ROOT2024# foo#0:1@48..51#ROOT2024#(#0:1@51..52#ROOT2024#&#0:1@52..53#ROOT2024#self#0:1@53..57#ROOT2024# )#0:1@57..58#ROOT2024# {#0:1@59..60#ROOT2024#
185-
self#0:1@65..69#ROOT2024# .#0:1@69..70#ROOT2024#0#0:1@70..71#ROOT2024#.#0:1@71..72#ROOT2024#1#0:1@73..74#ROOT2024#;#0:1@74..75#ROOT2024#
186-
}#0:1@76..77#ROOT2024#"#]],
184+
fn#0:Fn[4D85, 0]@45..47#ROOT2024# foo#0:Fn[4D85, 0]@48..51#ROOT2024#(#0:Fn[4D85, 0]@51..52#ROOT2024#&#0:Fn[4D85, 0]@52..53#ROOT2024#self#0:Fn[4D85, 0]@53..57#ROOT2024# )#0:Fn[4D85, 0]@57..58#ROOT2024# {#0:Fn[4D85, 0]@59..60#ROOT2024#
185+
self#0:Fn[4D85, 0]@65..69#ROOT2024# .#0:Fn[4D85, 0]@69..70#ROOT2024#0#0:Fn[4D85, 0]@70..71#ROOT2024#.#0:Fn[4D85, 0]@71..72#ROOT2024#1#0:Fn[4D85, 0]@73..74#ROOT2024#;#0:Fn[4D85, 0]@74..75#ROOT2024#
186+
}#0:Fn[4D85, 0]@76..77#ROOT2024#"#]],
187187
);
188188
}
189189

crates/hir-expand/src/builtin/quote.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ mod tests {
277277
assert_eq!(quoted.to_string(), "hello");
278278
let t = format!("{quoted:#?}");
279279
expect![[r#"
280-
SUBTREE $$ 937550:0@0..0#ROOT2024 937550:0@0..0#ROOT2024
281-
IDENT hello 937550:0@0..0#ROOT2024"#]]
280+
SUBTREE $$ 937550:Root[0000, 0]@0..0#ROOT2024 937550:Root[0000, 0]@0..0#ROOT2024
281+
IDENT hello 937550:Root[0000, 0]@0..0#ROOT2024"#]]
282282
.assert_eq(&t);
283283
}
284284

crates/hir-expand/src/files.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::borrow::Borrow;
33

44
use either::Either;
5-
use span::{AstIdNode, ErasedFileAstId, FileAstId, FileId, SyntaxContext};
5+
use span::{ErasedFileAstId, FileAstId, FileId, SyntaxContext};
66
use syntax::{AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize};
77

88
use crate::{
@@ -106,7 +106,7 @@ impl FileRange {
106106
/// It is stable across reparses, and can be used as salsa key/value.
107107
pub type AstId<N> = crate::InFile<FileAstId<N>>;
108108

109-
impl<N: AstIdNode> AstId<N> {
109+
impl<N: AstNode> AstId<N> {
110110
pub fn to_node(&self, db: &dyn ExpandDatabase) -> N {
111111
self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id))
112112
}

0 commit comments

Comments
 (0)