Repository navigation
fix(reconcile): keep what is known about an indexed value when the index is narrowed - #13344
Conversation
|
Behaviour change
|
✅ Performance: unchanged · Types: decreased
How to read this table
✅ symfony: issues +1 −1Memory 3 619 → 3 619 MB (<0.1 %) · Wall 268.2 → 287.1 s (+7.0 %)
Changed in place (1) @@ src/Symfony/Component/JsonSchema/Schema.php:182 MixedArgument @@
- Argument 2 of array_key_exists cannot be mixed, expecting array<array-key, mixed>
+ Argument 2 of array_key_exists cannot be array<array-key, array<array-key, mixed>|mixed>|mixed, expecting array<array-key, mixed>✅ drupal: issues +0 −25Memory 4 847 → 4 847 MB (<0.1 %) · Wall 247.1 → 256.4 s (+3.8 %) Weighted 169 393 → 169 346 (−47, −0.03 %): L1 −14 · L3 −11
Removed (25)
✅ wordpress: issues +3 −126Memory 2 061 → 2 060 MB (<0.1 %) · Wall 174.4 → 175.2 s (+0.5 %) Weighted 678 490 → 678 371 (−119, −0.02 %): L1 −125 · L3 +2
Changed in place (2) @@ src/wp-includes/ID3/module.audio-video.riff.php:1819 @@
- MixedArgument: Argument 2 of preg_match cannot be mixed, expecting string
- MixedArrayAccess: Cannot access array value on mixed variable $RIFFchunk[$chunkname][$thisindex]
+ PossiblyFalseArgument: Argument 2 of preg_match cannot be false, possibly string value expected
@@ src/wp-includes/ID3/module.audio-video.riff.php:1898 @@
- MixedArgument: Argument 1 of trim cannot be mixed, expecting string
- MixedArrayAccess: Cannot access array value on mixed variable $RIFFchunk[$chunkname][$thisindex]['parsed']
+ PossiblyFalseArgument: Argument 1 of trim cannot be false, possibly string value expectedAdded (1)
Removed (122)
head 4666ca1 · base eda66f2 (merge-base) · workflow run |
|
Checked against current master ( Not changed, read only: finding 1 ( Ran: the PR's case on master. No code change pushed. |
alies-dev
left a comment
There was a problem hiding this comment.
🤖 The idea is right (narrowing an index variable does not change what is known about $arr[$key]) and it fixes the issue's case, but by the author's own account it adds a new false-positive class on a benchmark project, and the branch conflicts with master.
Already fixed on master: no. On upstream/master the issue's snippet still reports PossiblyUndefinedStringArrayOffset for 'foo' and 'bar' inside if ("foo" === $key) / elseif ("bar" === $key).
Overlap / conflicts: GitHub shows the PR as conflicting. Against current master src/Psalm/Type/Reconciler.php and tests/ArrayAccessTest.php conflict (master added getPathType() and a series of offset-assertion changes around it). With #12633, #13087 and #13118 only tests/ArrayAccessTest.php conflicts.
Findings
- New issues in the perf report: drupal
ConfigSync.php:158/162/166/170PossiblyInvalidArrayOffset($form[$collection][$config_change_type]['heading'], "expecting -7|...|6"), symfonySchema.php:182MixedArgument, doctrineQueryBuilder.php:677MixedArrayAssignment. The author's note says theConfigSyncones come from keeping the whole stale entry of$arr[$key], not only its "is set" part. That is a new false positive caused by the change, not noise. The remembered entry should keep only what narrowing$keycannot invalidate (e.g. the possibly-undefined flag cleared), or be re-derived from$arr's value type, instead of preserving a stale type. (I could not reproduce theConfigSyncshape with a reduced copy of theforeach+switch ($type)pattern.) - A retained entry also bypasses the offset check of the narrowed key:
@param array<string, string|int> $arr,string|int $key,if (isset($arr[$key])) { if (is_int($key)) { echo $arr[$key]; } }reportsInvalidArrayOffseton master and nothing on the PR. Contrived, but it shows the entry is trusted beyond what the narrowing justifies.
Recommendations (non-blocking)
Reconciler::dependsOnNarrowedVar()is a public static textual check used fromReconciler,IfAnalyzer,ElseIfAnalyzer,ElseAnalyzer; fine as a consolidation, butContextis whereremoveDescendents()has the same rule, so consider one shared helper.issetOnAnIndexIsKeptWhenTheIndexIsNarrowedInALoopruns withensure_array_string_offsets_existoff and'assertions' => [], so on its own it asserts little; the twotestEnsureArrayOffsetsExist...cases carry the weight.
|
Not changed in this round: the open point of the review is the perf report (new issues in drupal, symfony and doctrine), and I could not reproduce them with reduced copies either, so I cannot say whether they are false positives or genuine, and changing the code blindly would risk regressions. Master was merged into the branch earlier today and nothing else changed. If a maintainer can point at the exact snippet of one of the perf deltas, I will reduce and fix it. |
|
Local benchmark run for the triage report's perf findings (the benchmark runner itself is not available to me): Psalm on
Limits: laravel only. Not run: the full suite (CI). |
|
Local benchmark run for the triage report's perf findings (the benchmark runner itself is not available to me), now on symfony: Psalm on a shallow clone of symfony/symfony, directories
Limits: a subset of symfony only. Not run: the full suite (CI). |
|
Local benchmark run on drupal (shallow clone of drupal/core:
Limits: a subset of drupal only. Not run: the full suite (CI). |
|
Local run on doctrine/orm (shallow clone, I reproduced it as a regression of this PR (master: the entry is final class Q {
/** @var array<string, mixed> */
private array $parts = ['select' => [], 'join' => []];
public function add(string $name, array $part, bool $append = false): void {
$isMultiple = is_array($this->parts[$name]) && ! ($name === 'join' && ! $append);
if ($name === 'join') {
$new = [];
foreach ($part as $k => $v) {
$new[$k] = $v;
}
$part = $new;
}
if ($append && $isMultiple) {
$__t = $this->parts[$name];
/** @psalm-trace $__t */ // master: array<array-key, mixed>, this branch: mixed
}
}
}It needs the |
|
Pushed 6b53dda for the doctrine/orm regression of the previous comment (
Ran single: the new case and the case of this PR, self-analysis clean. Not run: the full suite (CI), neighbouring if/elseif narrowing cases of other test files. |
|
Correction to my comment about 6b53dda: its cause was wrong and that commit broke this PR's own case. Pushed 6089ddc.
|
alies-dev
left a comment
There was a problem hiding this comment.
🤖 Re-checked against the current head: nothing blocks this anymore, and it is ready to merge. Since the last review, other PRs landed on master and this branch now conflicts in tests/ArrayAccessTest.php (test cases only; #13087 adds cases at the same spot, so rebase after it lands to avoid a second round). Please rebase onto master and keep both sides' test cases; I'll merge once CI is green.
|
From the triage report generated 2026-11-20 05:22 UTC (reviewed head 9e43f1a), finding 5 (the
|
orklah
left a comment
There was a problem hiding this comment.
Thanks for the follow-ups. The LoopAnalyzer part holds up: I tried twelve loop shapes that change a mixed variable (by-ref call, foreach value, list(), nested loops, use (&$m), while/do/for assignments, break, unset) and base and head agree on all of them, only the untouched-variable case changes.
One problem remains with the narrowing part, the stale type of the kept entry. It is reproducible:
function takesString(string $s): void {}
/**
* @param array{foo: string, bar: int} $a
* @param 'foo'|'bar' $k
*/
function f(array $a, string $k): void {
echo (string) $a[$k]; // same with `if (isset($a[$k]))`
if ($k === 'foo') {
takesString($a[$k]); // master: string, no issue
// this PR: int|string, InvalidScalarArgument
}
}The entry $a[$k] is kept with the type it got for 'foo'|'bar'. That is only safe when the value type does not depend on the key (array<K, V>, list<V>); for a keyed array it should be recomputed, keeping only the "is defined" part. The same mechanism repeats the drupal ConfigSync.php report on every case of the switch. Could you restrict the kept entry accordingly and add this snippet as a test with an assertion on the type?
The branch also conflicts with master in tests/ArrayAccessTest.php since #13118 landed; please rebase and keep both sides' cases.
f8346b6 to
85edc16
Compare
orklah
left a comment
There was a problem hiding this comment.
Thanks, the shape case is fixed: with array{foo: string, bar: int} the narrowed offset is string again, and uniform shapes, list<string> and $a['p'][$k] keep the defined offset as intended. The drupal ConfigSync.php repetition is gone from the perf report too.
Only a rebase is left: the branch conflicts again with master in tests/ArrayAccessTest.php (#14499 added cases at the top of the same two providers), please keep both sides. The red Unit Tests 4/8 jobs are not from this PR: master itself hung in that chunk this afternoon and is green again, so the new push will rerun them.
Optional: Reconciler is not @internal, so dependsOnNarrowedVar() becomes public API; marking the method @internal would avoid that.
2905977 to
83ac55b
Compare
|
@orklah Rebased onto master, new head 83ac55b. |
…dex is narrowed vimeo#7221 vimeo#7656 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…fter an if that narrows the variable Keeping `$this->parts[$name]` while `$name` is narrowed in the if branch lost what the `is_array($this->parts[$name])` clause of `$isMultiple` gave after the if, when the branch held a loop (doctrine/orm QueryBuilder::add(): new MixedArrayAssignment). IfAnalyzer drops them as before; the case this PR is about (an isset of an index narrowed in a loop or an elseif chain) does not need that site. The suppression of Config::getReportingLevelForFunction() that the IfAnalyzer part made unnecessary is back, so vimeo#7656 is only referenced. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…rvive it; restore the index relaxation 6b53dda put the original regular expression back in IfAnalyzer after the doctrine/orm regression (QueryBuilder::add(): `$isMultiple` clause `is_array($this->dqlParts[$dqlPartName])` lost after an `if ($name === 'join') { foreach ... }`), which broke the case of this PR (an isset of an index narrowed in nested ifs with ensureArrayStringOffsetsExist) and needed Config.php's suppression back. The cause was not the relaxation: LoopAnalyzer dropped the clauses of every `mixed` variable that was in scope when the loop is left, whether the loop touched it or not. The entry $this->parts[$name] is in scope again now that narrowing $name no longer removes it, so its clause was dropped; on master the entry was missing and the clause survived by accident. LoopAnalyzer drops the clauses only of a variable the loop changed (stamped in the loop context). IfAnalyzer and Config.php are as in dceb988. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…after it vimeo#7221 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…mputed when the index is narrowed vimeo#7221 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ead under the narrowed index vimeo#7221 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83ac55b to
4666ca1
Compare
…x type on it vimeo#13344 fixed the same forgetting of items keyed by a narrowed variable with Reconciler::dependsOnNarrowedVar(), so its src is kept. On top of it: - an item is still forgotten when the array's keys don't include the narrowed index, as PHP casts it, so that its fetch reports the offset - a loop pass doesn't restore an item that assigning the foreach variables forgot Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Psalm forgets that
$arr[$key]is set when$keyis narrowed insideif (isset($arr[$key])).Before
After
Fixes #7221
Fixes #7656