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).clone())
12 }
13
14 /// Re-analyze every transitive dependent of `file`.
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. Concurrent callers may run sweeps simultaneously;
19 /// each returns its 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 if cancel.is_cancelled() {
46 return Vec::new();
47 }
48
49 // Phase 1: compute dependents outside the analysis loop.
50 let dependents = self.dependency_graph().transitive_dependents(file);
51 if dependents.is_empty() {
52 return Vec::new();
53 }
54 let dependents: Vec<Arc<str>> = dependents
55 .into_iter()
56 .map(|path| Arc::from(path.as_str()))
57 .collect();
58 self.reanalyze_file_set(dependents, cancel)
59 }
60
61 /// Re-analyze an explicit file set — typically the editor's currently
62 /// open files — after an edit elsewhere in the workspace.
63 ///
64 /// This is the rust-analyzer diagnostics model: instead of computing the
65 /// edited file's transitive dependents (an O(all-ingested-files) graph
66 /// rebuild on every keystroke), the caller passes the handful of files it
67 /// actually publishes diagnostics for, and salsa memoization makes the
68 /// unaffected ones ~free — `analyze_file` re-validates each file's memo
69 /// against what actually changed and only re-executes bodies the edit
70 /// reaches. Per-edit cost is O(open files), independent of workspace size.
71 ///
72 /// Files the session has no source for are silently skipped. Cancellation
73 /// semantics match [`Self::reanalyze_dependents_cancellable`].
74 pub fn reanalyze_files_cancellable(
75 &self,
76 files: &[Arc<str>],
77 cancel: &crate::IndexCancel,
78 ) -> Vec<(Arc<str>, crate::FileAnalysis)> {
79 if cancel.is_cancelled() || files.is_empty() {
80 return Vec::new();
81 }
82 self.settle_workspace_index();
83 self.reanalyze_file_set(files.to_vec(), cancel)
84 }
85
86 /// Shared body of [`Self::reanalyze_dependents_cancellable`] and
87 /// [`Self::reanalyze_files_cancellable`]: warm up, analyze in parallel,
88 /// commit reference locations.
89 fn reanalyze_file_set(
90 &self,
91 files: Vec<Arc<str>>,
92 cancel: &crate::IndexCancel,
93 ) -> Vec<(Arc<str>, crate::FileAnalysis)> {
94 let dependents = files;
95
96 // Phase 2a: fault in each dependent's direct class references if the
97 // background indexer hasn't reached them yet (mirrors the FileAnalyzer
98 // warm-up behavior, avoiding transient false `UndefinedClass` during
99 // index warm-up).
100 //
101 // This runs SERIALLY and *before* the parallel analyze loop below:
102 // `prepare_ast_for_analysis` resolves and loads classes, and loading
103 // mutates the shared session salsa storage (`load_class` →
104 // `ingest_file` sets salsa inputs). Salsa input mutation cancels and
105 // blocks until every other database handle is released, so it must run
106 // with NO live snapshot in scope:
107 //
108 // - in parallel (the v0.37.0 regression), sibling rayon workers held
109 // live snapshot clones mid-`analyze_file`, so the first warm-up
110 // write blocked on them forever — under high dependent fan-out this
111 // deadlocked the whole runtime; and
112 // - even serially, a snapshot held across the loop (e.g. one taken to
113 // parse the dependents) blocks the very first write.
114 //
115 // `prepare_file_for_analysis` takes a *scoped* snapshot to fetch the
116 // parsed AST, drops it (the `Arc<ParseResult>` is owned), and only
117 // then warms up. Files already prepared against their current text
118 // skip the parse + AST walk entirely — hosts on the
119 // `ingest_file_prepared` write path pre-pay this per edit, making the
120 // whole loop a map-lookup sweep.
121 {
122 // One revision bump for the whole warm-up sweep instead of one per
123 // lazily-loaded class (each bump cancels every in-flight salsa
124 // reader). Closed before `commit_gen` is read below, so commits
125 // are stamped with the post-load generation as before.
126 let _deferred_bumps = self.defer_revision_bumps();
127 for file in &dependents {
128 if cancel.is_cancelled() {
129 return Vec::new();
130 }
131 self.prepare_file_for_analysis(file);
132 }
133 }
134
135 // Phase 2b: drive each dependent through the `analyze_file` tracked
136 // query. Salsa's memo validation does the real work here: after a
137 // body-only edit, a dependent whose tracked inputs are structurally
138 // unchanged (`FileDefinitions` backdating) returns its cached output
139 // without re-running body analysis — re-analysis cost scales with
140 // what actually changed, not with dependent count.
141 //
142 // The snapshot is taken AFTER the warm-up above so the loop observes
143 // the freshly-loaded classes. This loop is read-only on salsa: it does
144 // not mutate inputs, so its snapshot never contends on a write.
145 //
146 // Keep this loop on the caller thread. A request may already be one
147 // of several concurrent host threads, while background indexing uses
148 // the shared Rayon pool. Sending each sweep back through that pool
149 // creates a pool-fan-in deadlock: every worker can block on a writer
150 // while the thread that would release it waits for a worker. It also
151 // prevents a cancelled request from making progress. The caller-level
152 // concurrency preserves throughput without that dependency.
153 //
154 // Dependents' `FileAnalysis::symbols` are empty on this path:
155 // per-expression symbols are intentionally not memoized (a typical
156 // file resolves thousands; caching them balloons memory), and
157 // diagnostics consumers don't read them. Hover / go-to-definition
158 // flows analyze the open file directly via [`crate::FileAnalyzer`].
159 //
160 // The loop short-circuits when cancellation has been requested.
161 // Generation before the snapshot: a file add racing the sweep leaves
162 // the commits stale (self-healing), never wrongly fresh.
163 let commit_gen = self.index_generation();
164 // Freeze on the pass-scoped snapshot: warm-up (2a) completed every
165 // lazy load, and a concurrent index write cancels the pass, so the
166 // frozen view is never stale. Same discipline as the batch body pass.
167 let mut db_main = self.snapshot_db();
168 db_main.freeze_workspace_index();
169 // Sweeps are the steady-state population path for the mention index:
170 // every analyzed file gets a current mention scan alongside its
171 // postings, so later reference-gate checks are set lookups.
172 let mention_scanner = db_main.class_mention_scanner();
173 type Analyzed = (
174 Arc<str>,
175 Arc<str>,
176 std::sync::Arc<crate::db::AnalyzeOutput>,
177 Vec<crate::db::SubtypeEntry>,
178 Option<super::RefCachePut>,
179 Option<Box<[mir_types::Name]>>,
180 );
181 let mut results: Vec<Analyzed> = dependents
182 .into_iter()
183 .filter_map(|file| {
184 if cancel.is_cancelled() {
185 return None;
186 }
187 let sf = db_main.lookup_source_file(file.as_ref())?;
188 // Capture the text the analysis ran against: the freshness
189 // marks below must record exactly this Arc, so a text write
190 // racing the sweep leaves the file dirty rather than
191 // wrongly marked fresh.
192 let text = sf.text(&db_main as &dyn crate::db::MirDatabase).clone();
193 let out =
194 crate::db::analyze_file(&db_main as &dyn crate::db::MirDatabase, sf).clone();
195 let defs = crate::db::collect_file_definitions(
196 &db_main as &dyn crate::db::MirDatabase,
197 sf,
198 );
199 let entries = crate::db::subtype_index::entries_from_slice(&defs.slice);
200 // Stage the disk-cache write only when the postings commit
201 // below will actually rewrite — a no-op re-sweep (current
202 // commit) adds no hashing or parse-walk cost per file.
203 let put = if self.ref_commit_is_current(file.as_ref(), &text, &out) {
204 None
205 } else {
206 self.stage_ref_cache_put(
207 &db_main as &dyn crate::db::MirDatabase,
208 sf,
209 file.as_ref(),
210 &text,
211 &out,
212 )
213 };
214 let mentions = mention_scanner.as_ref().and_then(|s| {
215 (!db_main.class_mentions_current(file.as_ref(), &text, s.epoch()))
216 .then(|| s.scan(&text))
217 });
218 Some((file, text, out, entries, put, mentions))
219 })
220 .collect();
221
222 // Serial commit: each dependent's output is its complete reference
223 // set, so replace rather than append. Both inverted indexes and their
224 // freshness marks update here — this is what keeps read queries
225 // lookup-shaped instead of re-validating every candidate memo.
226 // Unchanged files (same text, same memoized output) skip the rebuild
227 // entirely, so a no-op re-sweep is a pointer compare per file.
228 {
229 let guard = self.db.salsa.read();
230 let mut dependency_graph_changed = false;
231 for (file, text, out, entries, put, mentions) in results.iter_mut() {
232 // Pointer-identical memo ⇒ identical postings: skip the
233 // index rewrite. The mark is re-stamped unconditionally so a
234 // no-op sweep still advances the commit's generation.
235 if !self.ref_commit_is_current(file.as_ref(), text, out) {
236 guard.set_file_reference_locations(file.as_ref(), out.ref_locs.to_vec());
237 dependency_graph_changed = true;
238 }
239 if let (Some(s), Some(m)) = (&mention_scanner, mentions.take()) {
240 guard.set_file_class_mentions(file, text, s.epoch(), m);
241 }
242 if let Some(put) = put.take() {
243 self.apply_ref_cache_put(file.as_ref(), out, put);
244 }
245 self.mark_ref_committed(
246 file,
247 text,
248 Some(out),
249 commit_gen,
250 !out.has_unresolved_names(),
251 );
252 if !self.is_defs_committed(file.as_ref(), text) {
253 guard.set_file_class_edges(file, entries.clone());
254 self.mark_defs_committed(file, text);
255 dependency_graph_changed = true;
256 }
257 }
258 if dependency_graph_changed {
259 self.clear_dependency_graph_cache();
260 }
261 }
262
263 results
264 .into_iter()
265 .map(|(file, _, out, _, _, _)| {
266 (
267 file,
268 crate::FileAnalysis {
269 issues: out.issues.to_vec(),
270 symbols: Vec::new(),
271 },
272 )
273 })
274 .collect()
275 }
276
277 /// FQCNs that `file` imports via `use` statements but that aren't yet
278 /// loaded in the session.
279 ///
280 /// Designed as the input to background prefetching: after the LSP server
281 /// Return the `use`-import alias map for a file: a list of `(alias, fqcn)`
282 /// pairs where `alias` is the local name (e.g. `"Str"`) and `fqcn` is the
283 /// fully-qualified name (e.g. `"Illuminate\\Support\\Str"`).
284 ///
285 /// Completion handlers can use this to expand a short class name written
286 /// before `::` into its FQN before looking up static members, mirroring the
287 /// same alias expansion that go-to-definition already performs via
288 /// `symbol_at` + `definition_of`.
289 ///
290 /// Returns an empty Vec if the file has not been ingested or has no use
291 /// imports.
292 pub fn class_imports(&self, file: &str) -> Vec<(Arc<str>, Arc<str>)> {
293 let db = self.snapshot_db();
294 let imports = db.file_class_imports(file);
295 imports
296 .iter()
297 .map(|(alias, fqcn)| (Arc::from(alias.as_str()), Arc::from(fqcn.as_str())))
298 .collect()
299 }
300
301 /// ingests an open buffer, it can call this and lazy-load the returned
302 /// FQCNs on a worker thread so the user's first Cmd+Click into vendor
303 /// code doesn't pay the file-read+parse cost.
304 ///
305 /// Returns an empty Vec if the file hasn't been ingested or has no
306 /// unresolved imports.
307 pub fn pending_lazy_loads(&self, file: &str) -> Vec<Arc<str>> {
308 use rustc_hash::FxHashSet;
309
310 let db = self.snapshot_db();
311 let imports = db.file_imports(file);
312 if imports.is_empty() {
313 return Vec::new();
314 }
315 let mut out = Vec::new();
316 let mut seen: FxHashSet<Arc<str>> = FxHashSet::default();
317 for fqcn in imports.values() {
318 let here = crate::db::Fqcn::interned(&db, *fqcn);
319 if crate::db::find_class_like(&db, here).is_some() {
320 continue;
321 }
322 if let Some(resolver) = &self.resolver {
323 let fqcn_arc: Arc<str> = Arc::from(fqcn.as_str());
324 if !seen.insert(fqcn_arc.clone()) {
325 continue;
326 }
327 if resolver.resolve(fqcn.as_str()).is_some() {
328 out.push(fqcn_arc);
329 }
330 }
331 }
332 out
333 }
334
335 /// Convenience: synchronously lazy-load every import of `file` that
336 /// isn't already in the codebase. Returns the number successfully loaded.
337 ///
338 /// For non-blocking prefetch, call this from a worker thread:
339 ///
340 /// ```ignore
341 /// let s = session.clone(); // AnalysisSession is wrapped in Arc by callers
342 /// std::thread::spawn(move || {
343 /// s.prefetch_imports(&file_path);
344 /// });
345 /// ```
346 ///
347 /// Uses a single shared-visited two-tier BFS across all pending imports
348 /// (see [`Self::load_classes_transitive_bounded`]) with a shallow depth so
349 /// member access on imported types type-checks without pulling in the
350 /// entire vendor tree.
351 pub fn prefetch_imports(&self, file: &str) -> usize {
352 let pending = self.pending_lazy_loads(file);
353 if pending.is_empty() {
354 return 0;
355 }
356 // Fault in each imported FQCN directly (single-file load + tier-merge).
357 // Inheritance ancestors / signature types resolve through the eagerly
358 // built workspace symbol index — no transitive walk needed here.
359 let _deferred_bumps = self.defer_revision_bumps();
360 let mut loaded = 0;
361 for fqcn in &pending {
362 if self.load_class(fqcn.as_ref()).is_loaded() {
363 loaded += 1;
364 }
365 }
366 loaded
367 }
368
369 /// All class / interface / trait / enum FQCNs currently known to the
370 /// session, each paired with the file that defines them when available.
371 ///
372 /// Use this to build workspace-wide views (outline, fuzzy search, etc.).
373 /// Consumers implement their own search/match logic on top — the analyzer
374 /// only exposes the iterator.
375 pub fn all_classes(&self) -> Vec<(Arc<str>, Option<mir_types::Location>)> {
376 let db = self.snapshot_db();
377 crate::db::workspace_classes(&db)
378 .iter()
379 .filter_map(|fqcn| {
380 let here = crate::db::Fqcn::from_str(&db, fqcn.as_str());
381 crate::db::find_class_like(&db, here)
382 .map(|class| (Arc::<str>::from(fqcn.as_str()), class.location().cloned()))
383 })
384 .collect()
385 }
386
387 /// All global function FQNs currently known to the session, each paired
388 /// with their declaration location when available.
389 pub fn all_functions(&self) -> Vec<(Arc<str>, Option<mir_types::Location>)> {
390 let db = self.snapshot_db();
391 crate::db::workspace_functions(&db)
392 .iter()
393 .filter_map(|fqn| {
394 let here = crate::db::Fqcn::from_str(&db, fqn.as_str());
395 crate::db::find_function(&db, here)
396 .map(|f| (Arc::<str>::from(fqn.as_str()), f.location.clone()))
397 })
398 .collect()
399 }
400
401 /// Every ancestor of `fqcn` — extended class, implemented interfaces,
402 /// used traits, transitively — most-derived first. Does not include
403 /// `fqcn` itself. Memoized per FQCN (shares `class_ancestors_by_fqcn`'s
404 /// tracked cache with member-resolution paths).
405 pub fn ancestors_of(&self, fqcn: &str) -> Vec<Arc<str>> {
406 let db = self.snapshot_db();
407 let here = crate::db::Fqcn::from_str(&db, fqcn.trim_start_matches('\\'));
408 crate::db::class_ancestors_by_fqcn(&db, here)
409 .iter()
410 .skip(1)
411 .cloned()
412 .collect()
413 }
414
415 /// Full signature for a global function, resolved by FQN.
416 pub fn function_signature(&self, fqn: &str) -> Option<Arc<crate::FunctionDef>> {
417 let db = self.snapshot_db();
418 let here = crate::db::Fqcn::from_str(&db, fqn);
419 crate::db::find_function(&db, here)
420 }
421
422 /// Compute `file`'s outgoing dependency edges and persist them to the
423 /// disk cache's reverse-dep graph (if configured). The in-memory graph
424 /// is no longer maintained imperatively: `dependency_graph()` derives
425 /// structural symbols from the memoized [`crate::db::file_structural_symbols`]
426 /// tracked query, then projects them through the workspace symbol index
427 /// alongside body-reference symbols.
428 pub(super) fn update_reverse_deps_for(&self, file: &str) {
429 if let Some(cache) = self.cache.as_deref() {
430 let db = self.snapshot_db();
431 let targets = file_outgoing_dependencies(&db, file, true);
432 cache.update_reverse_deps_for_file(file, &targets);
433 }
434 }
435
436 /// File dependency graph: which files depend on which other files.
437 /// Used for incremental invalidation in LSP servers and build systems.
438 ///
439 /// File dependency graph: which files depend on which other files.
440 /// Used for incremental invalidation in LSP servers and build systems.
441 ///
442 /// O(edges) — iterates symbol edges from the reference index and from
443 /// `file_structural_symbols`, then resolves each symbol to its defining
444 /// file via O(1) lookup. Total cost is O(E) where E is the number of
445 /// (file, symbol) edges.
446 pub fn dependency_graph(&self) -> crate::DependencyGraph {
447 if let Some(graph) = self.dependency_graph_cache.read().as_ref() {
448 return graph.clone();
449 }
450 let db = self.snapshot_db();
451
452 fn push_edge(
453 dependencies: &mut [Vec<u32>],
454 dependents: &mut [Vec<u32>],
455 from: u32,
456 to: u32,
457 ) {
458 if from == to {
459 return;
460 }
461 dependents[to as usize].push(from);
462 dependencies[from as usize].push(to);
463 }
464
465 fn sort_and_dedup(adjacency: &mut [Vec<u32>]) {
466 for deps in adjacency {
467 deps.sort();
468 deps.dedup();
469 }
470 }
471
472 fn symbol_defining_file_id(
473 db: &dyn crate::db::MirDatabase,
474 file_ids: &HashMap<Arc<str>, u32>,
475 symbol_key: &str,
476 ) -> Option<u32> {
477 let lookup = crate::defining_file_lookup_key(symbol_key);
478 db.symbol_defining_file(lookup)
479 .and_then(|file| file_ids.get(file.as_ref()).copied())
480 }
481
482 let mut all_files: Vec<Arc<str>> = db.source_file_paths().to_vec();
483 all_files.sort();
484 assert!(
485 u32::try_from(all_files.len()).is_ok(),
486 "dependency graph file ids require at most u32::MAX files"
487 );
488 let file_ids: HashMap<Arc<str>, u32> = all_files
489 .iter()
490 .enumerate()
491 .map(|(id, file)| (file.clone(), id as u32))
492 .collect();
493
494 let mut dependencies = vec![Vec::new(); all_files.len()];
495 let mut dependents = vec![Vec::new(); all_files.len()];
496 for (file_id, file) in all_files.iter().enumerate() {
497 let file_id = file_id as u32;
498 let mut file_deps: HashSet<u32> = HashSet::default();
499
500 // O(degree(file)) — forward reference-index lookup, no full-table scan.
501 for symbol_key in db.file_referenced_symbols(file.as_ref()) {
502 if let Some(def_id) = symbol_defining_file_id(&db, &file_ids, &symbol_key) {
503 file_deps.insert(def_id);
504 }
505 }
506
507 // Declaration-level symbol edges from Salsa. These cover imports,
508 // class hierarchy edges, and type-hint-only references that never
509 // appear in file_referenced_symbols.
510 if let Some(sf) = db.lookup_source_file(file.as_ref()) {
511 for symbol in crate::db::file_structural_symbols(&db, sf).iter() {
512 if let Some(def_id) = symbol_defining_file_id(&db, &file_ids, symbol) {
513 file_deps.insert(def_id);
514 }
515 }
516 }
517
518 for dep_id in file_deps {
519 push_edge(&mut dependencies, &mut dependents, file_id, dep_id);
520 }
521 }
522
523 sort_and_dedup(&mut dependents);
524 sort_and_dedup(&mut dependencies);
525
526 // Augment with stale dependents: files referencing symbols that were
527 // deleted from their defining file. These edges disappear from the
528 // symbol_defining_file lookup but the referencing file still needs
529 // re-analysis to surface the now-broken reference.
530 {
531 let stale = self.stale_defined_symbols.read();
532 if !stale.is_empty() {
533 for (file, deleted_syms) in stale.iter() {
534 let Some(&file_id) = file_ids.get(file.as_str()) else {
535 continue;
536 };
537 for sym in deleted_syms {
538 let lookup = crate::defining_file_lookup_key(sym);
539 // `defined_symbols()` only yields top-level FQ names
540 // (classes/interfaces/traits/enums, functions, global
541 // constants) — never knows here which kind `sym` was,
542 // so probe every prefix the reference index actually
543 // uses (see `Name::codebase_key`) rather than guessing
544 // one and silently missing referencers of the others.
545 for prefix in ["cls:", "fn:", "gcnst:"] {
546 for referencing_file in
547 db.symbol_referencers_of(&format!("{prefix}{lookup}"))
548 {
549 if let Some(&ref_id) = file_ids.get(referencing_file.as_ref()) {
550 push_edge(&mut dependencies, &mut dependents, ref_id, file_id);
551 }
552 }
553 }
554 }
555 }
556 // Re-sort and dedup since we may have added entries.
557 sort_and_dedup(&mut dependents);
558 sort_and_dedup(&mut dependencies);
559 }
560 }
561
562 let graph = crate::DependencyGraph::from_compact_parts(
563 all_files,
564 file_ids,
565 dependencies,
566 dependents,
567 );
568 *self.dependency_graph_cache.write() = Some(graph.clone());
569 graph
570 }
571}