Repository navigation
fix(mixed): a non-empty mixed is never a falsy value, nor an empty mixed a truthy one - #12672
Conversation
…xed a truthy one vimeo#8511 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ktu9y737NuDDE88xCYTSix
|
Behaviour change: |
# Conflicts: # tests/Template/ConditionalReturnTypeTest.php
✅ Performance: unchanged · Types: increased
How to read this table
✅ laravel: issues +0 −1Memory 1 765 → 1 765 MB (<0.1 %) · Wall 73.5 → 72.7 s (−1.1 %) Weighted 53 210 → 53 208 (−2, <0.01 %): L2 −1
Removed (1)
✅ symfony: issues +1 −1Memory 3 622 → 3 622 MB (<0.1 %) · Wall 201.7 → 198.6 s (−1.5 %)
Added (1)
Removed (1)
✅ drupal: issues +1 −0Memory 4 821 → 4 821 MB (<0.1 %) · Wall 306.6 → 293.1 s (−4.4 %) Weighted 169 331 → 169 335 (+4, <0.01 %): L4 +1
Added (1)
head f0e0aed · base 67cfa84 (merge-base) · workflow run |
# Conflicts: # tests/TypeReconciliation/EmptyTest.php
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Checked the report's findings against current master (07f4c6d) by running this PR's cases singly: Not changed, read only: finding 1 ( Ran: the seven cases on master. No code change pushed. |
alies-dev
left a comment
There was a problem hiding this comment.
🤖 The comparator change is at the right layer, but the reorder in TemplateInferredTypeReplacer introduces a regression for a template-param argument, visible in the perf report as new errors on Laravel.
Already fixed on master: no. On upstream/master the issue's example traces $result as false|int|string; at the PR head it is int.
Overlap / conflicts: conflicts with current master in UnionTypeComparator.php only because master added hasTemplateKeyOrValueOf() right where this PR inserts isMixedOfOppositeTruthiness(); trivial. No alternative PR.
Findings
src/Psalm/Internal/Type/TemplateInferredTypeReplacer.php:551: callingisDisjoint()beforeintersectUnionTypes()makes a conditional return resolve to the else branch when the argument is a generated template param whose bound ismixed. Repro (PR head vs its merge-base):
/**
* @param mixed $arg
* @return ($arg is (Countable|array<mixed>) ? string : int)
*/
function inner($arg) { return 1; }
/**
* @param mixed $arg
* @return ($arg is (Countable|array<mixed>) ? string : int)
*/
function outer($arg) { $r = inner($arg); /** @psalm-trace $r */ echo 1; return $r; }$r is int|string on the base and int on the PR (a plain mixed parameter stays int|string). Laravel's Eloquent\Builder::findOrFail() has exactly this shape (find($id) with ($id is Arrayable|array ? Collection<int, TModel> : TModel|null)), and the perf report shows count($result) going from PossiblyInvalidArgument to InvalidArgument on TModel|null at Builder.php:667, HasOneOrManyThrough.php:495 and :534, (the BelongsToMany.php lines only change message text). Reverting only the reorder restores int|string here but also brings back false|int|string for the issue's own example, so the reorder is needed and must be narrowed: let isDisjoint() look through a TTemplateParam candidate to its as type (a template whose bound is mixed is never decidably disjoint), and add a test for a template-param argument.
Recommendations (non-blocking)
- Sibling gap: after
if (empty($foo)) return;,$foo === false,=== ""and=== 0now reportTypeDoesNotContainType, but$foo === nullstill reports nothing at the PR head (null is just as falsy). If that path is a separate shortcut inAssertionFinder, a test or a note would help. canExpressionTypesBeIdentical()has about 25 callers (in_arrayprovider, shape comparators,TypeVariableTracker, purity analyzers); the newTypeDoesNotContainType/DocblockTypeContradictionresults fornon-empty-mixedare correct, but onein_array()or array-shape case would show the blast radius is understood.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…t from a conditional's type Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Pushed a3c81e2 (master merged in earlier at 3eed2a6; the
Not done: the Run: the cases above one by one, phpcs, a full self-analysis (clean). Not run: the whole suite and laravel, so the |
… empty mixed a truthy one `$foo === null` after `empty($foo) → return` reported nothing although `=== false` did: the comparator accepted any atomic in a TNonEmptyMixed container. Same rule as the identity check added earlier in this PR. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Pushed f49e30f for the two non-blocking notes.
Run: the two new cases single, plus 12 neighbouring single cases that mention |
…xed comparison Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
From the triage report generated 2026-11-19 05:16 UTC (reviewed head f49e30f); pushed dfe2f1f (tests only).
Ran single: the five cases (pass), self-analysis clean (the first push attempt failed phpcs on a missing |
|
Local benchmark run for the triage report's perf findings (the benchmark runner itself is not available to me): Psalm on
Limits: laravel only. Not run: the full suite (CI). |
alies-dev
left a comment
There was a problem hiding this comment.
🤖 Re-checked against the current head: nothing blocks this anymore, and it is ready to merge. Since the last review, other PRs landed on master and this branch now conflicts in src/Psalm/Internal/Type/TemplateInferredTypeReplacer.php, tests/Template/ConditionalReturnTypeTest.php and tests/TypeReconciliation/EmptyTest.php (mostly after #12883 and #12985). Please rebase onto master and keep both sides' test cases; I'll merge once CI is green.
# Conflicts: # src/Psalm/Internal/Type/TemplateInferredTypeReplacer.php # tests/Template/ConditionalReturnTypeTest.php # tests/TypeReconciliation/EmptyTest.php
…only when none of its parts can be of it Master's conditionalOnClosureKeepsTheIfBranchForAnInterfaceNarrowedToClosure: Factory&Closure is not contained by Closure in the comparator, so the whole-type check alone took it for disjoint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @jirkace! |
* build: require Psalm 7.14 - Psalm 7.14 changes what the type fixtures report (PHP deprecations, unrequired-package functions, mixed-widened templates), and the fixtures updated in this PR no longer pass on 7.13. - Raise the floor in composer.json and in the Symfony Console job of tests.yml. * test: give the type and emission fixtures the composer.json Psalm 7.14 needs - Psalm 7.14 counts a vendor function as defined only if the composer.json next to the config requires its package (vimeo/psalm#14413); tests/Type and the EmissionApp fixture have none, so `now()`, `cache()`, `env()`, `trans()`, `route()` and `to_route()` became UndefinedFunction and `mixed` (22 type tests and `EmissionAppTest`). - The type tests select a differently named `composer.fixture.json` through `COMPOSER`, which leaves the plugin's project-root detection (a `composer.json` in the working directory) untouched. * test: expect the PHP deprecations Psalm 7.14 reports in collection callback fixtures - `is_a($string, Class::class)` is deprecated since PHP 8.6 (DeprecatedFunction) and an implicitly nullable parameter since PHP 8.4 (DeprecatedSyntax); Psalm reports both regardless of the analysed PHP version (vimeo/psalm#14413). - The fixtures keep those shapes because the narrowing of the two-argument `is_a()` and of a `= null` closure parameter is what they pin; the deprecation is now part of `--EXPECTF--`. * test: pin that an unbounded template given to getOrPut() and queued() now resolves to mixed - Psalm 7.14 no longer treats a bare template as decidably not a Closure for the `$value is callable` guard (vimeo/psalm#12672), so the result is widened with `mixed` and returning it as the template reports MixedReturnStatement; before, a possible Closure result stayed silent. - Rename GetOrPutClosureDefaultKnownLimitation.phpt to GetOrPutClosureDefaultUnboundedTemplateTest.phpt (positive coverage) and rewrite the three stub docblocks that cited the limitation. * test: pin the possibly-empty array-shape unpack of Paginator::fragment() as a known limitation - Psalm 7.14 unpacks `array{0?: string}` like a `list<string>` (vimeo/psalm#14415), so the call no longer resolves both the getter and the setter branch of `fragment()`; 7.13.2 did. - Rename PaginatorFragmentUnpackTest.phpt to PaginatorFragmentUnpackKnownLimitation.phpt and rewrite the docblocks of the stub and of RouteDomainKnownLimitation.phpt that cited the old behavior. * test: suppress Psalm 7.14's static-call false positive on a trait constructor in the Blade taint fixture - Psalm 7.14 analyses the synthetic constructor call for uninitialised properties as a static call on the parent's trait and reports InvalidStaticInvocation (a regression of vimeo/psalm#14413, reproducible without the plugin). - `BladeComponentTaintTest::the_bridge_adds_no_non_taint_issue` asserts no other issue, so the fixture class suppresses it. * fix: accept the value argument in Enumerable::when() and unless() callbacks - Laravel's Enumerable interface docblock types the callbacks as callable($this) and $value as bool, but Conditionable calls $callback($this, $value) and accepts any value - Psalm 7.13.2 keeps the concrete $this type in callable($this) (vimeo/psalm#14423), which surfaced the arity mismatch as InvalidScalarArgument - Stub Enumerable::when() and unless() with the Conditionable signature * docs: link the upstream Enumerable docblock fix from the when()/unless() stub * test: link the filed Psalm issues from the workarounds and known limitations - vimeo/psalm#14554 (trait constructor InvalidStaticInvocation), #14556 (array-shape unpack), #14555 (composer.json next to the config).
A
non-empty-mixedwas treated as possibly identical to any type, so a conditional return type kept the branch for a falsy condition ($arg is false) for an argument that cannot be falsy.Before:
After:
Fixes #8511