Skip to content

Keep the list in an intersection of a list shape with another shape - #7221

Merged
ondrejmirtes merged 1 commit into
phpstan:2.3.xfrom
ondrejmirtes:intersect-lists-keep-list
Nov 27, 2026
Merged

ondrejmirtes merged 1 commit into
phpstan:2.3.xfrom
ondrejmirtes:intersect-lists-keep-list

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

ArrayAlgebra::intersectDefiniteConstantArrays() rebuilt the intersection key by key. From optional keys that may each be left out alone, the builder cannot tell that the result is a list: list{0: T1, 1: T3, 2?: …, 3?: …, 4?: …} & list{…} became array{0: T1, 1: T3, 2: T5, 3?: …, 4?: …}. The intersection with a list is a list, as tryIntersectShapeWithArray() already says.

The intersection runs in MutatingScope::processConditionalExpressionsAfterSpecifying() when entering an if branch. Since 0e9fdb5 keeps array shapes as tagged unions, such a non-list member survives until the loop widening, which then degrades list<T> to array<int<0, max>, T> (seven or more conditional appends in a loop). This broke the sebastianbergmann/phpunit integration job (src/Metadata/Api/CodeCoverage.php:157 and :226).

Closes phpstan/phpstan#15857

intersectDefiniteConstantArrays() rebuilt the intersection key by key, and
the builder cannot tell from optional keys that may each be left out alone
that the result is a list: list{0: int, 1?: int, 2?: int} & list{...} became
array{0: int, 1?: int, 2?: int}. The intersection with a list is a list.

In a loop of conditional appends, such a shape reached the widening of the
loop and degraded the list to array<int<0, max>, T>.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4a-d1b7-7397-8625-b131900ac0d3
@ondrejmirtes
ondrejmirtes merged commit 024ac54 into phpstan:2.3.x Nov 27, 2026
879 of 917 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array built with $a[] = … in a loop loses list with 7+ appends (since phpstan-src 0e9fdb53e, #7195)

1 participant