Repository navigation
Narrow the foreach source offset when the value variable is narrowed - #6386
Open
ondrejmirtes wants to merge 2 commits into
Open
ondrejmirtes wants to merge 2 commits into
ondrejmirtes wants to merge 2 commits into
Conversation
Collaborator
|
You've opened the pull request against the latest branch 2.3.x. PHPStan 2.3 is not going to be released for months. If your code is relevant on 2.2.x and you want it to be released sooner, please rebase your pull request and change its target to 2.2.x. |
This was referenced Sep 6, 2026
ondrejmirtes
force-pushed
the
dependent-types-foreach-alias
branch
from
September 12, 2026 15:13
f7872c9 to
405eec0
Compare
…ratee dim fetch Inside foreach ($array as $key => $value), the value variable and $array[$key] hold the same runtime value until any of $value, $key or the iteratee is written. enterForeach() now records that alias (ForeachValueAliasExpr, invalidated through containment of all three participating expressions), and applySpecifiedTypes() intersects a narrowing landed on the value variable into the tracked dim fetch holder while the link is intact. Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CGhnJQpUWRRpg6H5LuWJkA Amp-Thread-ID: https://ampcode.com/threads/T-01a19219-874b-7614-bea5-a5f416095107
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CGhnJQpUWRRpg6H5LuWJkA Amp-Thread-ID: https://ampcode.com/threads/T-01a19219-874b-7614-bea5-a5f416095107
ondrejmirtes
force-pushed
the
dependent-types-foreach-alias
branch
from
October 31, 2026 14:20
405eec0 to
faf0b8c
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.
Inside
foreach ($data as $key => $value), narrowing the value variable now also narrows the aliased source element$data[$key]for the current iteration, because they are two names for one runtime value.The alias is a pure precision optimisation, so it is dropped conservatively whenever a write could desync it from the value variable — writes that bypass the assignment path (by-reference closure use,
extract(), dynamic-variable writes), cross-iteration writes into the iteratee, and any write whose target cannot be proven to be exactly the current key. The same-key sub-offset write is kept.Two follow-up commits close the soundness holes found during review; the file is not shadowed by the turbo extension. Regression tests cover the fix and the bypass shapes; the full
NodeScopeResolverTeststays green and a local issue-bot run confirms the fix plus one collateral fix (#12500) with no regressions.Closes phpstan/phpstan#7508
Closes phpstan/phpstan#12500
🤖 Generated with Claude Code