Repository navigation
Compare ternary arm values, not their truthiness, in exact narrowing contexts - #6580
Merged
Merged
Conversation
…contexts `(cond ? if : else) !== false` was narrowed through the boolean rewrite `(cond && if) || (!cond && else)` with the `!== false` context passed to the arms, which reads `0 !== false` as `0` being truthy. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LuUxEfBoMtVfZmJsUxA85s
This was referenced Sep 24, 2026
ondrejmirtes
added a commit
that referenced
this pull request
Sep 24, 2026
The ternary exact-context narrowing (#6580) is ported onto the composed (cond && if) || (!cond && else) narrowing: in an exact context the arms become `arm === true` / `arm !== false` (...) operands of a truthy decomposition, narrowed like the comparison node would be, with a native mirror in turbo-ext/src/TernaryHandler.cpp. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LuUxEfBoMtVfZmJsUxA85s
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.
TernaryHandler::specifyTypes()narrowscond ? if : elsethrough the boolean rewrite(cond && if) || (!cond && else)and passes the context down unchanged. That only holds for truthy/falsey contexts. For the exact contexts (=== true,=== false,!== true,!== false) the arms became boolean operands, so e.g.(!$a ? f() : 0) !== falseread0 !== falseas "0is truthy", ruled out the$aarm and narrowed$atofalse.The assign-time conditional expressions (
if $b is int then $a is false) are built from exactly this narrowing, so the wrong type then leaked into later branches.Exact contexts now compare each arm to the constant instead (
(cond && if !== false) || (!cond && else !== false), evaluated in a truthy context), so only the condition is treated by truthiness. Side effect:=== false/=== trueon a ternary now narrows more precisely, e.g.(!$a ? f() : 0) === falsegives$aasfalse.Closes phpstan/phpstan#15307
Closes phpstan/phpstan#15226
🤖 Generated with Claude Code
https://claude.ai/code/session_01LuUxEfBoMtVfZmJsUxA85s