Repository navigation
fix(list): a write under the key array_key_last() returned for a list keeps it a list - #14428
Conversation
… keeps it a list vimeo#14267 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Behaviour change
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 19cbde7. Configure here.
✅ Performance: unchanged · Types: decreased
How to read this table
✅ symfony: issues +1 −2Memory 3 596 → 3 596 MB (+0.0 %) · Wall 198.7 → 198.3 s (−0.2 %) Weighted 98 403 → 98 400 (−3, <0.01 %): L3 −1
Changed in place (1) @@ src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php:210 ArgumentTypeCoercion @@
- …mixed>, but parent type array<array-key, mixed> provided
+ …mixed>, but parent type array<array-key, array<array-key, mixed>|mixed|null|object> providedRemoved (1)
head a8eeb12 · base 708bf98 (merge-base) · workflow run |
…amed argument vimeo#14267 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
orklah
left a comment
There was a problem hiding this comment.
Thanks. This fixes #14267, but the key stays bound to the list after the list changes, so writes that create a gap are no longer reported. Each of these returns LessSpecificReturnStatement on master and nothing on this branch:
/**
* @param non-empty-list<int> $l
* @return list<int>
*/
function popTwice(array $l): array {
$k = array_key_last($l);
array_pop($l);
array_pop($l);
$l[$k] = 1; // gap
return $l;
}
/**
* @param list<int> $a
* @param non-empty-list<int> $b
* @return list<int>
*/
function reassign(array $a, array $b): array {
$k = array_key_last($b);
$b = $a;
$b[$k] = 1; // gap
return $b;
}
/**
* @param non-empty-list<int> $l
* @return list<int>
*/
function sliced(array $l): array {
$k = array_key_last($l);
$l = array_slice($l, 1);
$l[$k] = 1; // gap
return $l;
}
final class Bag {
/** @var list<int> */
public array $items = [];
public function reset(): void { $this->items = []; }
public function m(): void {
$k = array_key_last($this->items);
if ($k === null) return;
$this->reset();
$this->items[$k] = 1; // PropertyTypeCoercion on master
}
}Reassigning the list does not drop the binding (only $l = [] happens to work): nothing invalidates dependent_list_key, its only reader is ArrayAssignmentAnalyzer. That is #13920, which the foreach key already has; with array_key_last() the key lives outside any loop, so it is much easier to hit. Could you drop the binding whenever the list variable changes (assignment, by-reference argument, property invalidated by a method call), and add these cases as invalid-code tests? That fixes #13920 too.
Minor: ArrayKeyFirstLastReturnTypeProvider is registered out of alphabetical order in FunctionReturnTypeProvider.
Context::$list_key_bound_lists records the lists a key type is bound to (array_key_first/last, foreach); the binding is dropped from the types in scope when the list variable changes (assignment, by-reference argument, method call). A write under an offset keeps it. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Pushed b979588. Invalid-code cases, all but the last failing before: |
…ere any change of it is reported The binding is released by an assignment of the list, a by-reference argument, an unset, a call that forgets the property, and when a nested scope released it; a write under an offset (also from a branch or a loop) keeps it. Psalm's own analysis reported the list/non-list change of loops that write $a[$i] under the key of a foreach. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Pushed 1275d2c: the |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # tests/ArrayFunctionCallTest.php

Writing under the key
array_key_last()(orarray_key_first()) returned for a list turns the list into an array, although that key exists.Before
After
Fixes #14267