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 /// Return the `use`-import alias map for a file: a list of `(alias, fqcn)`
156 /// pairs where `alias` is the local name (e.g. `"Str"`) and `fqcn` is the
157 /// fully-qualified name (e.g. `"Illuminate\\Support\\Str"`).
158 ///
159 /// Completion handlers can use this to expand a short class name written
160 /// before `::` into its FQN before looking up static members, mirroring the
161 /// same alias expansion that go-to-definition already performs via
162 /// `symbol_at` + `definition_of`.
163 ///
164 /// Returns an empty Vec if the file has not been ingested or has no use
165 /// imports.
166 pub fn class_imports(&self, file: &str) -> Vec<(Arc<str>, Arc<str>)> {
167 let db = self.snapshot_db();
168 let imports = db.file_imports(file);
169 imports
170 .iter()
171 .map(|(alias, fqcn)| (Arc::from(alias.as_str()), Arc::from(fqcn.as_str())))
172 .collect()
173 }
174
175 /// ingests an open buffer, it can call this and lazy-load the returned
176 /// FQCNs on a worker thread so the user's first Cmd+Click into vendor
177 /// code doesn't pay the file-read+parse cost.
178 ///
179 /// Returns an empty Vec if the file hasn't been ingested or has no
180 /// unresolved imports.
181 pub fn pending_lazy_loads(&self, file: &str) -> Vec<Arc<str>> {
182 let db = self.snapshot_db();
183 let imports = db.file_imports(file);
184 if imports.is_empty() {
185 return Vec::new();
186 }
187 let mut out = Vec::new();
188 for fqcn in imports.values() {
189 let here = crate::db::Fqcn::new(&db, *fqcn);
190 if crate::db::find_class_like(&db, here).is_some() {
191 continue;
192 }
193 if let Some(resolver) = &self.resolver {
194 if resolver.resolve(fqcn.as_str()).is_some() {
195 out.push(Arc::from(fqcn.as_str()));
196 }
197 }
198 }
199 out
200 }
201
202 /// Convenience: synchronously lazy-load every import of `file` that
203 /// isn't already in the codebase. Returns the number successfully loaded.
204 ///
205 /// For non-blocking prefetch, call this from a worker thread:
206 ///
207 /// ```ignore
208 /// let s = session.clone(); // AnalysisSession is wrapped in Arc by callers
209 /// std::thread::spawn(move || {
210 /// s.prefetch_imports(&file_path);
211 /// });
212 /// ```
213 ///
214 /// Uses a single shared-visited two-tier BFS across all pending imports
215 /// (see [`Self::load_classes_transitive_bounded`]) with a shallow depth so
216 /// member access on imported types type-checks without pulling in the
217 /// entire vendor tree.
218 pub fn prefetch_imports(&self, file: &str) -> usize {
219 let pending = self.pending_lazy_loads(file);
220 if pending.is_empty() {
221 return 0;
222 }
223 // Fault in each imported FQCN directly (single-file load + tier-merge).
224 // Inheritance ancestors / signature types resolve through the eagerly
225 // built workspace symbol index — no transitive walk needed here.
226 let mut loaded = 0;
227 for fqcn in &pending {
228 if self.load_class(fqcn.as_ref()).is_loaded() {
229 loaded += 1;
230 }
231 }
232 loaded
233 }
234
235 /// All class / interface / trait / enum FQCNs currently known to the
236 /// session, each paired with the file that defines them when available.
237 ///
238 /// Use this to build workspace-wide views (outline, fuzzy search, etc.).
239 /// Consumers implement their own search/match logic on top — the analyzer
240 /// only exposes the iterator.
241 pub fn all_classes(&self) -> Vec<(Arc<str>, Option<mir_types::Location>)> {
242 let db = self.snapshot_db();
243 crate::db::workspace_classes(&db)
244 .iter()
245 .filter_map(|fqcn| {
246 let here = crate::db::Fqcn::from_str(&db, fqcn.as_ref());
247 crate::db::find_class_like(&db, here)
248 .map(|class| (fqcn.clone(), class.location().cloned()))
249 })
250 .collect()
251 }
252
253 /// All global function FQNs currently known to the session, each paired
254 /// with their declaration location when available.
255 pub fn all_functions(&self) -> Vec<(Arc<str>, Option<mir_types::Location>)> {
256 let db = self.snapshot_db();
257 crate::db::workspace_functions(&db)
258 .iter()
259 .filter_map(|fqn| {
260 let here = crate::db::Fqcn::from_str(&db, fqn.as_ref());
261 crate::db::find_function(&db, here).map(|f| (fqn.clone(), f.location.clone()))
262 })
263 .collect()
264 }
265
266 /// Compute `file`'s outgoing dependency edges and persist them to the
267 /// disk cache's reverse-dep graph (if configured). The in-memory graph
268 /// is no longer maintained imperatively: `dependency_graph()` derives
269 /// structural edges from the memoized [`crate::db::file_structural_deps`]
270 /// tracked query, so there is no second copy to drift out of sync.
271 pub(super) fn update_reverse_deps_for(&self, file: &str) {
272 if let Some(cache) = self.cache.as_deref() {
273 let db = self.snapshot_db();
274 let targets = file_outgoing_dependencies(&db, file);
275 cache.update_reverse_deps_for_file(file, &targets);
276 }
277 }
278
279 /// File dependency graph: which files depend on which other files.
280 /// Used for incremental invalidation in LSP servers and build systems.
281 ///
282 /// File dependency graph: which files depend on which other files.
283 /// Used for incremental invalidation in LSP servers and build systems.
284 ///
285 /// O(edges) — iterates the `file_references` forward index (file → symbol
286 /// keys it references) which is always current, then resolves each symbol
287 /// to its defining file via O(1) lookup. Total cost is O(E) where E is the
288 /// number of (file, symbol) reference edges, vs. the old O(F × S × R) scan.
289 pub fn dependency_graph(&self) -> crate::DependencyGraph {
290 let db = self.snapshot_db();
291
292 let all_files: Vec<String> = db
293 .source_file_paths()
294 .iter()
295 .map(|f| f.as_ref().to_string())
296 .collect();
297
298 let mut dependencies: HashMap<String, Vec<String>> = HashMap::default();
299 let mut dependents: HashMap<String, Vec<String>> = HashMap::default();
300
301 for file in &all_files {
302 // O(degree(file)) — forward index lookup, no full-table scan.
303 let symbol_keys = db.file_referenced_symbols(file);
304 let mut file_deps: HashSet<String> = HashSet::default();
305 for symbol_key in &symbol_keys {
306 let lookup: &str = match symbol_key.split_once("::") {
307 Some((class, _)) => class,
308 None => symbol_key.as_ref(),
309 };
310 if let Some(def_file) = db.symbol_defining_file(lookup) {
311 let def = def_file.as_ref().to_string();
312 if &def != file {
313 file_deps.insert(def);
314 }
315 }
316 }
317 for dep in &file_deps {
318 dependents
319 .entry(dep.clone())
320 .or_default()
321 .push(file.clone());
322 dependencies
323 .entry(file.clone())
324 .or_default()
325 .push(dep.clone());
326 }
327 }
328
329 // Merge structural deps derived from definition collection. The
330 // forward pass above only captures bare-FQN references recorded
331 // during body analysis; `file_structural_deps` covers imports, class
332 // hierarchy (extends/implements/use), and type-hint-only references
333 // that never appear in file_referenced_symbols. The query is salsa-
334 // memoized, so the warm rebuild costs one map lookup per file rather
335 // than a definition walk — and there is no imperatively-maintained
336 // reverse map to drift out of sync with the definitions.
337 for file in &all_files {
338 let Some(sf) = db.lookup_source_file(file) else {
339 continue;
340 };
341 for target in crate::db::file_structural_deps(&db, sf).iter() {
342 let target = target.as_ref().to_string();
343 if &target != file {
344 dependents
345 .entry(target.clone())
346 .or_default()
347 .push(file.clone());
348 dependencies.entry(file.clone()).or_default().push(target);
349 }
350 }
351 }
352
353 for deps in dependents.values_mut() {
354 deps.sort();
355 deps.dedup();
356 }
357 for deps in dependencies.values_mut() {
358 deps.sort();
359 deps.dedup();
360 }
361
362 // Augment with stale dependents: files referencing symbols that were
363 // deleted from their defining file. These edges disappear from the
364 // symbol_defining_file lookup but the referencing file still needs
365 // re-analysis to surface the now-broken reference.
366 {
367 let stale = self.stale_defined_symbols.read();
368 if !stale.is_empty() {
369 for (file, deleted_syms) in stale.iter() {
370 for sym in deleted_syms {
371 let lookup: &str = match sym.split_once("::") {
372 Some((class, _)) => class,
373 None => sym.as_ref(),
374 };
375 for referencing_file in db.symbol_referencers_of(lookup) {
376 let ref_file = referencing_file.as_ref().to_string();
377 if &ref_file != file {
378 dependents
379 .entry(file.clone())
380 .or_default()
381 .push(ref_file.clone());
382 dependencies.entry(ref_file).or_default().push(file.clone());
383 }
384 }
385 }
386 }
387 // Re-sort and dedup since we may have added entries.
388 for deps in dependents.values_mut() {
389 deps.sort();
390 deps.dedup();
391 }
392 for deps in dependencies.values_mut() {
393 deps.sort();
394 deps.dedup();
395 }
396 }
397 }
398
399 crate::DependencyGraph {
400 dependencies,
401 dependents,
402 }
403 }
404}