feat(types): fluent wither-return covariance through descendant interfaces#518
Open
chadmandoo wants to merge 1 commit into
Open
Conversation
…faces A PSR-FIG immutable "wither" (`withHeader(): MessageInterface`) declares an ancestor interface as its return type but carries `@return static`. Called through a descendant-interface receiver, the result must keep the receiver's type so a subsequent descendant-only call type-checks — matching PHP's runtime `clone $this`. Both inference passes resolve a `with*` method whose declared return is a strict ancestor of the receiver interface back to the receiver: the checker (`infer_method_call_on_interface_type`) and the EIR lowering (`method_call_result_type`). Both are required — the checker alone leaves the EIR backend re-resolving to the ancestor and failing native codegen. Also accepts `Array(Mixed)` where an `AssocArray` parameter is declared (an untyped list literal passed to an `array<int, mixed>` param). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
What
A PSR-FIG immutable "wither" method (
withHeader(): MessageInterface) declares an ancestor interface as its return type but carries@return static. Called through a descendant-interface receiver, the result must keep the receiver's type so a subsequent descendant-only call type-checks — matching PHP's runtimeclone $thisand PHPStan's@return static.Previously rejected:
How
Both inference passes resolve a
with*method whose declared return is a strict ancestor of the receiver interface back to the receiver:infer_method_call_on_interface_typemethod_call_result_typeBoth are required: the checker fix alone leaves the EIR backend re-resolving the chained call to the ancestor and failing native codegen (
interface method call to unknown method Message::withMethod).Also accepts
Array(Mixed)where anAssocArrayparameter is declared (an untyped list literal passed to anarray<int, mixed>param).Test
test_interface_wither_ancestor_return_stays_receiver— aMessage/Request extends Messagewither chain, byte-parity vs PHP 8.5 (chain(new Req())printsPOST).🤖 Generated with Claude Code