Skip to content

Keep the members of unions of general arrays apart through joins, writes and foreach - #7237

Merged
ondrejmirtes merged 4 commits into
2.3.xfrom
keep-array-union-members-apart
Nov 28, 2026
Merged

ondrejmirtes merged 4 commits into
2.3.xfrom
keep-array-union-members-apart

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Fixes the forms reported in phpstan/phpstan#15873 after 0578e53 (unions of general arrays kept apart, bleeding edge):

  1. By-ref foreach and count() checks no longer merge list<X>|array<string, X>. The array{} of the loop-not-run or empty branch linked both general arrays in OverlappingArrayUnionRule. General arrays are now grouped among themselves, and a shape joins a group only when it reaches exactly one.
  2. Writing an array to an offset keeps list<int>|array<string, int> value members apart (same cause as 1).
  3. [$scalar] is accepted by list<string>|list<int>|…: UnionType::accepts() splits array{bool|int|…} into its members (via ArrayAlgebra::splitConstantArrayAtFiniteValue()), and joins no longer absorb array{T} into a list member.
  4. is_array($m[$k]) on list<shape>|array<stdClass> drops the array<stdClass> member, and a nested write to that tracked offset keeps list.
  5. The return-type tip no longer claims Offset 1 (X) does not accept type X for an offset that is accepted: a union that accepts drops the reasons of its rejecting members.
  6. foreach ($types as $key => $type) over a union of general arrays with disjoint key types ties $key, $type and $types by member, so is_int($key) narrows $type and $types.

Not fixed: the key-copying form ($copy[$key] = $type over array<int, T>|array<string, T> still gives array<int|string, T>). That would need conditional holders on the iteratee to survive the loop-head merge.

The changed shadowed classes (UnionType, MutatingScope, AssignHandler, ForeachHandler) are ported to turbo-ext. The full test suite passes with and without the extension, and smoke, signature parity and side-by-side pass.

Closes phpstan/phpstan#15873

@ondrejmirtes
ondrejmirtes force-pushed the keep-array-union-members-apart branch from c57482f to a8b3d2a Compare November 28, 2026 05:34
…tes and foreach

- OverlappingArrayUnionRule groups the general arrays among themselves
  first; a shape joins a group only when it reaches exactly one, so
  array{} no longer links list<X> and array<string, X> into one array.
- ArrayAlgebra::splitConstantArrayAtFiniteValue() also splits a value that
  is a union of other types, so list<int>|list<string> accepts
  array{int|string}.
- A certain non-null narrowing of $a[$k] drops the array members of $a
  whose values cannot hold it, and a write to a tracked offset keeps the
  container a list.
- UnionType::accepts() drops the reasons of rejecting members once one
  member accepts, so the tip no longer reports an accepted offset.
- foreach over a union of general arrays with disjoint key types ties the
  key, the value and the iteratee together by the member.

Closes phpstan/phpstan#15873

Amp-Thread-ID: https://ampcode.com/threads/T-01a21efd-7346-7575-a0dd-5474e0f5c329
A split shape is accepted member by member, which split the arrays among
its values again, so shapes nested in each other split into
exponentially many shapes (nested-nullable-shape-accepts: 213M to 23.8B
instructions). Scalar unions, which [$scalar] for list<A>|list<B> needs,
still split.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21efd-7346-7575-a0dd-5474e0f5c329
@ondrejmirtes
ondrejmirtes force-pushed the keep-array-union-members-apart branch from cc39314 to 5025c06 Compare November 28, 2026 06:15
A write under a string key to a tracked offset of array{} was intersected
with list, which made the written array never.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21efd-7346-7575-a0dd-5474e0f5c329
@ondrejmirtes
ondrejmirtes merged commit 58d53fb into 2.3.x Nov 28, 2026
890 of 917 checks passed
@ondrejmirtes
ondrejmirtes deleted the keep-array-union-members-apart branch November 28, 2026 07:32
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.

Unions of general arrays kept apart (0578e5364) still merge on by-ref foreach and array offset writes, and reject [$scalar] for list<A>|list<B>

1 participant