Repository navigation
Widen an unresolvable closure accumulator to mixed and drop array_reduce()'s initial value for non-empty arrays - #7236
Merged
Conversation
…uce()'s initial value for non-empty arrays Since accumulator templates of a closure are resolved to a fixed point, a round could solve the accumulator to a type with an error type, from a closure body that does not type with it, or with a template of the called function itself, as the TIn of array_reduce() a closure over a mixed array returns. Such an accumulator cannot be resolved further and now widens to mixed, as before the fixed point. ArrayReduceFunctionReturnTypeExtension dropped the initial value only for constant arrays; it now asks the array type whether it is iterable at least once, so array_reduce() over a non-empty-list without $initial is no longer nullable. Amp-Thread-ID: https://ampcode.com/threads/T-01a22076-55e7-74c4-b04d-1a7660d2f877
It uses the native mixed type. Amp-Thread-ID: https://ampcode.com/threads/T-01a22076-55e7-74c4-b04d-1a7660d2f877
This was referenced Nov 28, 2026
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.
Fixes the three forms of the
array_reduce()regression introduced by 2b60ab0 ("Resolve accumulator templates of a closure to a fixed point"):*ERROR*in the accumulator: an accumulator round that solves to a type containing an error type (the closure body does not type with the accumulator) now widens tomixed, as before the fixed point.TInfor amixedarray: an accumulator containing a template of the called function itself (the closure's unresolved$itemreturned into the carry) also widens tomixed. Templates of other scopes, such as the caller's, are kept.|nullfor a non-empty array without$initial:ArrayReduceFunctionReturnTypeExtensionnow usesisIterableAtLeastOnce()of the array type instead of only checking constant arrays.The two changed assertions in
nsrt/array-functions.phpreduce anon-empty-array, so the previousstring|null/1|stringwere the same bug.Regression test:
tests/PHPStan/Analyser/nsrt/bug-15874.php(the issue's three playground snippets, plus a maybe-empty control); it fails without the fix.Closes phpstan/phpstan#15874
Amp thread: https://ampcode.com/threads/T-01a22076-55e7-74c4-b04d-1a7660d2f877