Repository navigation
Walk a foreach over a union of general arrays once per member - #7307
Merged
Merged
Conversation
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
force-pushed
the
foreach-by-iteratee-member
branch
from
December 3, 2026 13:35
48e4bf5 to
6954613
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With bleeding edge,
foreach ($types as $key => $type) { $copy[$key] = $type; }overarray<int, T>|array<string, T>gavearray<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 ownConvergedLoopHeads, 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.ForeachHandleris ported to turbo-ext.make testspasses with and without the extension; strict build, smoke, signature parity, side-by-side and walk-trace (default corpus) pass. Self-analysis ofsrccosts about 0.6% more user CPU (near noise).Closes phpstan/phpstan#15946