mir-analyzer 0.87.0

Analysis engine for the mir PHP static analyzer
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Per-scope tracked inference.
//!
//! [`infer_scope`] memoizes body analysis at the granularity of one
//! top-level declaration (function or class-like) plus two file-frame
//! scopes, instead of one whole-file memo. The payoff is cross-file: when a
//! dependency's signature changes, a dependent file re-validates each scope
//! memo independently — scopes that don't touch the changed symbol stay
//! green and skip re-execution, so re-analysis cost scales with what the
//! change actually reaches, not with file size.
//!
//! Scope granularity is the *declaration*, not the method: intra-file
//! invalidation is file-granular either way (every scope depends on
//! `parse_file`), so per-method keys would only multiply memo count without
//! finer invalidation. Closures and anonymous classes stay inline in their
//! enclosing scope — their analysis input is the flow-sensitive variable
//! environment at their syntactic position, which is an output of analyzing
//! the enclosing body.
//!
//! Dispatch calls the *same* `BodyAnalyzer::analyze_*_decl` methods that
//! `analyze_bodies` calls, so per-scope output is byte-identical to the
//! whole-file walk; [`analyze_file_per_scope`] reassembles the file result
//! in `analyze_bodies`' emission order (header → declarations in source
//! order → top-level executable statements).

use std::sync::Arc;

use mir_issues::Issue;

use crate::body_analysis::BodyAnalyzer;

use super::*;

/// One memoization unit of per-scope inference.
///
/// `Function` / `ClassLike` carry the resolved FQN plus an occurrence index
/// (nth declaration with that FQN in the file, almost always 0) so duplicate
/// declarations — which `analyze_bodies` analyzes individually — each get
/// their own scope.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum ScopeKey {
    /// Duplicate-declaration check + `use`-statement casing checks.
    FileHeader,
    /// One top-level (or braced-namespace) function declaration.
    Function(Arc<str>, u32),
    /// One top-level (or braced-namespace) class / interface / trait / enum
    /// declaration: class frame plus all member bodies in member order.
    ClassLike(Arc<str>, u32),
    /// Top-level executable statements in global scope.
    FileExec,
}

/// Everything one scope's analysis produces that salsa memoizes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScopeInferenceResult {
    pub issues: Arc<[Issue]>,
    pub ref_locs: Arc<[RefLoc]>,
    pub(crate) inferred_types: Arc<crate::body_analysis::InferredTypes>,
}

impl ScopeInferenceResult {
    fn empty() -> Arc<Self> {
        Arc::new(Self {
            issues: Arc::from([]),
            ref_locs: Arc::from([]),
            inferred_types: Arc::new(crate::body_analysis::InferredTypes {
                functions: Vec::new(),
                methods: Vec::new(),
                properties: Vec::new(),
            }),
        })
    }
}

/// Declaration scopes of `file` in source order (file-frame scopes are
/// implicit). Drives [`analyze_file_per_scope`]'s merge order.
#[salsa::tracked]
pub fn file_scopes(db: &dyn MirDatabase, file: SourceFile) -> Arc<[ScopeKey]> {
    let path = file.path(db);
    let parsed_file = super::queries::parse_file(db, file);
    let parsed = &*parsed_file.0;

    let mut keys: Vec<ScopeKey> = Vec::new();
    let mut occurrences: rustc_hash::FxHashMap<(bool, Arc<str>), u32> =
        rustc_hash::FxHashMap::default();
    for_each_top_level_decl(&parsed.program.stmts, &mut |stmt| {
        use php_ast::owned::StmtKind;
        let (is_fn, name) = match &stmt.kind {
            StmtKind::Function(decl) => (true, decl.name.as_deref().unwrap_or("")),
            StmtKind::Class(decl) => (
                false,
                decl.name
                    .as_ref()
                    .and_then(|i| i.as_deref())
                    .unwrap_or("<anonymous>"),
            ),
            StmtKind::Enum(decl) => (false, decl.name.as_deref().unwrap_or("<anonymous>")),
            StmtKind::Interface(decl) => (false, decl.name.as_deref().unwrap_or("<anonymous>")),
            StmtKind::Trait(decl) => (false, decl.name.as_deref().unwrap_or("")),
            _ => return,
        };
        let fqn: Arc<str> = Arc::from(resolve_name(db, path.as_ref(), name));
        let occ = occurrences.entry((is_fn, fqn.clone())).or_insert(0);
        let key = if is_fn {
            ScopeKey::Function(fqn, *occ)
        } else {
            ScopeKey::ClassLike(fqn, *occ)
        };
        *occ += 1;
        keys.push(key);
    });
    keys.into()
}

/// Walk top-level declarations in source order, recursing into braced
/// namespaces — the exact stmt set `BodyAnalyzer::analyze_top_level_stmts`
/// dispatches on.
fn for_each_top_level_decl<'a>(
    stmts: &'a [php_ast::owned::Stmt],
    f: &mut impl FnMut(&'a php_ast::owned::Stmt),
) {
    use php_ast::owned::StmtKind;
    for stmt in stmts.iter() {
        match &stmt.kind {
            StmtKind::Function(_)
            | StmtKind::Class(_)
            | StmtKind::Enum(_)
            | StmtKind::Interface(_)
            | StmtKind::Trait(_) => f(stmt),
            StmtKind::Namespace(ns) => {
                if let php_ast::owned::NamespaceBody::Braced(inner) = &ns.body {
                    for_each_top_level_decl(&inner.stmts, f);
                }
            }
            _ => {}
        }
    }
}

/// Per-scope tracked inference: run body analysis for one scope of `file`
/// and memoize its issues + reference locations.
///
/// Returns an empty result for hard parse errors (parse-error issues are
/// emitted by `analyze_file`, not per scope) and for scope keys that don't
/// resolve to a declaration in this file.
///
/// No `lru`: salsa 0.28 throws `PropagatedPanic` when a fixpoint query's evicted
/// memo is recomputed after revalidation (see `memo_bounds`).
///
/// Cross-file inferred-type demands can re-enter a scope still on the stack
/// (`A::f` → `B::g` → `A::h`); salsa then fixpoint-iterates from an empty
/// result, so the memo is independent of which scope was demanded first.
#[salsa::tracked(cycle_fn = infer_scope_cycle, cycle_initial = infer_scope_initial)]
pub fn infer_scope(
    db: &dyn MirDatabase,
    file: SourceFile,
    scope: ScopeKey,
) -> Arc<ScopeInferenceResult> {
    crate::recursion::ensure_phase_stack(|| infer_scope_body(db, file, scope))
}

fn infer_scope_body(
    db: &dyn MirDatabase,
    file: SourceFile,
    scope: ScopeKey,
) -> Arc<ScopeInferenceResult> {
    let prepared = super::queries::prepare_analysis_file(db, file);
    let path = &prepared.path;
    let text = &prepared.text;
    let parsed = prepared.parse_result();

    if prepared.has_hard_parse_errors {
        return ScopeInferenceResult::empty();
    }

    let driver = BodyAnalyzer::new(db, prepared.php_version);
    let mut driver = driver;
    driver.collect_inferred_types = true;
    driver.collect_symbols = false;

    let mut issues: Vec<Issue> = Vec::new();

    // Isolate this scope's refs in a fresh staging frame so nested on-demand
    // inference on the same handle can't leak into (or consume) them.
    db.push_ref_loc_frame();

    match &scope {
        ScopeKey::FileHeader => {
            crate::body_analysis::check_duplicate_declarations(
                &parsed.program.stmts,
                path,
                text.as_ref(),
                &parsed.source_map,
                &mut issues,
            );
            check_use_decls(
                &parsed.program.stmts,
                db,
                path,
                text.as_ref(),
                &parsed.source_map,
                &mut issues,
                true,
            );
        }
        ScopeKey::FileExec => {
            driver.analyze_global_exec(
                &parsed.program,
                path,
                text.as_ref(),
                &parsed.source_map,
                &mut issues,
                None,
            );
        }
        ScopeKey::Function(fqn, occ) | ScopeKey::ClassLike(fqn, occ) => {
            let want_fn = matches!(scope, ScopeKey::Function(..));
            let mut seen: u32 = 0;
            let mut found = false;
            for_each_top_level_decl(&parsed.program.stmts, &mut |stmt| {
                use php_ast::owned::StmtKind;
                if found {
                    return;
                }
                let _scope = crate::db::ns_scope::enter(db, path, stmt.span.start);
                let (is_fn, name) = match &stmt.kind {
                    StmtKind::Function(decl) => (true, decl.name.as_deref().unwrap_or("")),
                    StmtKind::Class(decl) => (
                        false,
                        decl.name
                            .as_ref()
                            .and_then(|i| i.as_deref())
                            .unwrap_or("<anonymous>"),
                    ),
                    StmtKind::Enum(decl) => (false, decl.name.as_deref().unwrap_or("<anonymous>")),
                    StmtKind::Interface(decl) => {
                        (false, decl.name.as_deref().unwrap_or("<anonymous>"))
                    }
                    StmtKind::Trait(decl) => (false, decl.name.as_deref().unwrap_or("")),
                    _ => return,
                };
                if is_fn != want_fn || resolve_name(db, path.as_ref(), name) != fqn.as_ref() {
                    return;
                }
                if seen != *occ {
                    seen += 1;
                    return;
                }
                found = true;
                match &stmt.kind {
                    StmtKind::Function(decl) => driver.analyze_fn_decl(
                        decl,
                        path,
                        text.as_ref(),
                        &parsed.source_map,
                        &mut issues,
                        None,
                    ),
                    StmtKind::Class(decl) => driver.analyze_class_decl(
                        decl,
                        path,
                        text.as_ref(),
                        &parsed.source_map,
                        &mut issues,
                        None,
                        &Default::default(),
                    ),
                    StmtKind::Enum(decl) => driver.analyze_enum_decl(
                        decl,
                        path,
                        text.as_ref(),
                        &parsed.source_map,
                        &mut issues,
                        None,
                    ),
                    StmtKind::Interface(decl) => driver.analyze_interface_decl(
                        decl,
                        path,
                        text.as_ref(),
                        &parsed.source_map,
                        &mut issues,
                        &Default::default(),
                        None,
                    ),
                    StmtKind::Trait(decl) => driver.analyze_trait_decl(
                        decl,
                        path,
                        text.as_ref(),
                        &parsed.source_map,
                        &mut issues,
                        None,
                    ),
                    _ => {}
                }
            });
        }
    }

    let ref_locs = db.pop_ref_loc_frame();
    let inferred_types = Arc::new(driver.take_inferred_types());
    crate::metrics::record_scope_analysis(path.as_ref(), 0);
    db.note_work(crate::db::Work::ScopeAnalysis, 1);
    crate::metrics::record_infer_scope_retained(
        issues.len(),
        ref_locs.len(),
        inferred_types.functions.len(),
        inferred_types.methods.len(),
        inferred_types.properties.len(),
    );

    Arc::new(ScopeInferenceResult {
        issues: issues.into(),
        ref_locs: ref_locs.into(),
        inferred_types,
    })
}

fn infer_scope_initial(
    _db: &dyn MirDatabase,
    _id: salsa::Id,
    _file: SourceFile,
    _scope: ScopeKey,
) -> Arc<ScopeInferenceResult> {
    ScopeInferenceResult::empty()
}

fn infer_scope_cycle(
    _db: &dyn MirDatabase,
    cycle: &salsa::Cycle,
    last: &Arc<ScopeInferenceResult>,
    value: Arc<ScopeInferenceResult>,
    _file: SourceFile,
    _scope: ScopeKey,
) -> Arc<ScopeInferenceResult> {
    if !super::inferred_types::should_widen(cycle) {
        return value;
    }
    use super::inferred_types::widen_unstable;
    let (prev, mut next) = (&last.inferred_types, (*value.inferred_types).clone());
    widen_unstable(&prev.functions, &mut next.functions, |e| &mut e.1);
    widen_unstable(&prev.methods, &mut next.methods, |e| &mut e.2);
    widen_unstable(&prev.properties, &mut next.properties, |e| &mut e.2);
    Arc::new(ScopeInferenceResult {
        issues: value.issues.clone(),
        ref_locs: value.ref_locs.clone(),
        inferred_types: Arc::new(next),
    })
}

/// Run `check_use_decl_casing` for every top-level `use` statement, in
/// source order (recursing into braced namespaces) — mirrors the `Use` arm
/// of `analyze_top_level_stmts`.
fn check_use_decls(
    stmts: &[php_ast::owned::Stmt],
    db: &dyn MirDatabase,
    file: &Arc<str>,
    source: &str,
    source_map: &php_rs_parser::source_map::SourceMap,
    issues: &mut Vec<Issue>,
    is_full: bool,
) {
    use php_ast::owned::StmtKind;
    for stmt in stmts.iter() {
        let _scope = crate::db::ns_scope::enter(db, file, stmt.span.start);
        match &stmt.kind {
            StmtKind::Use(use_decl) => {
                crate::body_analysis::check_use_decl_casing(
                    use_decl, db, file, source, source_map, issues, None, None, None, false,
                    is_full, true,
                );
            }
            StmtKind::Namespace(ns) => {
                if let php_ast::owned::NamespaceBody::Braced(inner) = &ns.body {
                    check_use_decls(&inner.stmts, db, file, source, source_map, issues, is_full);
                }
            }
            _ => {}
        }
    }
}

/// Reassemble whole-file analysis output from per-scope memos in
/// `analyze_bodies`' emission order: file header (duplicates + use casing),
/// declarations in source order, top-level executable statements.
///
/// Ref locs are concatenated unsorted; the caller (`analyze_file`)
/// sorts + dedups for memo determinism.
pub fn analyze_file_per_scope(db: &dyn MirDatabase, file: SourceFile) -> (Vec<Issue>, Vec<RefLoc>) {
    let mut issues: Vec<Issue> = Vec::new();
    let mut ref_locs: Vec<RefLoc> = Vec::new();

    let mut merge = |r: &ScopeInferenceResult| {
        issues.extend(r.issues.iter().cloned());
        ref_locs.extend(r.ref_locs.iter().cloned());
    };

    merge(infer_scope(db, file, ScopeKey::FileHeader));
    for key in file_scopes(db, file).iter() {
        merge(infer_scope(db, file, key.clone()));
    }
    merge(infer_scope(db, file, ScopeKey::FileExec));

    (issues, ref_locs)
}