Repository navigation
Require ternary arm remainders to be disjoint from the other arm in both isSuperTypeOf() directions before recording conditional expressions - #7094
Conversation
…oth `isSuperTypeOf()` directions before recording conditional expressions
- AssignHandler: when assigning a ternary, an arm's remainder (the part the
other arm cannot produce) is now used as a conditional-expression guard only
if both `otherArm->isSuperTypeOf(remainder)` and
`remainder->isSuperTypeOf(otherArm)` are `no`. Constant arrays that are not
sealed (no bleeding edge) compare asymmetrically: array{'px', 'rem'} is a
supertype of array{'px', 'rem', '%'}. The guard
"if $units is array{'px', 'rem'} then $blur is true" then supertype-matched
values of the other arm (e.g. array{0: 'px', 1: 'rem', 2?: '%'} after
in_array()), narrowing $blur to true.
- turbo-ext/src/AssignHandler.cpp: same change in the native mirror.
- Probed analogous constructs: if/else branch merges (ScopeOps guards already
reject guards that are not disjoint from the other branch) and match
arm assignments were already correct; they are covered in the new test.
staabm
left a comment
There was a problem hiding this comment.
can the mutations be killed?
https://github.com/phpstan/phpstan-src/actions/runs/43982145564/job/133703799426?pr=7094
| /** | ||
| * Runs without bleeding edge, so that constant arrays are not sealed. | ||
| */ | ||
| class Bug15760Test extends TypeInferenceTestCase |
There was a problem hiding this comment.
in https://github.com/phpstan/phpstan-src/pull/7095/files#diff-5330012288c961f747397e9ce43ad8efc3b0cd3461a646a6f38178c5431dba9fR11 we added a similar test.
can we consolidate all tests which extend extends TypeInferenceTestCase and do not have any specific use-case but just don't enable bleeding edge?
something like NoBleedingEdgeTypeInferenceTestCase
There was a problem hiding this comment.
I've consolidated the test classes into one, committed it as 0bf009d38 (not pushed), and make phpstan and make tests both pass.
What changed
Bug10980Test,Bug15736TestandBug15760Testwere identical single-file tests that only existed to run without bleeding edge. I deleted all three.- Their data files moved to
tests/PHPStan/Analyser/data/no-bleeding-edge/. - The new
tests/PHPStan/Analyser/NoBleedingEdgeTypeInferenceTest.phpruns every file in that directory, without bleeding edge. Adding a new case is now just dropping a file there; no new test class is needed. Because the directory sits underdata/, it's already excluded from lint, phpcs and phpstan, so no config changes were needed. - I left
ScopePhpVersionTestandTypeInferenceTestCaseTestas they are. They also extendTypeInferenceTestCasewithout bleeding edge, but they test specific things rather than just running a data file.
Checks
- The new test runs the same 10 assertions as the three old classes did.
- With the
AssignHandler.phpfix temporarily reverted, 3 of them fail (the bug-15760 cases), so the test still catches the bug after the move. make tests: 23866 tests pass, 116 skipped.
The PR #7095 test the reviewer linked isn't on this branch (I couldn't fetch that PR here), so it can move into the new directory once one of the two PRs is merged.
I didn't do anything about staabm's earlier question on whether the escaped mutations can be killed. I couldn't open the CI mutation report from here, and this review was only about consolidating the tests, so that question is still open.
…er arm These cases kill the escaped mutants that turn the reverse disjointness check from ->no() into !->yes(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I added tests that should kill the mutants, but I couldn't open the infection job log ( Likely escaped mutants: the existing tests only covered the case where the reverse check New tests (committed as $units = $blur ? ['px', 'rem'] : ['px', $s, '%'];
if ($units === ['px', 'rem', '%']) {
assertType('bool', $blur);
}Here the remainder Mutants I applied by hand to
|
Summary
Without bleeding edge,
$units = $blur ? ['px', 'rem'] : ['px', 'rem', '%']followed by a passingin_array($unit, $units, true)narrowed$blurtotrue. That caused a falseternary.alwaysTrueon the next ternary on$blur. This regressed when ternary assignments started deriving conditional expressions from overlapping arm types (fba3878). The fix restores the two-directional disjointness check for the arm remainders.Changes
src/Analyser/ExprHandler/AssignHandler.php: a ternary arm's remainder is recorded as a guard only if it is disjoint from the other arm in bothisSuperTypeOf()directions. Previously onlyotherArm->isSuperTypeOf(remainder)was checked.turbo-ext/src/AssignHandler.cpp: the same change in the native mirror. I built the extension and confirmed it fixes the test on its own.ScopeOps::createConditionalExpressions()already checksguard->isSuperTypeOf(theirType).matcharm assignments.Root cause
Without bleeding edge, constant arrays are not sealed, so
isSuperTypeOf()is not symmetric for them.array{'px', 'rem', '%'}is not a supertype ofarray{'px', 'rem'}, butarray{'px', 'rem'}is a supertype ofarray{'px', 'rem', '%'}, because extra keys are ignored.For the true arm,
remove(array{'px','rem'}, array{'px','rem','%'})staysarray{'px','rem'}. The one-directional check passed, so the guard "if$unitsisarray{'px','rem'}then$bluris true" was recorded. The false arm's remainder wasnever, so it got no guard.After
in_array(),$unitsisarray{0: 'px', 1: 'rem', 2?: '%'}. The supertype pass ofScopeOps::matchConditionalExpressions()matched it against the true-arm guard and narrowed$blurtotrue. Before fba3878, both arms had to be mutually disjoint in both directions. The fix reapplies that requirement to the remainders.Test
tests/PHPStan/Analyser/Bug15760Test.phpwithtests/PHPStan/Analyser/data/bug-15760.phpruns without bleeding edge, so constant arrays are not sealed. The regular nsrt suite runs with bleeding edge, where sealed arrays hide the bug. It covers:matchvariants;Fixes phpstan/phpstan#15760