Skip to content

Commit c6c1e75

Browse files
pmmaganikic
authored andcommitted
Fix bug #74607: Don't check for bi-directional compatibility in traits
1 parent f2e7cdb commit c6c1e75

File tree

6 files changed

+53
-8
lines changed

6 files changed

+53
-8
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ PHP NEWS
5555
a fatal error). (pmmaga)
5656
. Fixed bug #63384 (Cannot override an abstract method with an abstract
5757
method). (pmmaga, wes)
58+
. Fixed bug #74607 (Traits enforce different inheritance rules). (pmmaga)
5859
. Fixed misparsing of abstract unix domain socket names. (Sara)
5960

6061
- BCMath:

Zend/tests/traits/bug60217b.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ $o = new CBroken;
2323
$o->foo(1);
2424

2525
--EXPECTF--
26-
Fatal error: Declaration of TBroken2::foo($a, $b = 0) must be compatible with TBroken1::foo($a) in %s on line %d
26+
Fatal error: Declaration of TBroken1::foo($a) must be compatible with TBroken2::foo($a, $b = 0) in %s

Zend/tests/traits/bug74607.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Bug #74607 (Traits enforce different inheritance rules - return types)
3+
--FILE--
4+
<?php
5+
6+
abstract class L1{
7+
abstract function m3($x);
8+
}
9+
10+
trait L2t{
11+
function m3($x): int{}
12+
}
13+
14+
class L2 extends L1{
15+
use L2t;
16+
}
17+
18+
echo "DONE";
19+
20+
?>
21+
--EXPECT--
22+
DONE

Zend/tests/traits/bug74607a.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Bug #74607 (Traits enforce different inheritance rules - number of required parameters)
3+
--FILE--
4+
<?php
5+
6+
abstract class L1{
7+
abstract function m3($x);
8+
}
9+
10+
trait L2t{
11+
function m3($x, $y = 0){}
12+
}
13+
14+
class L2 extends L1{
15+
use L2t;
16+
}
17+
18+
echo "DONE";
19+
20+
?>
21+
--EXPECT--
22+
DONE

Zend/tests/traits/bugs/abstract-methods05.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ class TraitsTest1 {
2222

2323
?>
2424
--EXPECTF--
25-
Fatal error: Declaration of THelloA::hello($a) must be compatible with THelloB::hello() in %s on line %d
25+
Fatal error: Declaration of THelloB::hello() must be compatible with THelloA::hello($a) in %s on line %d

Zend/zend_inheritance.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,6 @@ static zend_bool zend_traits_method_compatibility_check(zend_function *fn, zend_
10881088
uint32_t other_flags = other_fn->common.scope->ce_flags;
10891089

10901090
return zend_do_perform_implementation_check(fn, other_fn)
1091-
&& ((other_fn->common.scope->ce_flags & ZEND_ACC_INTERFACE) || zend_do_perform_implementation_check(other_fn, fn))
10921091
&& ((fn_flags & (ZEND_ACC_FINAL|ZEND_ACC_STATIC)) ==
10931092
(other_flags & (ZEND_ACC_FINAL|ZEND_ACC_STATIC))); /* equal final and static qualifier */
10941093
}
@@ -1158,12 +1157,13 @@ static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_s
11581157
ZSTR_VAL(zend_get_function_declaration(fn)),
11591158
ZSTR_VAL(zend_get_function_declaration(existing_fn)));
11601159
}
1161-
} else if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
1160+
}
1161+
if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
11621162
/* Make sure the abstract declaration is compatible with previous declaration */
11631163
if (UNEXPECTED(!zend_traits_method_compatibility_check(existing_fn, fn))) {
11641164
zend_error_noreturn(E_COMPILE_ERROR, "Declaration of %s must be compatible with %s",
1165-
ZSTR_VAL(zend_get_function_declaration(fn)),
1166-
ZSTR_VAL(zend_get_function_declaration(existing_fn)));
1165+
ZSTR_VAL(zend_get_function_declaration(existing_fn)),
1166+
ZSTR_VAL(zend_get_function_declaration(fn)));
11671167
}
11681168
return;
11691169
}
@@ -1186,8 +1186,8 @@ static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_s
11861186
/* Make sure the abstract declaration is compatible with previous declaration */
11871187
if (UNEXPECTED(!zend_traits_method_compatibility_check(existing_fn, fn))) {
11881188
zend_error_noreturn(E_COMPILE_ERROR, "Declaration of %s must be compatible with %s",
1189-
ZSTR_VAL(zend_get_function_declaration(fn)),
1190-
ZSTR_VAL(zend_get_function_declaration(existing_fn)));
1189+
ZSTR_VAL(zend_get_function_declaration(existing_fn)),
1190+
ZSTR_VAL(zend_get_function_declaration(fn)));
11911191
}
11921192
return;
11931193
} else if (UNEXPECTED(existing_fn->common.scope->ce_flags & ZEND_ACC_TRAIT)) {

0 commit comments

Comments
 (0)