Repository navigation
Infer the return type of a closure calling itself through a by-ref use - #7216
Merged
Merged
Conversation
A closure assigned to the variable it uses by reference, as in $f = function () use (&$f), is now typed by a fixed point of its return: the variable starts as the closure returning never, each round walks the body with the previous round's closure, and the rounds widen as a loop does. Amp-Thread-ID: https://ampcode.com/threads/T-01a21c3c-cedc-7330-a083-cc986590888f
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.
Follow-up to #7212 and #7213.
A closure that calls itself through a by-ref use had its return type computed with the recursive call typed from a walk that entered the by-ref variable as
mixed, so the self-call contributedmixedor a benevolent arithmetic union:When
ClosureProcessortypes the by-ref use of the variable the closure is assigned to, and the right side is the closure itself without a declared return type,resolveAssignedClosureType()now finds the least fixed point of its return:never.LOOP_SCOPE_ITERATIONS/GENERALIZE_AFTER_ITERATION,generalizeIteratedType()from Resolve accumulator templates of a closure to a fixed point #7212). Without a fixed point within them, the type resolved as before stays.Starting from
neverrather than from today's type matters: a round that already seesfloat|intfrom the self-call can only reproduce it.To walk the body with the seeded variable,
ClosureTypeResolver::getClosureType()takes a new optional$byRefUseTypesparameter. It types those by-ref uses in the walk, which otherwise enters them asmixed, and it is part of the cache key.ClosureTypeResolveris not@api. Both changes are mirrored inturbo-ext/src/ClosureProcessor.cppandClosureTypeResolver.cpp.Changed expectation, more precise and sound:
nsrt/closure-signature-from-usages.phprecursion(): the untyped$fact($n), whose parameter is inferred asfloat|intfrom its usages, returnsNAN|float<1.0000000000000002, max>|int<1, max>instead offloat|int. The base case returns1, and the recursive branch runs only for$n > 1or NAN.Tests:
nsrt/recursive-closure-return-type.php. Without the source change, four of its assertions fail. A closure with a declared return type ($fib) is the control and staysint.side-by-side.php,signature-parity.phpandsmoke.phppass.