fix(compiler): preserve runtime class dispatch and introspection for polymorphic objects - #110
Open
prateekbhujel wants to merge 1 commit into
Open
Conversation
…polymorphic objects When an object variable has an upper-bound declared type (e.g. assigned from a function or method return), SSA marks it as a stable object. However, stableObjects does not guarantee the concrete runtime class: the instance may be a subclass at runtime, or null if nullable. 1. FuncCallOptimizer::genGetClassOptimized checked isStableObject($obj->name) and folded get_class($obj) to the declared type literal string. For $a = getAnimal() returning a Dog, get_class($a) was incorrectly folded to "Animal" at compile time. 2. MethodCallTrait::parseStaticCall checked isStableObject($class) and jumped to _do_call with the declared base class. For $a::who(), this statically invoked Base::who() instead of dispatching to Dog::who() on the runtime object. Require proven exact object instances (exactObjects, populated from direct new ConcreteClass() definitions) before folding get_class() or devirtualizing static calls on object variables. Non-exact variables fall back to runtime class resolution (php::fn::get_class() and php::callStaticMethod()), matching PHP semantics. Add unit test coverage verifying exact objects continue to fold/devirtualize while polymorphic variables retain dynamic runtime dispatch.
prateekbhujel
force-pushed
the
prateek/preserve-runtime-class-dispatch
branch
from
September 12, 2026 12:54
e1e645c to
2a8f442
Compare
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.
When an object comes from a function or method return (like
$a = getAnimal()), SSA marks it instableObjects. But that only tracks the upper-bound declared type, not the actual runtime subclass:get_class($a)was getting folded at compile time to the base class instead of returning the runtime subclass (and returned a string onnullinstead of throwingTypeError)$a::who()statically resolved to the base class, bypassing subclass overridesSwapped
isStableObjectwithisset($this->context->exactObjects[...])ingenGetClassOptimizedandparseStaticCall. If the class isn't proven exact vianew ConcreteClass(), it safely falls back to runtime dispatch (php::fn::get_classandphp::callStaticMethod).Added unit tests for both exact and polymorphic cases.