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

Commit efd76ec

Browse files
committed
Move AstIdMap infra to the span crate
1 parent 549aae7 commit efd76ec

File tree

14 files changed

+61
-73
lines changed

14 files changed

+61
-73
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use std::mem;
66
use base_db::CrateId;
77
use either::Either;
88
use hir_expand::{
9-
ast_id_map::AstIdMap,
109
name::{name, AsName, Name},
1110
ExpandError, InFile,
1211
};
1312
use intern::Interned;
1413
use profile::Count;
1514
use rustc_hash::FxHashMap;
1615
use smallvec::SmallVec;
16+
use span::AstIdMap;
1717
use syntax::{
1818
ast::{
1919
self, ArrayExprKind, AstChildren, BlockExpr, HasArgList, HasAttrs, HasLoopBody, HasName,

crates/hir-def/src/item_tree.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,13 @@ use std::{
4747
use ast::{AstNode, StructKind};
4848
use base_db::CrateId;
4949
use either::Either;
50-
use hir_expand::{
51-
ast_id_map::{AstIdNode, FileAstId},
52-
attrs::RawAttrs,
53-
name::Name,
54-
ExpandTo, HirFileId, InFile,
55-
};
50+
use hir_expand::{attrs::RawAttrs, name::Name, ExpandTo, HirFileId, InFile};
5651
use intern::Interned;
5752
use la_arena::{Arena, Idx, IdxRange, RawIdx};
5853
use profile::Count;
5954
use rustc_hash::FxHashMap;
6055
use smallvec::SmallVec;
61-
use span::Span;
56+
use span::{AstIdNode, FileAstId, Span};
6257
use stdx::never;
6358
use syntax::{ast, match_ast, SyntaxKind};
6459
use triomphe::Arc;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
33
use std::collections::hash_map::Entry;
44

5-
use hir_expand::{
6-
ast_id_map::AstIdMap, mod_path::path, name, name::AsName, span_map::SpanMapRef, HirFileId,
7-
};
5+
use hir_expand::{mod_path::path, name, name::AsName, span_map::SpanMapRef, HirFileId};
86
use la_arena::Arena;
7+
use span::AstIdMap;
98
use syntax::{
109
ast::{self, HasModuleItem, HasName, HasTypeBounds, IsString},
1110
AstNode,

crates/hir-def/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ use base_db::{
7676
CrateId, Edition,
7777
};
7878
use hir_expand::{
79-
ast_id_map::{AstIdNode, FileAstId},
8079
builtin_attr_macro::BuiltinAttrExpander,
8180
builtin_derive_macro::BuiltinDeriveExpander,
8281
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
@@ -91,7 +90,7 @@ use hir_expand::{
9190
use item_tree::ExternBlock;
9291
use la_arena::Idx;
9392
use nameres::DefMap;
94-
use span::{FileId, Span};
93+
use span::{AstIdNode, FileAstId, FileId, Span};
9594
use stdx::impl_from;
9695
use syntax::{ast, AstNode};
9796

crates/hir-def/src/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
use std::cell::OnceCell;
33

44
use hir_expand::{
5-
ast_id_map::{AstIdMap, AstIdNode},
65
span_map::{SpanMap, SpanMapRef},
76
AstId, HirFileId, InFile,
87
};
8+
use span::{AstIdMap, AstIdNode};
99
use syntax::ast;
1010
use triomphe::Arc;
1111

crates/hir-def/src/nameres.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ use std::ops::Deref;
6161

6262
use base_db::{CrateId, Edition, FileId};
6363
use hir_expand::{
64-
ast_id_map::FileAstId, name::Name, proc_macro::ProcMacroKind, HirFileId, InFile, MacroCallId,
65-
MacroDefId,
64+
name::Name, proc_macro::ProcMacroKind, HirFileId, InFile, MacroCallId, MacroDefId,
6665
};
6766
use itertools::Itertools;
6867
use la_arena::Arena;
6968
use profile::Count;
7069
use rustc_hash::{FxHashMap, FxHashSet};
70+
use span::FileAstId;
7171
use stdx::format_to;
7272
use syntax::{ast, SmolStr};
7373
use triomphe::Arc;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use base_db::{CrateId, Dependency, Edition, FileId};
99
use cfg::{CfgExpr, CfgOptions};
1010
use either::Either;
1111
use hir_expand::{
12-
ast_id_map::FileAstId,
1312
attrs::{Attr, AttrId},
1413
builtin_attr_macro::{find_builtin_attr, BuiltinAttrExpander},
1514
builtin_derive_macro::find_builtin_derive,
@@ -23,7 +22,7 @@ use itertools::{izip, Itertools};
2322
use la_arena::Idx;
2423
use limit::Limit;
2524
use rustc_hash::{FxHashMap, FxHashSet};
26-
use span::{ErasedFileAstId, Span, SyntaxContextId};
25+
use span::{ErasedFileAstId, FileAstId, Span, SyntaxContextId};
2726
use stdx::always;
2827
use syntax::{ast, SmolStr};
2928
use triomphe::Arc;

crates/hir-expand/src/db.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ use either::Either;
55
use limit::Limit;
66
use mbe::{syntax_node_to_token_tree, ValueResult};
77
use rustc_hash::FxHashSet;
8-
use span::{SyntaxContextData, SyntaxContextId};
8+
use span::{AstIdMap, SyntaxContextData, SyntaxContextId};
99
use syntax::{
1010
ast::{self, HasAttrs},
1111
AstNode, Parse, SyntaxError, SyntaxNode, SyntaxToken, T,
1212
};
1313
use triomphe::Arc;
1414

1515
use crate::{
16-
ast_id_map::AstIdMap,
1716
attrs::collect_attrs,
1817
builtin_attr_macro::pseudo_derive_attr_expansion,
1918
builtin_fn_macro::EagerExpander,
@@ -58,7 +57,6 @@ pub trait ExpandDatabase: SourceDatabase {
5857
#[salsa::input]
5958
fn proc_macros(&self) -> Arc<ProcMacros>;
6059

61-
#[salsa::invoke(AstIdMap::new)]
6260
fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
6361

6462
/// Main public API -- parses a hir file, not caring whether it's a real
@@ -253,6 +251,10 @@ pub fn expand_speculative(
253251
Some((node.syntax_node(), token))
254252
}
255253

254+
fn ast_id_map(db: &dyn ExpandDatabase, file_id: span::HirFileId) -> triomphe::Arc<AstIdMap> {
255+
triomphe::Arc::new(AstIdMap::from_source(&db.parse_or_expand(file_id)))
256+
}
257+
256258
fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> SyntaxNode {
257259
match file_id.repr() {
258260
HirFileIdRepr::FileId(file_id) => db.parse(file_id).syntax_node(),

crates/hir-expand/src/files.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
use std::iter;
33

44
use either::Either;
5-
use span::{FileId, FileRange, HirFileId, HirFileIdRepr, MacroFileId, SyntaxContextId};
6-
use syntax::{AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize};
5+
use span::{
6+
AstIdNode, ErasedFileAstId, FileAstId, FileId, FileRange, HirFileId, HirFileIdRepr,
7+
MacroFileId, SyntaxContextId,
8+
};
9+
use syntax::{AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize};
710

8-
use crate::{db, map_node_range_up, span_for_offset, MacroFileIdExt};
11+
use crate::{
12+
db::{self, ExpandDatabase},
13+
map_node_range_up, span_for_offset, MacroFileIdExt,
14+
};
915

1016
/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
1117
///
@@ -23,6 +29,31 @@ pub type InFile<T> = InFileWrapper<HirFileId, T>;
2329
pub type InMacroFile<T> = InFileWrapper<MacroFileId, T>;
2430
pub type InRealFile<T> = InFileWrapper<FileId, T>;
2531

32+
/// `AstId` points to an AST node in any file.
33+
///
34+
/// It is stable across reparses, and can be used as salsa key/value.
35+
pub type AstId<N> = crate::InFile<FileAstId<N>>;
36+
37+
impl<N: AstIdNode> AstId<N> {
38+
pub fn to_node(&self, db: &dyn ExpandDatabase) -> N {
39+
self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id))
40+
}
41+
pub fn to_in_file_node(&self, db: &dyn ExpandDatabase) -> crate::InFile<N> {
42+
crate::InFile::new(self.file_id, self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id)))
43+
}
44+
pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> AstPtr<N> {
45+
db.ast_id_map(self.file_id).get(self.value)
46+
}
47+
}
48+
49+
pub type ErasedAstId = crate::InFile<ErasedFileAstId>;
50+
51+
impl ErasedAstId {
52+
pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> SyntaxNodePtr {
53+
db.ast_id_map(self.file_id).get_erased(self.value)
54+
}
55+
}
56+
2657
impl<FileKind, T> InFileWrapper<FileKind, T> {
2758
pub fn new(file_id: FileKind, value: T) -> Self {
2859
Self { file_id, value }

crates/hir-expand/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
77
#![warn(rust_2018_idioms, unused_lifetimes)]
88

9-
pub mod ast_id_map;
109
pub mod attrs;
1110
pub mod builtin_attr_macro;
1211
pub mod builtin_derive_macro;
@@ -32,7 +31,7 @@ use std::{fmt, hash::Hash};
3231

3332
use base_db::{salsa::impl_intern_value_trivial, CrateId, Edition, FileId};
3433
use either::Either;
35-
use span::{FileRange, HirFileIdRepr, Span, SyntaxContextData, SyntaxContextId};
34+
use span::{ErasedFileAstId, FileRange, HirFileIdRepr, Span, SyntaxContextData, SyntaxContextId};
3635
use syntax::{
3736
ast::{self, AstNode},
3837
SyntaxNode, SyntaxToken, TextRange, TextSize,
@@ -49,8 +48,7 @@ use crate::{
4948
span_map::{ExpansionSpanMap, SpanMap},
5049
};
5150

52-
pub use crate::ast_id_map::{AstId, ErasedAstId, ErasedFileAstId};
53-
pub use crate::files::{InFile, InMacroFile, InRealFile};
51+
pub use crate::files::{AstId, ErasedAstId, InFile, InMacroFile, InRealFile};
5452

5553
pub use mbe::ValueResult;
5654
pub use span::{HirFileId, MacroCallId, MacroFileId};

crates/span/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ authors.workspace = true
1212
[dependencies]
1313
la-arena.workspace = true
1414
salsa.workspace = true
15-
15+
rustc-hash.workspace = true
16+
hashbrown.workspace = true
1617

1718
# local deps
1819
vfs.workspace = true

crates/hir-expand/src/ast_id_map.rs renamed to crates/span/src/ast_id.rs

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
//! item as an ID. That way, id's don't change unless the set of items itself
66
//! changes.
77
8-
// FIXME: Consider moving this into the span crate
9-
108
use std::{
119
any::type_name,
1210
fmt,
@@ -15,38 +13,12 @@ use std::{
1513
};
1614

1715
use la_arena::{Arena, Idx, RawIdx};
18-
use profile::Count;
1916
use rustc_hash::FxHasher;
2017
use syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr};
2118

22-
use crate::db::ExpandDatabase;
23-
24-
pub use span::ErasedFileAstId;
25-
26-
/// `AstId` points to an AST node in any file.
27-
///
28-
/// It is stable across reparses, and can be used as salsa key/value.
29-
pub type AstId<N> = crate::InFile<FileAstId<N>>;
30-
31-
impl<N: AstIdNode> AstId<N> {
32-
pub fn to_node(&self, db: &dyn ExpandDatabase) -> N {
33-
self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id))
34-
}
35-
pub fn to_in_file_node(&self, db: &dyn ExpandDatabase) -> crate::InFile<N> {
36-
crate::InFile::new(self.file_id, self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id)))
37-
}
38-
pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> AstPtr<N> {
39-
db.ast_id_map(self.file_id).get(self.value)
40-
}
41-
}
42-
43-
pub type ErasedAstId = crate::InFile<ErasedFileAstId>;
44-
45-
impl ErasedAstId {
46-
pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> SyntaxNodePtr {
47-
db.ast_id_map(self.file_id).get_erased(self.value)
48-
}
49-
}
19+
/// See crates\hir-expand\src\ast_id_map.rs
20+
/// This is a type erased FileAstId.
21+
pub type ErasedFileAstId = la_arena::Idx<syntax::SyntaxNodePtr>;
5022

5123
/// `AstId` points to an AST node in a specific file.
5224
pub struct FileAstId<N: AstIdNode> {
@@ -138,7 +110,6 @@ pub struct AstIdMap {
138110
arena: Arena<SyntaxNodePtr>,
139111
/// Reverse: map ptr to id.
140112
map: hashbrown::HashMap<Idx<SyntaxNodePtr>, (), ()>,
141-
_c: Count<Self>,
142113
}
143114

144115
impl fmt::Debug for AstIdMap {
@@ -155,14 +126,7 @@ impl PartialEq for AstIdMap {
155126
impl Eq for AstIdMap {}
156127

157128
impl AstIdMap {
158-
pub(crate) fn new(
159-
db: &dyn ExpandDatabase,
160-
file_id: span::HirFileId,
161-
) -> triomphe::Arc<AstIdMap> {
162-
triomphe::Arc::new(AstIdMap::from_source(&db.parse_or_expand(file_id)))
163-
}
164-
165-
fn from_source(node: &SyntaxNode) -> AstIdMap {
129+
pub fn from_source(node: &SyntaxNode) -> AstIdMap {
166130
assert!(node.parent().is_none());
167131
let mut res = AstIdMap::default();
168132

crates/span/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ use std::fmt::{self, Write};
33

44
use salsa::InternId;
55

6+
mod ast_id;
67
mod hygiene;
78
mod map;
89

910
pub use self::{
11+
ast_id::{AstIdMap, AstIdNode, ErasedFileAstId, FileAstId},
1012
hygiene::{SyntaxContextData, SyntaxContextId, Transparency},
1113
map::{RealSpanMap, SpanMap},
1214
};
@@ -26,10 +28,6 @@ pub struct FileRange {
2628
pub range: TextRange,
2729
}
2830

29-
/// See crates\hir-expand\src\ast_id_map.rs
30-
/// This is a type erased FileAstId.
31-
pub type ErasedFileAstId = la_arena::Idx<syntax::SyntaxNodePtr>;
32-
3331
// The first index is always the root node's AstId
3432
/// The root ast id always points to the encompassing file, using this in spans is discouraged as
3533
/// any range relative to it will be effectively absolute, ruining the entire point of anchored

0 commit comments

Comments
 (0)