|
| 1 | +//! Cache key for per-pass member-access subject resolution. |
| 2 | +//! |
| 3 | +//! A single file can contain hundreds of member-access spans that share |
| 4 | +//! the same subject text (e.g. 60 occurrences of `$this->assertEquals`, |
| 5 | +//! `$this->assertTrue`, …). Without caching, each span triggers the |
| 6 | +//! full resolution pipeline including `resolve_variable_types`, which |
| 7 | +//! re-parses the entire file via `with_parsed_program`. |
| 8 | +//! |
| 9 | +//! Every diagnostic that caches subject resolutions must key that cache |
| 10 | +//! the same way, because the key is what decides whether two accesses |
| 11 | +//! are guaranteed to see the same type. A key that is too coarse leaks |
| 12 | +//! one access's type into another: keying only by `(variable_name, |
| 13 | +//! class_name)` reports a deprecation against a same-named parameter in |
| 14 | +//! a *different* method of the same class. [`SubjectCacheKey::build`] |
| 15 | +//! is the one place that decision lives, so all consumers share it. |
| 16 | +//! |
| 17 | +//! The key deliberately omits per-access byte offsets so the cache stays |
| 18 | +//! effective — a service file with 200 accesses to `$model->` resolves |
| 19 | +//! the variable once, not 200 times. Expression-level narrowing |
| 20 | +//! (ternary `instanceof`, inline `&&` chains) can refine a type at a |
| 21 | +//! single byte offset without creating a narrowing block; consumers that |
| 22 | +//! care handle it with an uncached re-resolution fallback rather than by |
| 23 | +//! making the key finer. |
| 24 | +
|
| 25 | +use crate::symbol_map::SymbolMap; |
| 26 | +use crate::types::{AccessKind, ClassInfo}; |
| 27 | + |
| 28 | +/// Scope identifier for the subject resolution cache. |
| 29 | +/// |
| 30 | +/// Two member accesses share the same scope when they are inside the |
| 31 | +/// same class body (identified by class name and byte offset of the |
| 32 | +/// opening brace) **and** the same function/method/closure body |
| 33 | +/// (identified by its start offset). This prevents two methods in |
| 34 | +/// the same class from sharing a cache entry when a same-named |
| 35 | +/// variable has a different type in each method. |
| 36 | +#[derive(Clone, Debug, PartialEq, Eq, Hash)] |
| 37 | +pub(crate) enum ScopeKey { |
| 38 | + /// Inside a class at the given byte offset, within a specific |
| 39 | + /// function/method/closure scope. `fn_scope_start` is the byte |
| 40 | + /// offset of the enclosing function body (from |
| 41 | + /// [`SymbolMap::find_enclosing_scope`]), or `0` for class-level |
| 42 | + /// code outside any method. |
| 43 | + Class { |
| 44 | + name: String, |
| 45 | + start_offset: u32, |
| 46 | + fn_scope_start: u32, |
| 47 | + }, |
| 48 | + /// Top-level code outside any class, within a specific |
| 49 | + /// function scope (`0` when truly top-level). |
| 50 | + TopLevel { fn_scope_start: u32 }, |
| 51 | +} |
| 52 | + |
| 53 | +/// Cache key combining the subject text, access kind, and scope. |
| 54 | +#[derive(Clone, Debug, PartialEq, Eq, Hash)] |
| 55 | +pub(crate) struct SubjectCacheKey { |
| 56 | + subject_text: String, |
| 57 | + access_kind: AccessKind, |
| 58 | + scope: ScopeKey, |
| 59 | + /// The `effective_from` offset of the active variable definition at |
| 60 | + /// the point of access, or `0` for non-variable subjects. This |
| 61 | + /// ensures that accesses before and after a reassignment get |
| 62 | + /// separate cache entries. |
| 63 | + var_def_offset: u32, |
| 64 | + /// The innermost narrowing block containing the access for variable |
| 65 | + /// subjects (excluding `$this`), or `0` for non-variable subjects. |
| 66 | + /// This ensures that accesses inside different instanceof-narrowing |
| 67 | + /// contexts (e.g. different if-bodies) get independent cache |
| 68 | + /// entries. Without this, the first access caches a narrowed type |
| 69 | + /// and subsequent accesses in a different narrowing context reuse |
| 70 | + /// the wrong result. |
| 71 | + narrowing_offset: u32, |
| 72 | + /// The offset of the most recent `assert($var instanceof …)` |
| 73 | + /// statement preceding this access, or `0` if there is none. |
| 74 | + /// Assert-instanceof statements act as sequential narrowing |
| 75 | + /// boundaries: they change the variable's resolved type without |
| 76 | + /// creating a block scope, so accesses before and after the |
| 77 | + /// assert must get separate cache entries. |
| 78 | + assert_offset: u32, |
| 79 | +} |
| 80 | + |
| 81 | +impl SubjectCacheKey { |
| 82 | + /// Build the cache key for a member access on `subject_text` at |
| 83 | + /// `access_offset`. |
| 84 | + pub(crate) fn build( |
| 85 | + symbol_map: &SymbolMap, |
| 86 | + current_class: Option<&ClassInfo>, |
| 87 | + subject_text: &str, |
| 88 | + access_kind: AccessKind, |
| 89 | + access_offset: u32, |
| 90 | + ) -> Self { |
| 91 | + let fn_scope_start = symbol_map.find_enclosing_scope(access_offset); |
| 92 | + |
| 93 | + // For variable subjects (excluding $this), compute the active |
| 94 | + // definition offset so that accesses before and after a |
| 95 | + // reassignment get separate cache entries. |
| 96 | + let var_def_offset = if subject_text.starts_with('$') |
| 97 | + && subject_text != "$this" |
| 98 | + && !subject_text.starts_with("$this->") |
| 99 | + { |
| 100 | + // Extract the bare variable name (e.g. "$file" from "$file" |
| 101 | + // or from a chain like "$file->foo()"). |
| 102 | + let var_name = subject_text |
| 103 | + .find("->") |
| 104 | + .map(|i| &subject_text[..i]) |
| 105 | + .unwrap_or(subject_text); |
| 106 | + symbol_map.active_var_def_offset( |
| 107 | + &var_name[1..], // strip leading '$' |
| 108 | + access_offset, |
| 109 | + ) |
| 110 | + } else { |
| 111 | + 0 |
| 112 | + }; |
| 113 | + |
| 114 | + // Narrowing discrimination applies to regular variables ($var) |
| 115 | + // AND property chains on $this ($this->prop), because instanceof |
| 116 | + // checks and assert() calls can narrow property types just like |
| 117 | + // local variables. Bare $this is excluded because its type |
| 118 | + // never changes within a method. |
| 119 | + let needs_narrowing_discriminator = |
| 120 | + subject_text.starts_with('$') && subject_text != "$this"; |
| 121 | + let (narrowing_offset, assert_offset) = if needs_narrowing_discriminator { |
| 122 | + ( |
| 123 | + symbol_map.find_narrowing_block(access_offset), |
| 124 | + symbol_map.find_preceding_assert_offset(access_offset), |
| 125 | + ) |
| 126 | + } else { |
| 127 | + (0, 0) |
| 128 | + }; |
| 129 | + |
| 130 | + SubjectCacheKey { |
| 131 | + subject_text: subject_text.to_string(), |
| 132 | + access_kind, |
| 133 | + scope: scope_key_for(current_class, fn_scope_start), |
| 134 | + var_def_offset, |
| 135 | + narrowing_offset, |
| 136 | + assert_offset, |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +/// Build a [`ScopeKey`] from the innermost enclosing class (if any) |
| 142 | +/// and the enclosing function/method/closure scope start offset. |
| 143 | +fn scope_key_for(current_class: Option<&ClassInfo>, fn_scope_start: u32) -> ScopeKey { |
| 144 | + match current_class { |
| 145 | + Some(cc) => ScopeKey::Class { |
| 146 | + name: cc.name.to_string(), |
| 147 | + start_offset: cc.start_offset, |
| 148 | + fn_scope_start, |
| 149 | + }, |
| 150 | + None => ScopeKey::TopLevel { fn_scope_start }, |
| 151 | + } |
| 152 | +} |
0 commit comments