Skip to content

Commit cdee65f

Browse files
committed
Newtype ErasedFileAstId
1 parent 2e7db9c commit cdee65f

File tree

13 files changed

+52
-39
lines changed

13 files changed

+52
-39
lines changed

src/tools/rust-analyzer/Cargo.lock

+1-2
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,7 @@ name = "paths"
12681268
version = "0.0.0"
12691269
dependencies = [
12701270
"camino",
1271+
"serde",
12711272
]
12721273

12731274
[[package]]
@@ -1330,14 +1331,12 @@ dependencies = [
13301331
"base-db",
13311332
"indexmap",
13321333
"intern",
1333-
"la-arena 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
13341334
"paths",
13351335
"rustc-hash",
13361336
"serde",
13371337
"serde_json",
13381338
"span",
13391339
"stdx",
1340-
"text-size",
13411340
"tracing",
13421341
"tt",
13431342
]

src/tools/rust-analyzer/crates/base-db/src/lib.rs

+1-1
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

src/tools/rust-analyzer/crates/paths/Cargo.toml

+2-5
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

src/tools/rust-analyzer/crates/proc-macro-api/Cargo.toml

+2-3
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]

src/tools/rust-analyzer/crates/proc-macro-api/src/msg.rs

+2-4
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([

src/tools/rust-analyzer/crates/proc-macro-api/src/msg/flat.rs

+3-5
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),

src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs

+2-2
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
};

src/tools/rust-analyzer/crates/proc-macro-srv/src/tests/utils.rs

+2-2
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
};

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

-2
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) => {

src/tools/rust-analyzer/crates/span/src/ast_id.rs

+32-7
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

src/tools/rust-analyzer/crates/span/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ pub use vfs::FileId;
2121
/// The root ast id always points to the encompassing file, using this in spans is discouraged as
2222
/// any range relative to it will be effectively absolute, ruining the entire point of anchored
2323
/// relative text ranges.
24-
pub const ROOT_ERASED_FILE_AST_ID: ErasedFileAstId =
25-
la_arena::Idx::from_raw(la_arena::RawIdx::from_u32(0));
24+
pub const ROOT_ERASED_FILE_AST_ID: ErasedFileAstId = ErasedFileAstId::from_raw(0);
2625

2726
/// FileId used as the span for syntax node fixups. Any Span containing this file id is to be
2827
/// considered fake.
2928
pub const FIXUP_ERASED_FILE_AST_ID_MARKER: ErasedFileAstId =
30-
// we pick the second to last for this in case we every consider making this a NonMaxU32, this
29+
// we pick the second to last for this in case we ever consider making this a NonMaxU32, this
3130
// is required to be stable for the proc-macro-server
32-
la_arena::Idx::from_raw(la_arena::RawIdx::from_u32(!0 - 1));
31+
ErasedFileAstId::from_raw(!0 - 1);
3332

3433
pub type Span = SpanData<SyntaxContextId>;
3534

src/tools/rust-analyzer/crates/span/src/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl fmt::Display for RealSpanMap {
119119
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120120
writeln!(f, "RealSpanMap({:?}):", self.file_id)?;
121121
for span in self.pairs.iter() {
122-
writeln!(f, "{}: {}", u32::from(span.0), span.1.into_raw().into_u32())?;
122+
writeln!(f, "{}: {}", u32::from(span.0), span.1.into_raw())?;
123123
}
124124
Ok(())
125125
}

src/tools/rust-analyzer/crates/test-fixture/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{iter, mem, str::FromStr, sync};
33

44
use base_db::{
55
CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Env, FileChange,
6-
FileSet, LangCrateOrigin, SourceRootDatabase, SourceRoot, Version, VfsPath,
6+
FileSet, LangCrateOrigin, SourceRoot, SourceRootDatabase, Version, VfsPath,
77
};
88
use cfg::CfgOptions;
99
use hir_expand::{

0 commit comments

Comments
 (0)