Repository navigation
feat: a plain PHP method with no return type implements a PHP# interface member - #185
Merged
Merged
Conversation
…ace member A PHP# interface member keeps the type it states. Each PHP# call of the member, and each call of its method value, checks the result in one VERIFY_TYPE_ARGS named as PHP# writes the member, as `App.Taggable.morphToMany()`. The check reads a collection alone, as `List<Any?>`, so it costs the same however many elements the result holds. So a plain PHP class implements the member with a method that declares no return type, its own or one a package gives it, as Eloquent's Model gives getMorphClass(): - the bridge marks the member's result ZEND_SHARP_RESULT_CALLER_CHECKED, and the engine links an untyped method under it, beside ZEND_PARAM_WIDENED; - the analysis refuses none, by the one codex predicate ClassLikeMetadata::checks_results_at_call the bridge reads too. A method that declares another type is refused at compile and at load, as before. A call of a PHP# class's own method, or of an interface's static operator, checks nothing.
… adds patch-coverage needs every added line of the bridge run. The member's class and metadata now come from the one lookup of its callee, so no branch is left that no call reaches. A result PHP# writes always carries its type text, so marking it is an unreachable!(…) for any other node, as the bridge writes impossible arms. The call test checks a Set, a Map and an Iterable member too.
… the call is written The review of #185 found six gaps in the check each PHP# call of a PHP# interface's member runs on its result. - A `never` or `void` member no longer panics mago compile: the bridge marks the member's result ZEND_SHARP_RESULT_CALLER_CHECKED from the attr it gives the result, never from its node kind. - A call of a plain PHP class's untyped method, on the class or on `this` of a PHP# subclass, is a call of the member of the class's PHP# interface that declares it: the analyzer types it by the member, and the bridge checks it as a call through the interface. A class whose interfaces declare different results for the method is refused at the link, as Kotlin refuses conflicting inherited members. - The error names the type the member states: the check's text carries the type the error names after the type it checks, so a `List<int>` member's error says `List<int>`, and a null-safe call's says `Relation`. - The check reads each collection alone at any depth, a tuple's elements included. - A check's text in a static member of a generic class names the class's type parameter as the static member's `#i`, through the rewrite its own type texts take, so it no longer asserts in the engine. - A call that gives a generic method, or a generic class's static method, the type argument its result names checks the result against that type, as Java checks an erased generic method's result at its caller, since a check in the method's body reads the type parameter as any type. #179's forwarder goes: the engine links a plain PHP parent's untyped method under the member itself, and a PHP# subclass's calls take the member's type and check, so Lowering::forwarders, Types::forwarded_methods and the analyzer's forwarded exemption are deleted. The in-process engine test caller_checked.phpt runs every case under opcache, repeat and each optimizer pass.
…at/untyped-implementers
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.
A PHP# interface member keeps the type the user writes. A plain PHP class whose method declares no return type, its own or one a package gives it, now implements the member at
mago compileand when the engine links the class. Each PHP# call of the member checks the result, so a wrong value is refused where PHP# code uses it, with an error that names the member and the type it states as PHP# writes them.Before this, every Eloquent model behind a PHP# interface failed:
string getMorphClass()overModel::getMorphClass()was refused bymago analyzeand fataled when the class linked. The only way out was to state the memberAny?and cast each read, which is writing around the PHP underneath.How it works
The call checks the result. The bridge wraps each PHP# call of a PHP# interface's instance member, and each call of its method value, in one
VERIFY_TYPE_ARGSnamedApp.Taggable.morphToMany(). The check reads eachList,Set,MapandIterablein the result alone, a tuple's included, so it costs the same however many elements the result holds. A type that holds afloat, anAnyor anObjectis still checked whole, as section 24 checks every value from plain PHP. Avoidornevermember takes no check. Plain PHP callers stay unchecked.The error names the type the code wrote. The check's text carries the type the error names after the type it checks, as
List<Any?>, List<int>, and the engine names the second. A null-safe call's error namesRelation, notRelation?.A call on the plain class is a call of the member. When a class's method declares no return type and a PHP# interface the class implements declares the member, the analyzer types
contact.morphToMany(…)andthis.morphToMany(…)in a PHP# subclass by the member, and the bridge checks it as a call through the interface. A class whose interfaces declare different results for the method is refused at the link, as Kotlin refuses conflicting inherited members.A generic method's caller checks the type it gives. A check inside a generic method, or a generic class's static method, reads the type parameter as any type. So a call that gives that type argument checks the result against it, as Java checks an erased generic method's result at its caller. A call that gives its own method's type parameter leaves the check to the call that gives that one, and a data enum's case factory takes none.
The engine links an untyped method under the member. The bridge marks the member's result
ZEND_SHARP_RESULT_CALLER_CHECKED, and the compiler turns it into a type bit besideZEND_PARAM_WIDENED. Inheritance takes an implementation without a return type under it. This retires #179's forwarder, so a PHP# class over an untyped plain parent declares nothing for the member.What stays as it was:
: stringunderMorphToMany, is refused at compile and at load.Verification
MorphToMany morphToMany(string related, string name)over a plain class inheriting an untyped package method: compile accepts it, the engine loads it, and the call returns the relation with no cast, through the interface, on the plain class and onthisof a PHP# subclass.TypeError: CallerChecked.Taggable.morphToMany(): Return value must be of type CallerChecked.Relation, string returned.: stringmethod under the member: compile refuses it withincompatible-return-type, and the engine fatals at link.SHARP_VERIFY_TYPE_ARGSafterDO_FCALL. A call of a PHP# class's own method pays none.interface-member-typed-against-untyped-php-implementercompiles, analyzes with no issue, and printslabel:recordfor the PHP# and the plain PHP implementer.Tests
Zend/tests/sharp/caller_checked.phptruns in process, under every opcache mode, repeat and each optimizer pass. It covers anevermember, a generic class's static method, a method's own type parameter, a null-safe call, a tuple, the plain class, a PHP# subclass and itsthis, and each collection kind with a wrong value, with every error's exact text. Each case failed on the previous head: thenevermember panickedmago compile, the plain class and the subclass were refused, the static method asserted in the engine, the method's type parameter let a string through, and the errors namedList<Any?>andRelation?.The bridge tests
a_call_of_an_interface_member_checks_its_result,a_call_of_a_plain_class_method_under_an_interface_member_checks_its_result,a_call_of_a_generic_method_checks_its_result_against_the_type_it_givesandan_interface_member_marks_its_return_type_as_checked_by_each_call, and the analyzer testa_call_of_a_plain_class_method_under_an_interface_member_takes_the_members_type, each failed on the previous head.Spec
Section 22 states the rule beside the plain-PHP-implementer bullet, with an example
sharp/bin/specruns, and a second bullet for the generic caller's check. The MockK andall-openline is corrected in section 22 and in question 6 of slice 22.