From 0afa0ac10bf0e2d0216e2bd3ce7248d683f322d6 Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Thu, 10 Apr 2025 16:36:32 +0200 Subject: [PATCH 1/3] Fix self inheritance type checks for traits Fixes GH-18295 --- Zend/tests/traits/bug78776.phpt | 2 +- Zend/tests/traits/bug81192.phpt | 2 +- .../tests/traits/bugs/abstract-methods05.phpt | 2 +- .../tests/traits/bugs/abstract-methods06.phpt | 2 +- Zend/tests/traits/gh18295.phpt | 21 +++++++++++++++++++ Zend/tests/traits/inheritance003.phpt | 2 +- Zend/zend_inheritance.c | 5 +++++ 7 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 Zend/tests/traits/gh18295.phpt diff --git a/Zend/tests/traits/bug78776.phpt b/Zend/tests/traits/bug78776.phpt index 3696d955a3a3e..feecfeb8fddd4 100644 --- a/Zend/tests/traits/bug78776.phpt +++ b/Zend/tests/traits/bug78776.phpt @@ -25,4 +25,4 @@ B::createApp(); ?> --EXPECTF-- -Fatal error: Cannot make non static method A::createApp() static in class C in %s on line %d +Fatal error: Cannot make non static method A::createApp() static in class B in %s on line %d diff --git a/Zend/tests/traits/bug81192.phpt b/Zend/tests/traits/bug81192.phpt index 00f6f1d2fbf71..268df826ddfde 100644 --- a/Zend/tests/traits/bug81192.phpt +++ b/Zend/tests/traits/bug81192.phpt @@ -17,4 +17,4 @@ class B extends A { ?> --EXPECTF-- -Fatal error: Declaration of T::foo(): string must be compatible with A::foo(): int in %sbug81192_trait.inc on line 4 +Fatal error: Declaration of B::foo(): string must be compatible with A::foo(): int in %sbug81192_trait.inc on line 4 diff --git a/Zend/tests/traits/bugs/abstract-methods05.phpt b/Zend/tests/traits/bugs/abstract-methods05.phpt index 96619eae910b8..e0ffc60c14921 100644 --- a/Zend/tests/traits/bugs/abstract-methods05.phpt +++ b/Zend/tests/traits/bugs/abstract-methods05.phpt @@ -22,4 +22,4 @@ class TraitsTest1 { ?> --EXPECTF-- -Fatal error: Declaration of THelloB::hello() must be compatible with THelloA::hello($a) in %s on line %d +Fatal error: Declaration of TraitsTest1::hello() must be compatible with THelloA::hello($a) in %s on line %d diff --git a/Zend/tests/traits/bugs/abstract-methods06.phpt b/Zend/tests/traits/bugs/abstract-methods06.phpt index 8e2f25b048e0b..82ad805aed538 100644 --- a/Zend/tests/traits/bugs/abstract-methods06.phpt +++ b/Zend/tests/traits/bugs/abstract-methods06.phpt @@ -23,4 +23,4 @@ class TraitsTest1 { ?> --EXPECTF-- -Fatal error: Declaration of THelloB::hello() must be compatible with THelloA::hello($a) in %s on line %d +Fatal error: Declaration of TraitsTest1::hello() must be compatible with THelloA::hello($a) in %s on line %d diff --git a/Zend/tests/traits/gh18295.phpt b/Zend/tests/traits/gh18295.phpt new file mode 100644 index 0000000000000..438431a5b4eec --- /dev/null +++ b/Zend/tests/traits/gh18295.phpt @@ -0,0 +1,21 @@ +--TEST-- +GH-18295: Parent self is substitutable with child self through trait +--FILE-- + +===DONE=== +--EXPECT-- +===DONE=== diff --git a/Zend/tests/traits/inheritance003.phpt b/Zend/tests/traits/inheritance003.phpt index 1e630eef61d88..1826a084c5a01 100644 --- a/Zend/tests/traits/inheritance003.phpt +++ b/Zend/tests/traits/inheritance003.phpt @@ -35,4 +35,4 @@ $o->sayHello(array()); --EXPECTF-- World! -Fatal error: Declaration of SayWorld::sayHello(Base $d) must be compatible with Base::sayHello(array $a) in %s on line %d +Fatal error: Declaration of MyHelloWorld::sayHello(Base $d) must be compatible with Base::sayHello(array $a) in %s on line %d diff --git a/Zend/zend_inheritance.c b/Zend/zend_inheritance.c index 090b1049418d2..53fe021d2d4ec 100644 --- a/Zend/zend_inheritance.c +++ b/Zend/zend_inheritance.c @@ -3608,6 +3608,11 @@ ZEND_API zend_class_entry *zend_do_link_class(zend_class_entry *ce, zend_string zend_do_traits_method_binding(ce, traits_and_interfaces, trait_exclude_tables, trait_aliases, false, &trait_contains_abstract_methods); zend_do_traits_constant_binding(ce, traits_and_interfaces); zend_do_traits_property_binding(ce, traits_and_interfaces); + + zend_function *fn; + ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, fn) { + zend_fixup_trait_method(fn, ce); + } ZEND_HASH_FOREACH_END(); } if (parent) { if (!(parent->ce_flags & ZEND_ACC_LINKED)) { From 231980cb99cf1a47d7abbd43014c6b31383992e4 Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Thu, 10 Apr 2025 17:50:19 +0200 Subject: [PATCH 2/3] Fix other tests --- Zend/tests/inheritance/bug70957.phpt | 2 +- Zend/tests/type_declarations/variance/trait_error.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Zend/tests/inheritance/bug70957.phpt b/Zend/tests/inheritance/bug70957.phpt index 9341fcd288d95..6e5a51d9edea4 100644 --- a/Zend/tests/inheritance/bug70957.phpt +++ b/Zend/tests/inheritance/bug70957.phpt @@ -19,4 +19,4 @@ class B extends Foo } ?> --EXPECTF-- -Fatal error: Declaration of T::bar() must be compatible with Foo::bar($a = 'Foo') in %s on line %d +Fatal error: Declaration of B::bar() must be compatible with Foo::bar($a = 'Foo') in %s on line %d diff --git a/Zend/tests/type_declarations/variance/trait_error.phpt b/Zend/tests/type_declarations/variance/trait_error.phpt index 667b177a5b931..dd4ac59d8e7e8 100644 --- a/Zend/tests/type_declarations/variance/trait_error.phpt +++ b/Zend/tests/type_declarations/variance/trait_error.phpt @@ -17,4 +17,4 @@ class U extends X { ?> --EXPECTF-- -Fatal error: Could not check compatibility between T::method($r): B and X::method($a): A, because class B is not available in %s on line %d +Fatal error: Could not check compatibility between U::method($r): B and X::method($a): A, because class B is not available in %s on line %d From a118d62353fc739902c3d62b27d6e639cca8adff Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Mon, 14 Apr 2025 15:20:12 +0200 Subject: [PATCH 3/3] Only repeat zend_fixup_trait_method() for trait_contains_abstract_methods --- Zend/zend_inheritance.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Zend/zend_inheritance.c b/Zend/zend_inheritance.c index 53fe021d2d4ec..e718eb5684e65 100644 --- a/Zend/zend_inheritance.c +++ b/Zend/zend_inheritance.c @@ -3623,6 +3623,13 @@ ZEND_API zend_class_entry *zend_do_link_class(zend_class_entry *ce, zend_string if (ce->num_traits) { if (trait_contains_abstract_methods) { zend_do_traits_method_binding(ce, traits_and_interfaces, trait_exclude_tables, trait_aliases, true, &trait_contains_abstract_methods); + + /* New abstract methods may have been added, make sure to add + * ZEND_ACC_IMPLICIT_ABSTRACT_CLASS to ce. */ + zend_function *fn; + ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, fn) { + zend_fixup_trait_method(fn, ce); + } ZEND_HASH_FOREACH_END(); } if (trait_exclude_tables) { @@ -3639,11 +3646,6 @@ ZEND_API zend_class_entry *zend_do_link_class(zend_class_entry *ce, zend_string if (trait_aliases) { efree(trait_aliases); } - - zend_function *fn; - ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, fn) { - zend_fixup_trait_method(fn, ce); - } ZEND_HASH_FOREACH_END(); } if (ce->num_interfaces) { /* Also copy the parent interfaces here, so we don't need to reallocate later. */