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
/*
* 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
*/
//! Scoped qualifier resolution: resolves path qualifiers (e.g. `range::`, `crate::foo::`)
//! respecting Rust's lexical and module visibility hierarchy:
//! 1. File scope: local `mod`, local items, and local `use` declarations in `file`.
//! 2. Crate scope: file's enclosing module and crate root (`crate::<qual>`).
//! 3. Prelude: standard library prelude 2021/2024 types and traits.
//! 4. External dependencies: crates in dependencies.
//! 5. Sysroot: standard library (`core`, `alloc`, `std`) if explicitly prefixed or requested.
//! 6. Fallback: single unique workspace match.
use super::prelude::prelude_candidate_paths;
use super::WorkspaceIndex;
use crate::format::{DefEntry, DefKind};
use crate::reader::ResolvedDefinition;
impl WorkspaceIndex {
fn resolve_file_item<'a>(&'a self, idx: usize, def: &'a DefEntry, rest_opt: Option<&str>) -> Option<ResolvedDefinition<'a>> {
let kind = DefKind::from(def.kind);
if kind == DefKind::Use {
let resolved = self.make_resolved_definition(idx, def);
let target_opt = resolved
.signature
.and_then(|sig| self.find_definition_by_path(sig))
.or_else(|| self.find_indexed_definition_at(resolved.file, resolved.start_line, resolved.start_col));
if let Some(target) = target_opt {
if target.kind == DefKind::Use
|| target.kind == DefKind::Local
|| (target.file == resolved.file && target.start_line == resolved.start_line && target.start_col == resolved.start_col)
{
return None;
}
if let Some(rest) = rest_opt {
return self.find_definition_by_path(&format!("{}::{rest}", target.path));
} else {
return Some(target);
}
}
return None;
} else if matches!(kind, DefKind::Mod | DefKind::Struct | DefKind::Enum | DefKind::Union | DefKind::Trait | DefKind::TyAlias) {
let local_def = self.make_resolved_definition(idx, def);
if let Some(rest) = rest_opt {
return self.find_definition_by_path(&format!("{}::{rest}", local_def.path));
} else {
return Some(local_def);
}
}
None
}
/// Resolves a qualifier path in the lexical and module scope of `file`.
pub fn resolve_qualifier<'a>(&'a self, file: &str, qual: &str) -> Option<ResolvedDefinition<'a>> {
if qual.is_empty() {
return None;
}
if qual == "crate" {
let cname = self.crate_name_for_file(file)?;
return self.find_definition_by_path(cname);
}
if let Some(rest) = qual.strip_prefix("crate::") {
let cname = self.crate_name_for_file(file)?;
return self.find_definition_by_path(&format!("{cname}::{rest}"));
}
// Handle `super::` qualifiers by navigating up from the file's module path
if qual.starts_with("super::") {
let mod_path = self.module_path_for_file(file)?;
let mut parts: Vec<&str> = mod_path.split("::").collect();
let mut cur = qual;
while let Some(rest) = cur.strip_prefix("super::") {
if parts.len() > 1 {
parts.pop();
}
cur = rest;
}
let base = parts.join("::");
let target = if cur.is_empty() { base } else { format!("{base}::{cur}") };
return self.find_definition_by_path(&target);
}
let explicit_self = qual.starts_with("self::");
let qual_trimmed = if let Some(rest) = qual.strip_prefix("self::") { rest } else { qual };
let mut segs = qual_trimmed.split("::");
let first_seg = segs.next().unwrap_or(qual_trimmed);
let rest_opt = if qual_trimmed.len() > first_seg.len() + 2 { Some(&qual_trimmed[first_seg.len() + 2..]) } else { None };
let reader_indices = self.reader_indices_for_file(file);
let crate_idx = self
.reader_index_from_indices(&reader_indices, file)
.or_else(|| reader_indices.first().copied().or_else(|| self.candidate_reader_indices_for_file(file).into_iter().next()));
let mod_path_opt = crate_idx.and_then(|idx| self.module_path_for_reader(idx, file));
let is_sysroot = !explicit_self && matches!(first_seg, "core" | "alloc" | "std");
let is_dep_crate = !explicit_self && !is_sysroot && self.has_crate(first_seg);
// 1. File scope: check definitions and imports declared directly in `file` across matching readers
let mut seen_globs = std::collections::HashSet::new();
for &idx in &reader_indices {
let reader = &self.readers[idx];
if let Some(fid) = self.target_file_id_for_reader(idx, file) {
let target_file = reader.get_file(fid);
// Fast path: check if local item exists with path `<mod_path>::<first_seg>`
if !is_sysroot && !is_dep_crate {
if let Some(ref mod_path) = mod_path_opt {
let fast_target = format!("{mod_path}::{first_seg}");
if let Some(def) = reader.find_definition_by_path(&fast_target) {
let span = def.span();
if span.file_id == fid {
if let Some(res) = self.resolve_file_item(idx, def, rest_opt) {
return Some(res);
}
}
}
}
}
let defs = reader.definitions();
let file_defs = self.unit_match.defs_in_file_up_to(idx, fid, u32::MAX);
for def in file_defs.iter().filter_map(|&(_, _, di)| defs.get(di as usize)) {
if reader.get_str(def.name_offset()) == first_seg {
if (is_sysroot || is_dep_crate) && DefKind::from(def.kind) == DefKind::Use {
continue;
}
if let Some(res) = self.resolve_file_item(idx, def, rest_opt) {
return Some(res);
}
}
}
// Glob imports declared in `file`: `use X::*` (and `use super::*`)
if !is_sysroot && !is_dep_crate {
let cname = reader.crate_name();
for def in file_defs.iter().filter_map(|&(_, _, di)| defs.get(di as usize)) {
if DefKind::from(def.kind) == DefKind::Use {
let name = reader.get_str(def.name_offset());
let path = reader.get_str(def.path_offset());
let is_glob = name == "*" || name == "{{glob}}" || name.starts_with("{{use}}") || path.ends_with("::*");
if is_glob {
let raw_target = if def.sig_offset() != 0 {
let sig = reader.get_str(def.sig_offset());
sig.strip_suffix("::*").unwrap_or(sig).to_string()
} else {
let resolved = self.make_resolved_definition(idx, def);
if let Some(target) = reader.find_definition_at(target_file.as_ref(), resolved.start_line, resolved.start_col) {
target.path.to_string()
} else if let Some(stripped) = path.strip_suffix("::*") {
stripped.to_string()
} else {
String::new()
}
};
let glob_target = if raw_target == "super" || raw_target.starts_with("super::") {
if let Some(ref mod_path) = mod_path_opt {
let mut parts: Vec<&str> = mod_path.split("::").collect();
let mut cur = raw_target.as_str();
while let Some(rest) = cur.strip_prefix("super::") {
if parts.len() > 1 {
parts.pop();
}
cur = rest;
}
if cur == "super" && parts.len() > 1 {
parts.pop();
parts.join("::")
} else if cur != "super" {
format!("{}::{cur}", parts.join("::"))
} else {
parts.join("::")
}
} else {
raw_target
}
} else if let Some(rest) = raw_target.strip_prefix("crate::") {
format!("{cname}::{rest}")
} else {
raw_target
};
if !glob_target.is_empty() && seen_globs.insert(glob_target.clone()) {
let probe = format!("{glob_target}::{first_seg}");
if let Some(mut found) = self.find_definition_by_path(&probe) {
if found.kind == DefKind::Use {
let target = found
.signature
.and_then(|sig| self.find_definition_by_path(sig))
.or_else(|| self.find_indexed_definition_at(found.file, found.start_line, found.start_col));
if let Some(target) = target {
if target.kind != DefKind::Local && target.kind != DefKind::Use {
found = target;
} else {
continue;
}
} else {
continue;
}
}
if let Some(rest) = rest_opt {
if let Some(sub) = self.find_definition_by_path(&format!("{}::{rest}", found.path)) {
return Some(sub);
}
} else {
return Some(found);
}
}
}
}
}
}
}
}
}
// 2. Crate scope: check enclosing module (<module_path>::<qual>) and crate root (<crate>::<qual>)
if let Some(idx) = crate_idx {
let cname = self.readers[idx].crate_name();
if !is_sysroot && !is_dep_crate {
// Try enclosing module
if let Some(ref mod_path) = mod_path_opt {
if let Some(def) = self.find_definition_by_path(&format!("{mod_path}::{qual_trimmed}")) {
if def.kind != DefKind::Use {
return Some(def);
}
}
}
// Try crate root
if let Some(def) = self.find_definition_by_path(&format!("{cname}::{qual_trimmed}")) {
if def.kind != DefKind::Use {
return Some(def);
}
}
}
}
// 3. Prelude: standard library prelude 2021/2024 types and traits
for candidate in prelude_candidate_paths(first_seg) {
if let Some(cand) = self.find_definition_by_path(candidate) {
if let Some(rest) = rest_opt {
if let Some(sub) = self.find_definition_by_path(&format!("{}::{rest}", cand.path)) {
return Some(sub);
}
} else {
return Some(cand);
}
}
}
// 4. External dependencies: check if `first_seg` is a loaded dependency crate (not sysroot)
if self.has_crate(first_seg) && first_seg != "core" && first_seg != "alloc" && first_seg != "std" {
let rest = rest_opt?;
if let Some(def) = self.find_definition_by_path(&format!("{first_seg}::{rest}")) {
return Some(def);
}
}
// 5. Sysroot: check standard library if explicitly requested or unshadowed
if qual_trimmed.starts_with("core::") || qual_trimmed.starts_with("alloc::") || qual_trimmed.starts_with("std::") || matches!(first_seg, "core" | "alloc" | "std") {
if let Some(def) = self.find_definition_by_path(qual_trimmed) {
return Some(def);
}
}
// 6. Fallback: single unique match across workspace readers (reject ambiguous matches)
let mut matched: Option<ResolvedDefinition<'a>> = None;
for (i, reader) in self.readers.iter().enumerate() {
if !qual_trimmed.contains("::") && Some(i) != crate_idx && reader.crate_name() != qual_trimmed {
continue;
}
let is_inaccessible = !reader_indices.contains(&i) && self.target_kinds.get(i).copied().flatten().map(|k| !k.is_lib()).unwrap_or(false);
if is_inaccessible {
continue;
}
let prefix = format!("{}::", reader.crate_name());
let sub = qual_trimmed.strip_prefix(&prefix).unwrap_or(qual_trimmed);
if let Some(def) = reader.find_definition_by_path(qual_trimmed).or_else(|| reader.find_definition_by_path(sub)) {
if DefKind::from(def.kind) == DefKind::Use {
continue;
}
if matched.is_some() {
return None;
}
matched = Some(self.make_resolved_definition(i, def));
}
}
matched
}
}