Skip to content

Walk a foreach over a union of general arrays once per member - #7307

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
foreach-by-iteratee-member
Dec 3, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
foreach-by-iteratee-member

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

With bleeding edge, foreach ($types as $key => $type) { $copy[$key] = $type; } over array<int, T>|array<string, T> gave array<int|string, T>, which is rejected now that the declared union is no longer merged (since 0578e53). This is the form #7237 left open, and the last doctrine/dbal failure in the integration tests (Connection::expandArrayParameters()).

When the iteratee is a union of general arrays whose keys share no value (the same condition as the conditional holders from #7237, now in getIterateeMembers()), the loop is also walked once per member, with the iteratee narrowed to it and a no-op node callback, and the scope after the loop is the join of those walks. Rules still see only the regular walk. Each member walk converges with its own ConvergedLoopHeads, so it does not warm-start an enclosing loop's passes from a narrowed entry. Without bleeding edge such unions are merged before the loop, so nothing changes there.

ForeachHandler is ported to turbo-ext. make tests passes with and without the extension; strict build, smoke, signature parity, side-by-side and walk-trace (default corpus) pass. Self-analysis of src costs about 0.6% more user CPU (near noise).

Closes phpstan/phpstan#15946

What a loop body builds from the keys and the values of an iteratee like
array<int, A>|array<string, B> comes from one member only, but the body
was walked with the whole union, so `$copy[$key] = $value` gave
array<int|string, A|B>. With bleeding edge the declared union is no
longer merged, so the copy was rejected as a return value (doctrine/dbal
Connection::expandArrayParameters()).

When the iteratee is a union of general arrays whose keys share no value,
the loop is also walked once per member, with the iteratee narrowed to it
and no node callback, and the scope after the loop is the join of those
walks. Each walk converges with loop heads of its own.

Amp-Thread-ID: https://ampcode.com/threads/T-01a23be5-5b73-7642-80c5-29429c352002
@ondrejmirtes
ondrejmirtes force-pushed the foreach-by-iteratee-member branch from 48e4bf5 to 6954613 Compare December 3, 2026 13:35
@ondrejmirtes
ondrejmirtes merged commit 8a13a45 into 2.3.x Dec 3, 2026
319 of 329 checks passed
@ondrejmirtes
ondrejmirtes deleted the foreach-by-iteratee-member branch December 3, 2026 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant