mir_analyzer/session/incremental.rs
1use super::*;
2
3impl AnalysisSession {
4 /// Retrieve the source text the session has registered for `file`, if
5 /// any. Returns `None` when the file has never been ingested. Used by
6 /// the parallel re-analysis path to re-feed dependents to body analysis without
7 /// the caller having to track sources independently.
8 pub fn source_of(&self, file: &str) -> Option<Arc<str>> {
9 let db = self.snapshot_db();
10 let sf = db.lookup_source_file(file)?;
11 Some(sf.text(&db))
12 }
13
14 /// Re-analyze every transitive dependent of `file` in parallel.
15 ///
16 /// When the user saves a file that other files depend on (e.g. editing
17 /// a base class, an interface, or a trait), those dependents may have
18 /// new diagnostics. This method computes them in parallel using rayon
19 /// and returns the per-file analysis results so the LSP server can
20 /// publish updated diagnostics in one batch.
21 ///
22 /// Source text for dependents is retrieved from the session's salsa
23 /// inputs (set by previous `ingest_file` calls) — the caller doesn't
24 /// need to track or re-read files. Files for which the session has no
25 /// source are silently skipped (returns the analyzable subset).
26 ///
27 /// Cross-file inferred return types are resolved on demand via salsa.
28 pub fn reanalyze_dependents(&self, file: &str) -> Vec<(Arc<str>, crate::FileAnalysis)> {
29 self.reanalyze_dependents_cancellable(file, &crate::IndexCancel::new())
30 }
31
32 /// Cancellable variant of [`Self::reanalyze_dependents`].
33 ///
34 /// The consumer flips `cancel` (typically because a newer edit arrived) to
35 /// abandon the re-analysis; the flag is checked at each file boundary. Salsa
36 /// cannot unwind the plain-Rust body-analysis walk mid-flight, so a file
37 /// already in progress finishes, but no further files are started. Files
38 /// skipped due to cancellation are simply absent from the returned vec —
39 /// the consumer should drop a stale flag and start fresh work on each edit.
40 pub fn reanalyze_dependents_cancellable(
41 &self,
42 file: &str,
43 cancel: &crate::IndexCancel,
44 ) -> Vec<(Arc<str>, crate::FileAnalysis)> {
45 use rayon::prelude::*;
46
47 if cancel.is_cancelled() {
48 return Vec::new();
49 }
50
51 // Phase 1: compute dependents outside the analysis loop.
52 let dependents = self.dependency_graph().transitive_dependents(file);
53 if dependents.is_empty() {
54 return Vec::new();
55 }
56 let dependents: Vec<Arc<str>> = dependents
57 .into_iter()
58 .map(|path| Arc::from(path.as_str()))
59 .collect();
60
61 // Phase 2a: fault in each dependent's direct class references if the
62 // background indexer hasn't reached them yet (mirrors the FileAnalyzer
63 // warm-up behavior, avoiding transient false `UndefinedClass` during
64 // index warm-up).
65 //
66 // This runs SERIALLY and *before* the parallel analyze loop below:
67 // `prepare_ast_for_analysis` resolves and loads classes, and loading
68 // mutates the shared session salsa storage (`load_class` →
69 // `ingest_file` sets salsa inputs). Salsa input mutation cancels and
70 // blocks until every other database handle is released, so it must run
71 // with NO live snapshot in scope:
72 //
73 // - in parallel (the v0.37.0 regression), sibling rayon workers held
74 // live snapshot clones mid-`analyze_file`, so the first warm-up
75 // write blocked on them forever — under high dependent fan-out this
76 // deadlocked the whole runtime; and
77 // - even serially, a snapshot held across the loop (e.g. one taken to
78 // parse the dependents) blocks the very first write.
79 //
80 // So each iteration takes a *scoped* snapshot to fetch the parsed AST,
81 // drops it (the `Arc<ParseResult>` is owned), and only then warms up.
82 for file in &dependents {
83 if cancel.is_cancelled() {
84 return Vec::new();
85 }
86 let parsed = {
87 let db = self.snapshot_db();
88 let Some(sf) = db.lookup_source_file(file.as_ref()) else {
89 continue;
90 };
91 crate::db::parse_file(&db as &dyn crate::db::MirDatabase, sf).0
92 };
93 self.prepare_ast_for_analysis(&parsed.program, file.as_ref());
94 }
95
96 // Phase 2b: drive each dependent through the `analyze_file` tracked
97 // query in parallel. Salsa's memo validation does the real work
98 // here: after a body-only edit, a dependent whose tracked inputs are
99 // structurally unchanged (`FileDefinitions` backdating) returns its
100 // cached output without re-running body analysis — re-analysis cost
101 // scales with what actually changed, not with dependent count.
102 //
103 // The snapshot is taken AFTER the warm-up above so each worker observes
104 // the freshly-loaded classes. This loop is read-only on salsa: no
105 // worker mutates inputs, so the snapshots never contend on a write.
106 //
107 // Dependents' `FileAnalysis::symbols` are empty on this path:
108 // per-expression symbols are intentionally not memoized (a typical
109 // file resolves thousands; caching them balloons memory), and
110 // diagnostics consumers don't read them. Hover / go-to-definition
111 // flows analyze the open file directly via [`crate::FileAnalyzer`].
112 //
113 // Each worker short-circuits when cancellation has been requested.
114 let db_main = self.snapshot_db();
115 let results: Vec<(Arc<str>, std::sync::Arc<crate::db::AnalyzeOutput>)> = dependents
116 .into_par_iter()
117 .map_with(db_main, |db, file| {
118 if cancel.is_cancelled() {
119 return None;
120 }
121 let sf = db.lookup_source_file(file.as_ref())?;
122 let out = crate::db::analyze_file(&*db as &dyn crate::db::MirDatabase, sf);
123 Some((file, out))
124 })
125 .flatten()
126 .collect();
127
128 // Serial commit: each dependent's output is its complete reference
129 // set, so replace rather than append.
130 {
131 let guard = self.db.salsa.read();
132 for (file, out) in &results {
133 guard.set_file_reference_locations(file.as_ref(), out.ref_locs.to_vec());
134 }
135 }
136
137 results
138 .into_iter()
139 .map(|(file, out)| {
140 (
141 file,
142 crate::FileAnalysis {
143 issues: out.issues.to_vec(),
144 symbols: Vec::new(),
145 },
146 )
147 })
148 .collect()
149 }
150
151 /// FQCNs that `file` imports via `use` statements but that aren't yet
152 /// loaded in the session.
153 ///
154 /// Designed as the input to background prefetching: after the LSP server
155 /// ingests an open buffer, it can call this and lazy-load the returned
156 /// FQCNs on a worker thread so the user's first Cmd+Click into vendor
157 /// code doesn't pay the file-read+parse cost.
158 ///
159 /// Returns an empty Vec if the file hasn't been ingested or has no
160 /// unresolved imports.
161 pub fn pending_lazy_loads(&self, file: &str) -> Vec<Arc<str>> {
162 let db = self.snapshot_db();
163 let imports = db.file_imports(file);
164 if imports.is_empty() {
165 return Vec::new();
166 }
167 let mut out = Vec::new();
168 for fqcn in imports.values() {
169 let here = crate::db::Fqcn::new(&db, *fqcn);
170 if crate::db::find_class_like(&db, here).is_some() {
171 continue;
172 }
173 if let Some(resolver) = &self.resolver {
174 if resolver.resolve(fqcn.as_str()).is_some() {
175 out.push(Arc::from(fqcn.as_str()));
176 }
177 }
178 }
179 out
180 }
181
182 /// Convenience: synchronously lazy-load every import of `file` that
183 /// isn't already in the codebase. Returns the number successfully loaded.
184 ///
185 /// For non-blocking prefetch, call this from a worker thread:
186 ///
187 /// ```ignore
188 /// let s = session.clone(); // AnalysisSession is wrapped in Arc by callers
189 /// std::thread::spawn(move || {
190 /// s.prefetch_imports(&file_path);
191 /// });
192 /// ```
193 ///
194 /// Uses a single shared-visited two-tier BFS across all pending imports
195 /// (see [`Self::load_classes_transitive_bounded`]) with a shallow depth so
196 /// member access on imported types type-checks without pulling in the
197 /// entire vendor tree.
198 pub fn prefetch_imports(&self, file: &str) -> usize {
199 let pending = self.pending_lazy_loads(file);
200 if pending.is_empty() {
201 return 0;
202 }
203 // Fault in each imported FQCN directly (single-file load + tier-merge).
204 // Inheritance ancestors / signature types resolve through the eagerly
205 // built workspace symbol index — no transitive walk needed here.
206 let mut loaded = 0;
207 for fqcn in &pending {
208 if self.load_class(fqcn.as_ref()).is_loaded() {
209 loaded += 1;
210 }
211 }
212 loaded
213 }
214
215 /// All class / interface / trait / enum FQCNs currently known to the
216 /// session, each paired with the file that defines them when available.
217 ///
218 /// Use this to build workspace-wide views (outline, fuzzy search, etc.).
219 /// Consumers implement their own search/match logic on top — the analyzer
220 /// only exposes the iterator.
221 pub fn all_classes(&self) -> Vec<(Arc<str>, Option<mir_types::Location>)> {
222 let db = self.snapshot_db();
223 crate::db::workspace_classes(&db)
224 .iter()
225 .filter_map(|fqcn| {
226 let here = crate::db::Fqcn::from_str(&db, fqcn.as_ref());
227 crate::db::find_class_like(&db, here)
228 .map(|class| (fqcn.clone(), class.location().cloned()))
229 })
230 .collect()
231 }
232
233 /// All global function FQNs currently known to the session, each paired
234 /// with their declaration location when available.
235 pub fn all_functions(&self) -> Vec<(Arc<str>, Option<mir_types::Location>)> {
236 let db = self.snapshot_db();
237 crate::db::workspace_functions(&db)
238 .iter()
239 .filter_map(|fqn| {
240 let here = crate::db::Fqcn::from_str(&db, fqn.as_ref());
241 crate::db::find_function(&db, here).map(|f| (fqn.clone(), f.location.clone()))
242 })
243 .collect()
244 }
245
246 /// Compute `file`'s outgoing dependency edges and persist them to the
247 /// disk cache's reverse-dep graph (if configured). The in-memory graph
248 /// is no longer maintained imperatively: `dependency_graph()` derives
249 /// structural edges from the memoized [`crate::db::file_structural_deps`]
250 /// tracked query, so there is no second copy to drift out of sync.
251 pub(super) fn update_reverse_deps_for(&self, file: &str) {
252 if let Some(cache) = self.cache.as_deref() {
253 let db = self.snapshot_db();
254 let targets = file_outgoing_dependencies(&db, file);
255 cache.update_reverse_deps_for_file(file, &targets);
256 }
257 }
258
259 /// File dependency graph: which files depend on which other files.
260 /// Used for incremental invalidation in LSP servers and build systems.
261 ///
262 /// File dependency graph: which files depend on which other files.
263 /// Used for incremental invalidation in LSP servers and build systems.
264 ///
265 /// O(edges) — iterates the `file_references` forward index (file → symbol
266 /// keys it references) which is always current, then resolves each symbol
267 /// to its defining file via O(1) lookup. Total cost is O(E) where E is the
268 /// number of (file, symbol) reference edges, vs. the old O(F × S × R) scan.
269 pub fn dependency_graph(&self) -> crate::DependencyGraph {
270 let db = self.snapshot_db();
271
272 let all_files: Vec<String> = db
273 .source_file_paths()
274 .iter()
275 .map(|f| f.as_ref().to_string())
276 .collect();
277
278 let mut dependencies: HashMap<String, Vec<String>> = HashMap::default();
279 let mut dependents: HashMap<String, Vec<String>> = HashMap::default();
280
281 for file in &all_files {
282 // O(degree(file)) — forward index lookup, no full-table scan.
283 let symbol_keys = db.file_referenced_symbols(file);
284 let mut file_deps: HashSet<String> = HashSet::default();
285 for symbol_key in &symbol_keys {
286 let lookup: &str = match symbol_key.split_once("::") {
287 Some((class, _)) => class,
288 None => symbol_key.as_ref(),
289 };
290 if let Some(def_file) = db.symbol_defining_file(lookup) {
291 let def = def_file.as_ref().to_string();
292 if &def != file {
293 file_deps.insert(def);
294 }
295 }
296 }
297 for dep in &file_deps {
298 dependents
299 .entry(dep.clone())
300 .or_default()
301 .push(file.clone());
302 dependencies
303 .entry(file.clone())
304 .or_default()
305 .push(dep.clone());
306 }
307 }
308
309 // Merge structural deps derived from definition collection. The
310 // forward pass above only captures bare-FQN references recorded
311 // during body analysis; `file_structural_deps` covers imports, class
312 // hierarchy (extends/implements/use), and type-hint-only references
313 // that never appear in file_referenced_symbols. The query is salsa-
314 // memoized, so the warm rebuild costs one map lookup per file rather
315 // than a definition walk — and there is no imperatively-maintained
316 // reverse map to drift out of sync with the definitions.
317 for file in &all_files {
318 let Some(sf) = db.lookup_source_file(file) else {
319 continue;
320 };
321 for target in crate::db::file_structural_deps(&db, sf).iter() {
322 let target = target.as_ref().to_string();
323 if &target != file {
324 dependents
325 .entry(target.clone())
326 .or_default()
327 .push(file.clone());
328 dependencies.entry(file.clone()).or_default().push(target);
329 }
330 }
331 }
332
333 for deps in dependents.values_mut() {
334 deps.sort();
335 deps.dedup();
336 }
337 for deps in dependencies.values_mut() {
338 deps.sort();
339 deps.dedup();
340 }
341
342 // Augment with stale dependents: files referencing symbols that were
343 // deleted from their defining file. These edges disappear from the
344 // symbol_defining_file lookup but the referencing file still needs
345 // re-analysis to surface the now-broken reference.
346 {
347 let stale = self.stale_defined_symbols.read();
348 if !stale.is_empty() {
349 for (file, deleted_syms) in stale.iter() {
350 for sym in deleted_syms {
351 let lookup: &str = match sym.split_once("::") {
352 Some((class, _)) => class,
353 None => sym.as_ref(),
354 };
355 for referencing_file in db.symbol_referencers_of(lookup) {
356 let ref_file = referencing_file.as_ref().to_string();
357 if &ref_file != file {
358 dependents
359 .entry(file.clone())
360 .or_default()
361 .push(ref_file.clone());
362 dependencies.entry(ref_file).or_default().push(file.clone());
363 }
364 }
365 }
366 }
367 // Re-sort and dedup since we may have added entries.
368 for deps in dependents.values_mut() {
369 deps.sort();
370 deps.dedup();
371 }
372 for deps in dependencies.values_mut() {
373 deps.sort();
374 deps.dedup();
375 }
376 }
377 }
378
379 crate::DependencyGraph {
380 dependencies,
381 dependents,
382 }
383 }
384}