fix: walk parent_class chain when looking up __clone for op_clone (#57)#1562
Open
KorsarOfficial wants to merge 1 commit intoVKCOM:masterfrom
Open
fix: walk parent_class chain when looking up __clone for op_clone (#57)#1562KorsarOfficial wants to merge 1 commit intoVKCOM:masterfrom
KorsarOfficial wants to merge 1 commit intoVKCOM:masterfrom
Conversation
…COM#57) When cloning an object whose class does not define __clone() but an ancestor does, KPHP was not calling the inherited magic method because members.has_instance_method() only checks the class's own methods and does not walk the inheritance chain. Changed on_clone() in ConvertInvokeToFuncCallPass to use find_instance_method_by_local_name() which walks the full parent_class chain, matching PHP semantics. Also adds two regression tests: - tests/phpt/clone_keyword/105_inherited_clone_method.php - tests/phpt/clone_keyword/106_deep_inheritance_clone.php
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.
Problem
When cloning an object whose class
Childdoes not define__clone()but ancestorParentdoes, KPHP silently skips the inherited magic method — violating PHP semantics.Formal Analysis: Method Resolution Order
Define the class hierarchy:
parent : C → C ∪ {∅}— parent class functionMRO(C) = [C₀, C₁, …, Cₖ]whereC₀ = C,Cᵢ₊₁ = parent(Cᵢ)methods(C)= set of methods declared directly inClookup(C, m) = Cⱼwherej = min{i : m ∈ methods(Cᵢ)}Bug: The code used
m ∈ methods(C₀)instead oflookup(C, m).Example:
methods(Child) = ∅→__clone ∈ ∅ = false→ skiplookup(Child, __clone) = Parent ≠ ∅→ should callParent::__clone()Fix
Replace
klass->members.has_instance_method()(checks onlyC₀'s own members) withklass->find_instance_method_by_local_name()which traverses the fullparent_classchain.Correctness proof (by code inspection of class-data.cpp:268-299):
find_instance_method_by_local_name(C, m)iteratescur = C, parent(C), parent²(C), …returning the first match — exactly implementinglookup(C, m). ∎Complexity:
O(d)whered= inheritance depth (typically 1–5).Tests
tests/phpt/clone_keyword/105_inherited_clone_method.php— parent defines__clone(), child inheritstests/phpt/clone_keyword/106_deep_inheritance_clone.php—__clone()two levels upTechnical Report
📄 Full report with mathematical formalization (PDF)
Closes #57