1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* rust-intelligence — Ultra-fast, zero-memory code intelligence for Rust
* Copyright (c) 2026 Alexander Panasenko
*
* Contact: alex@prod.codes
* Author: https://prod.codes/about/
* Project: https://github.com/alex09x/rust-intelligence
* SPDX-License-Identifier: MIT OR Apache-2.0
*/
//! Core `u32`-keyed identifiers and trait boundaries (`SourceDb`, `ItemDb`) decoupling
//! `ridx-syntax::name_res` from binary index storage (`ridx-format`).
use super::db_impls::{ImplTargetKey, IndexedImpl};
/// Dense crate or compilation-unit identifier (`u32::MAX` = absent sentinel).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CrateId(pub u32);
impl CrateId {
pub const NONE: Self = Self(u32::MAX);
/// Synthetic crate slot used for dirty-file local items not yet present in the compiler index.
pub const LOCAL_OVERLAY: Self = Self(u32::MAX - 1);
#[inline]
pub const fn is_some(self) -> bool {
self.0 != Self::NONE.0
}
}
impl Default for CrateId {
#[inline]
fn default() -> Self {
Self::NONE
}
}
/// Dense source-file identifier inside a [`SourceDb`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FileId(pub u32);
impl FileId {
pub const NONE: Self = Self(u32::MAX);
#[inline]
pub const fn is_some(self) -> bool {
self.0 != Self::NONE.0
}
}
impl Default for FileId {
#[inline]
fn default() -> Self {
Self::NONE
}
}
/// Dense module identifier inside a crate's [`super::mod_tree::ModuleTree`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ModuleId(pub u32);
impl ModuleId {
pub const ROOT: Self = Self(0);
pub const NONE: Self = Self(u32::MAX);
#[inline]
pub const fn is_some(self) -> bool {
self.0 != Self::NONE.0
}
}
impl Default for ModuleId {
#[inline]
fn default() -> Self {
Self::NONE
}
}
/// Compact 8-byte global definition key (`(CrateId, def_index)`), mapping 1-to-1 to `(shard_idx, def_idx)`
/// in `WorkspaceIndex` or `(CrateId::LOCAL_OVERLAY, local_idx)` for dirty-file definitions.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct GlobalDefId {
pub krate: CrateId,
pub index: u32,
}
impl GlobalDefId {
pub const NONE: Self = Self { krate: CrateId::NONE, index: u32::MAX };
#[inline]
pub const fn new(krate: CrateId, index: u32) -> Self {
Self { krate, index }
}
#[inline]
pub const fn is_some(self) -> bool {
self.krate.is_some() && self.index != u32::MAX
}
#[inline]
pub const fn is_overlay(self) -> bool {
self.krate.0 == CrateId::LOCAL_OVERLAY.0 && self.index != u32::MAX
}
}
impl Default for GlobalDefId {
#[inline]
fn default() -> Self {
Self::NONE
}
}
/// Rust language edition of a crate.
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Edition {
Edition2015 = 0,
Edition2018 = 1,
#[default]
Edition2021 = 2,
Edition2024 = 3,
}
impl Edition {
#[inline]
pub const fn has_uniform_paths(self) -> bool {
(self as u8) >= (Self::Edition2018 as u8)
}
}
/// Visibility class aligned with `ridx-format::ItemVis`, extended with local `super` / `self` forms.
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum VisKind {
Public = 0,
Crate = 1,
Restricted = 2,
#[default]
Private = 3,
Super = 4,
}
/// Kind of an indexed or local definition, aligned with `ridx-format::DefKind`.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ItemKind {
Mod = 0,
Struct = 1,
Enum = 2,
Union = 3,
Trait = 4,
Fn = 5,
TypeAlias = 6,
Const = 7,
Static = 8,
Variant = 9,
Field = 10,
AssocFn = 11,
AssocTy = 12,
AssocConst = 13,
Macro = 14,
Impl = 15,
Use = 16,
ExternCrate = 17,
}
impl ItemKind {
/// Bitmask of Rust namespaces (`NsMask`) occupied by a definition of this kind.
#[inline]
pub const fn ns_mask(self, has_value_ctor: bool) -> NsMask {
match self {
Self::Mod | Self::Enum | Self::Union | Self::Trait | Self::TypeAlias | Self::AssocTy => NsMask::TYPE,
Self::Struct | Self::Variant => {
if has_value_ctor {
NsMask(NsMask::TYPE.0 | NsMask::VALUE.0)
} else {
NsMask::TYPE
}
}
Self::Fn | Self::Const | Self::Static | Self::Field | Self::AssocFn | Self::AssocConst => NsMask::VALUE,
Self::Macro => NsMask::MACRO,
Self::ExternCrate => NsMask::TYPE,
Self::Impl | Self::Use => NsMask::EMPTY,
}
}
}
/// Bitmask over the three Rust namespaces (`type`, `value`, `macro`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct NsMask(pub u8);
impl NsMask {
pub const EMPTY: Self = Self(0);
pub const TYPE: Self = Self(1 << 0);
pub const VALUE: Self = Self(1 << 1);
pub const MACRO: Self = Self(1 << 2);
pub const ALL: Self = Self(Self::TYPE.0 | Self::VALUE.0 | Self::MACRO.0);
#[inline]
pub const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
#[inline]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
/// Indexed definition summary surfaced by [`ItemDb`] for module children and associated items.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IndexedDef<'a> {
pub id: GlobalDefId,
pub name: &'a str,
pub kind: ItemKind,
pub vis: VisKind,
pub has_value_ctor: bool,
}
/// Indexed `use` / re-export row surfaced by [`ItemDb`] (matching `v5` `UseEntry`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IndexedUse<'a> {
pub name: &'a str,
pub type_def: GlobalDefId,
pub value_def: GlobalDefId,
pub macro_def: GlobalDefId,
pub vis: VisKind,
pub is_glob: bool,
pub is_reexport: bool,
pub is_prelude: bool,
pub is_underscore: bool,
pub prim_ty_code: u8,
}
/// Read-only virtual filesystem / source provider for workspace `.rs` files.
pub trait SourceDb {
/// Maps a forward-slash normalized crate-relative or workspace-relative path to its [`FileId`].
fn resolve_path(&self, normalized_path: &str) -> Option<FileId>;
/// Forward-slash normalized path of `file`.
fn file_path(&self, file: FileId) -> &str;
/// UTF-8 source text of `file` (including any unsaved editor overlay).
fn file_text(&self, file: FileId) -> &str;
}
/// Compiler-index query boundary (`v5` `.ridx` adapter or in-memory test database).
pub trait ItemDb {
/// Returns `true` when bare `#[cfg(key)]` is active for `krate` (e.g. `unix`, `test`).
fn cfg_flag(&self, krate: CrateId, key: &str) -> bool;
/// Returns `true` when `#[cfg(key = "value")]` is active for `krate` (e.g. `feature = "rt"`).
fn cfg_key_value(&self, krate: CrateId, key: &str, value: &str) -> bool;
/// Language edition of `krate`.
fn crate_edition(&self, _krate: CrateId) -> Edition {
Edition::Edition2021
}
/// Canonical crate name of `krate` (underscores, e.g. `"ridx_syntax"`).
fn crate_name(&self, _krate: CrateId) -> &str {
""
}
/// Looks up a compile-time Cargo/rustc environment variable (`OUT_DIR`, `CARGO_MANIFEST_DIR`, ...)
/// recorded for `krate`.
fn crate_env(&self, _krate: CrateId, _key: &str) -> Option<&str> {
None
}
/// Resolves an `include!(...)` target relative to `from_file` (or absolute) to `(FileId, source_text)`
/// when a backing [`SourceDb`] is attached to this [`ItemDb`].
fn resolve_include_file(&self, _from_file: FileId, _rel_or_abs: &str) -> Option<(FileId, &str)> {
None
}
/// Returns the exact byte length of an `include_bytes!(...)` target relative to `from_file` (or absolute).
fn include_bytes_len(&self, _from_file: FileId, _rel_or_abs: &str) -> Option<u64> {
None
}
/// Resolves an external dependency crate available to `from_krate` under `extern_name`.
fn extern_crate(&self, _from_krate: CrateId, _extern_name: &str) -> Option<CrateId> {
None
}
/// Appends all external dependency crates visible to `from_krate` as `(extern_name, CrateId)`.
fn extern_crates<'a>(&'a self, _from_krate: CrateId, _out: &mut Vec<(&'a str, CrateId)>) {}
/// Returns the crate root [`GlobalDefId`] of `krate`, if indexed.
fn crate_root_def(&self, _krate: CrateId) -> Option<GlobalDefId> {
None
}
/// Returns the prelude module [`GlobalDefId`] configured for `krate`, if any.
fn prelude_module(&self, _krate: CrateId) -> Option<GlobalDefId> {
None
}
/// Looks up a declarative `macro_rules!` body (`($pat) => { $body }; ...`) for item-level
/// expansion when `call_path` (e.g. `"cfg_if"` or `"cfg::alloc"`) is invoked in `module_path`.
fn macro_rules_body(&self, _krate: CrateId, _module_path: &str, _call_path: &str) -> Option<&str> {
None
}
/// Like [`ItemDb::macro_rules_body`], also returning the `$crate` substitution token
/// (`"crate"` for crate-local macros or the dependency crate alias for external macros).
fn macro_rules_with_crate(&self, krate: CrateId, module_path: &str, call_path: &str) -> Option<(&str, &str)> {
self.macro_rules_body(krate, module_path, call_path).map(|b| (b, "crate"))
}
/// Looks up the declarative `macro_rules!` `(body, dollar_crate)` for a resolved macro [`GlobalDefId`]
/// invoked inside `from_krate`.
fn macro_rules_by_def(&self, _from_krate: CrateId, _def: GlobalDefId) -> Option<(&str, &str)> {
None
}
/// Returns the [`ItemKind`] of an indexed [`GlobalDefId`], if known (`O(1)` in `.ridx`).
fn def_kind(&self, _def: GlobalDefId) -> Option<ItemKind> {
None
}
/// Returns the defining workspace [`FileId`] of `def` (`FileId::NONE` for external crates).
fn def_file(&self, _def: GlobalDefId) -> FileId {
FileId::NONE
}
/// Underlying [`ImplTargetKey`] of an indexed `type Alias = Target;` (`None` if not an alias or unknown).
fn type_alias_target(&self, _alias_def: GlobalDefId) -> Option<ImplTargetKey> {
None
}
/// Appends indexed `impl` blocks matching `target` across `krate` + visible dependency crates.
fn impls_for_target(&self, _target: ImplTargetKey, _out: &mut Vec<IndexedImpl>) {}
/// Appends indexed `impl` blocks defined inside `krate`.
fn crate_impls(&self, _krate: CrateId, _out: &mut Vec<IndexedImpl>) {}
/// Appends direct child definitions of `module_def` into `out`.
fn module_defs<'a>(&'a self, _module_def: GlobalDefId, _out: &mut Vec<IndexedDef<'a>>) {}
/// Appends variants of an indexed `enum_def` into `out` (defaults to `module_defs`).
fn enum_variants<'a>(&'a self, enum_def: GlobalDefId, out: &mut Vec<IndexedDef<'a>>) {
self.module_defs(enum_def, out);
}
/// Appends `UseEntry` rows belonging to `module_def` (or crate root) into `out`.
fn module_uses<'a>(&'a self, _module_def: GlobalDefId, _out: &mut Vec<IndexedUse<'a>>) {}
/// Appends inherent and trait associated items on `owner_def` into `out`.
fn assoc_items<'a>(&'a self, _owner_def: GlobalDefId, _out: &mut Vec<IndexedDef<'a>>) {}
/// Appends direct supertrait [`GlobalDefId`]s declared on `trait_def` (`trait Sub: Super`).
fn trait_supertraits(&self, _trait_def: GlobalDefId, _out: &mut Vec<GlobalDefId>) {}
/// Resolves a named field on `adt_or_variant_def` (`Struct`, `Union`, or `Variant`), if indexed.
fn adt_field(&self, _adt_or_variant_def: GlobalDefId, _field_name: &str) -> Option<GlobalDefId> {
None
}
/// Resolves an indexed definition in `(krate, file)` whose identifier byte span is `[start..end)`.
fn def_at_byte_span(&self, _krate: CrateId, _file: FileId, _start: u32, _end: u32) -> GlobalDefId {
GlobalDefId::NONE
}
/// Looks up a lang item (`u16` discriminant matching `LangItemKind`) across visible shards.
fn lang_item(&self, _lang_kind: u16) -> Option<GlobalDefId> {
None
}
}