Skip to content

Commit 188c577

Browse files
committed
Newtype ErasedFileAstId
1 parent fcb8883 commit 188c577

File tree

13 files changed

+52
-39
lines changed

13 files changed

+52
-39
lines changed

Cargo.lock

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

crates/base-db/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! base_db defines basic database traits. The concrete DB is defined by ide.
2-
2+
// FIXME: Rename this crate, base db is non descriptive
33
mod change;
44
mod input;
55

crates/paths/Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ doctest = false
1313

1414
[dependencies]
1515
camino.workspace = true
16-
# Adding this dep sadly puts a lot of rust-analyzer crates after the
17-
# serde-derive crate. Even though we don't activate the derive feature here,
18-
# someone else in the crate graph certainly does!
19-
# serde.workspace = true
16+
serde = { workspace = true, optional = true }
2017

2118
[features]
22-
serde1 = ["camino/serde1"]
19+
serde1 = ["camino/serde1", "dep:serde"]
2320

2421
[lints]
2522
workspace = true

crates/proc-macro-api/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ indexmap.workspace = true
2222
paths = { workspace = true, features = ["serde1"] }
2323
tt.workspace = true
2424
stdx.workspace = true
25-
text-size.workspace = true
26-
span.workspace = true
2725
# Ideally this crate would not depend on salsa things, but we need span information here which wraps
2826
# InternIds for the syntax context
27+
span.workspace = true
28+
# only here due to the `Env` newtype :/
2929
base-db.workspace = true
30-
la-arena.workspace = true
3130
intern.workspace = true
3231

3332
[lints]

crates/proc-macro-api/src/msg.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ type ProtocolWrite<W: Write> = for<'o, 'msg> fn(out: &'o mut W, msg: &'msg str)
158158
#[cfg(test)]
159159
mod tests {
160160
use intern::{sym, Symbol};
161-
use la_arena::RawIdx;
162-
use span::{ErasedFileAstId, Span, SpanAnchor, SyntaxContextId};
163-
use text_size::{TextRange, TextSize};
161+
use span::{ErasedFileAstId, Span, SpanAnchor, SyntaxContextId, TextRange, TextSize};
164162
use tt::{Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, Spacing, Subtree, TokenTree};
165163

166164
use super::*;
@@ -171,7 +169,7 @@ mod tests {
171169
span::FileId::from_raw(0xe4e4e),
172170
span::Edition::CURRENT,
173171
),
174-
ast_id: ErasedFileAstId::from_raw(RawIdx::from(0)),
172+
ast_id: ErasedFileAstId::from_raw(0),
175173
};
176174

177175
let token_trees = Box::new([

crates/proc-macro-api/src/msg/flat.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@
3838
use std::collections::VecDeque;
3939

4040
use intern::Symbol;
41-
use la_arena::RawIdx;
4241
use rustc_hash::FxHashMap;
4342
use serde::{Deserialize, Serialize};
44-
use span::{EditionedFileId, ErasedFileAstId, Span, SpanAnchor, SyntaxContextId};
45-
use text_size::TextRange;
43+
use span::{EditionedFileId, ErasedFileAstId, Span, SpanAnchor, SyntaxContextId, TextRange};
4644

4745
use crate::msg::{ENCODE_CLOSE_SPAN_VERSION, EXTENDED_LEAF_DATA};
4846

@@ -54,7 +52,7 @@ pub fn serialize_span_data_index_map(map: &SpanDataIndexMap) -> Vec<u32> {
5452
.flat_map(|span| {
5553
[
5654
span.anchor.file_id.as_u32(),
57-
span.anchor.ast_id.into_raw().into_u32(),
55+
span.anchor.ast_id.into_raw(),
5856
span.range.start().into(),
5957
span.range.end().into(),
6058
span.ctx.into_u32(),
@@ -71,7 +69,7 @@ pub fn deserialize_span_data_index_map(map: &[u32]) -> SpanDataIndexMap {
7169
Span {
7270
anchor: SpanAnchor {
7371
file_id: EditionedFileId::from_raw(file_id),
74-
ast_id: ErasedFileAstId::from_raw(RawIdx::from_u32(ast_id)),
72+
ast_id: ErasedFileAstId::from_raw(ast_id),
7573
},
7674
range: TextRange::new(start.into(), end.into()),
7775
ctx: SyntaxContextId::from_u32(e),

crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ mod tests {
479479
range: TextRange::empty(TextSize::new(0)),
480480
anchor: span::SpanAnchor {
481481
file_id: EditionedFileId::current_edition(FileId::from_raw(0)),
482-
ast_id: span::ErasedFileAstId::from_raw(0.into()),
482+
ast_id: span::ErasedFileAstId::from_raw(0),
483483
},
484484
ctx: SyntaxContextId::ROOT,
485485
};
@@ -515,7 +515,7 @@ mod tests {
515515
range: TextRange::empty(TextSize::new(0)),
516516
anchor: span::SpanAnchor {
517517
file_id: EditionedFileId::current_edition(FileId::from_raw(0)),
518-
ast_id: span::ErasedFileAstId::from_raw(0.into()),
518+
ast_id: span::ErasedFileAstId::from_raw(0),
519519
},
520520
ctx: SyntaxContextId::ROOT,
521521
};

crates/proc-macro-srv/src/tests/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ fn assert_expand_impl(
6969
range: TextRange::new(0.into(), 150.into()),
7070
anchor: SpanAnchor {
7171
file_id: EditionedFileId::current_edition(FileId::from_raw(41)),
72-
ast_id: ErasedFileAstId::from_raw(From::from(1)),
72+
ast_id: ErasedFileAstId::from_raw(1),
7373
},
7474
ctx: SyntaxContextId::ROOT,
7575
};
7676
let call_site = Span {
7777
range: TextRange::new(0.into(), 100.into()),
7878
anchor: SpanAnchor {
7979
file_id: EditionedFileId::current_edition(FileId::from_raw(42)),
80-
ast_id: ErasedFileAstId::from_raw(From::from(2)),
80+
ast_id: ErasedFileAstId::from_raw(2),
8181
},
8282
ctx: SyntaxContextId::ROOT,
8383
};

crates/rust-analyzer/tests/slow-tests/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,6 @@ fn resolve_proc_macro() {
10841084
let sysroot = project_model::Sysroot::discover(
10851085
&AbsPathBuf::assert_utf8(std::env::current_dir().unwrap()),
10861086
&Default::default(),
1087-
false,
10881087
);
10891088

10901089
let proc_macro_server_path = sysroot.discover_proc_macro_srv().unwrap();
@@ -1125,7 +1124,6 @@ edition = "2021"
11251124
proc-macro = true
11261125
11271126
//- /bar/src/lib.rs
1128-
extern crate proc_macro;
11291127
use proc_macro::{Delimiter, Group, Ident, Span, TokenStream, TokenTree};
11301128
macro_rules! t {
11311129
($n:literal) => {

crates/span/src/ast_id.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,28 @@ use syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr};
1818

1919
/// See crates\hir-expand\src\ast_id_map.rs
2020
/// This is a type erased FileAstId.
21-
pub type ErasedFileAstId = la_arena::Idx<syntax::SyntaxNodePtr>;
21+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
22+
pub struct ErasedFileAstId(u32);
23+
24+
impl ErasedFileAstId {
25+
pub const fn into_raw(self) -> u32 {
26+
self.0
27+
}
28+
pub const fn from_raw(u32: u32) -> Self {
29+
Self(u32)
30+
}
31+
}
32+
33+
impl fmt::Display for ErasedFileAstId {
34+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35+
self.0.fmt(f)
36+
}
37+
}
38+
impl fmt::Debug for ErasedFileAstId {
39+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40+
self.0.fmt(f)
41+
}
42+
}
2243

2344
/// `AstId` points to an AST node in a specific file.
2445
pub struct FileAstId<N: AstIdNode> {
@@ -47,7 +68,7 @@ impl<N: AstIdNode> Hash for FileAstId<N> {
4768

4869
impl<N: AstIdNode> fmt::Debug for FileAstId<N> {
4970
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50-
write!(f, "FileAstId::<{}>({})", type_name::<N>(), self.raw.into_raw())
71+
write!(f, "FileAstId::<{}>({})", type_name::<N>(), self.raw)
5172
}
5273
}
5374

@@ -176,7 +197,10 @@ impl AstIdMap {
176197
let ptr = ptr.syntax_node_ptr();
177198
let hash = hash_ptr(&ptr);
178199
match self.map.raw_entry().from_hash(hash, |&idx| self.arena[idx] == ptr) {
179-
Some((&raw, &())) => FileAstId { raw, covariant: PhantomData },
200+
Some((&raw, &())) => FileAstId {
201+
raw: ErasedFileAstId(raw.into_raw().into_u32()),
202+
covariant: PhantomData,
203+
},
180204
None => panic!(
181205
"Can't find {:?} in AstIdMap:\n{:?}",
182206
ptr,
@@ -186,18 +210,19 @@ impl AstIdMap {
186210
}
187211

188212
pub fn get<N: AstIdNode>(&self, id: FileAstId<N>) -> AstPtr<N> {
189-
AstPtr::try_from_raw(self.arena[id.raw]).unwrap()
213+
AstPtr::try_from_raw(self.arena[Idx::from_raw(RawIdx::from_u32(id.raw.into_raw()))])
214+
.unwrap()
190215
}
191216

192217
pub fn get_erased(&self, id: ErasedFileAstId) -> SyntaxNodePtr {
193-
self.arena[id]
218+
self.arena[Idx::from_raw(RawIdx::from_u32(id.into_raw()))]
194219
}
195220

196221
fn erased_ast_id(&self, item: &SyntaxNode) -> ErasedFileAstId {
197222
let ptr = SyntaxNodePtr::new(item);
198223
let hash = hash_ptr(&ptr);
199224
match self.map.raw_entry().from_hash(hash, |&idx| self.arena[idx] == ptr) {
200-
Some((&idx, &())) => idx,
225+
Some((&idx, &())) => ErasedFileAstId(idx.into_raw().into_u32()),
201226
None => panic!(
202227
"Can't find {:?} in AstIdMap:\n{:?}",
203228
item,
@@ -207,7 +232,7 @@ impl AstIdMap {
207232
}
208233

209234
fn alloc(&mut self, item: &SyntaxNode) -> ErasedFileAstId {
210-
self.arena.alloc(SyntaxNodePtr::new(item))
235+
ErasedFileAstId(self.arena.alloc(SyntaxNodePtr::new(item)).into_raw().into_u32())
211236
}
212237
}
213238

0 commit comments

Comments
 (0)