Skip to content

Commit d9db446

Browse files
committed
Fix iface const visibility variance check
1 parent 7343ae5 commit d9db446

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

UPGRADING

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ PHP 8.3 UPGRADE NOTES
4343
next index is n+1 instead of 0.
4444
. Static variable initializers can now contain arbitrary expressions.
4545
RFC: https://wiki.php.net/rfc/arbitrary_static_variable_initializers
46+
. Class constant visibility variance is now correctly checked when inherited
47+
from interfaces.
4648

4749
- FFI:
4850
. C functions that have a return type of void now return null instead of
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Interface constant visibility should be invariant
3+
--FILE--
4+
<?php
5+
interface I {
6+
public const FOO = 'foo';
7+
}
8+
9+
class C implements I {
10+
private const FOO = 'foo';
11+
}
12+
?>
13+
--EXPECTF--
14+
Fatal error: Access level to C::FOO must be public (as in interface I) in %s on line %d

Zend/zend_inheritance.c

+9-10
Original file line numberDiff line numberDiff line change
@@ -1404,16 +1404,6 @@ static void do_inherit_class_constant(zend_string *name, zend_class_constant *pa
14041404

14051405
if (zv != NULL) {
14061406
c = (zend_class_constant*)Z_PTR_P(zv);
1407-
1408-
if (UNEXPECTED((ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PPP_MASK) > (ZEND_CLASS_CONST_FLAGS(parent_const) & ZEND_ACC_PPP_MASK))) {
1409-
zend_error_noreturn(E_COMPILE_ERROR, "Access level to %s::%s must be %s (as in class %s)%s",
1410-
ZSTR_VAL(ce->name), ZSTR_VAL(name),
1411-
zend_visibility_string(ZEND_CLASS_CONST_FLAGS(parent_const)),
1412-
ZSTR_VAL(parent_const->ce->name),
1413-
(ZEND_CLASS_CONST_FLAGS(parent_const) & ZEND_ACC_PUBLIC) ? "" : " or weaker"
1414-
);
1415-
}
1416-
14171407
bool inherit = do_inherit_constant_check(ce, parent_const, name);
14181408
ZEND_ASSERT(!inherit);
14191409
} else if (!(ZEND_CLASS_CONST_FLAGS(parent_const) & ZEND_ACC_PRIVATE)) {
@@ -1734,6 +1724,15 @@ static bool do_inherit_constant_check(
17341724
ZSTR_VAL(parent_constant->ce->name), ZSTR_VAL(name));
17351725
}
17361726

1727+
if (UNEXPECTED((ZEND_CLASS_CONST_FLAGS(child_constant) & ZEND_ACC_PPP_MASK) > (ZEND_CLASS_CONST_FLAGS(parent_constant) & ZEND_ACC_PPP_MASK))) {
1728+
zend_error_noreturn(E_COMPILE_ERROR, "Access level to %s::%s must be %s (as in %s %s)%s",
1729+
ZSTR_VAL(ce->name), ZSTR_VAL(name),
1730+
zend_visibility_string(ZEND_CLASS_CONST_FLAGS(parent_constant)),
1731+
zend_get_object_type(parent_constant->ce),
1732+
ZSTR_VAL(parent_constant->ce->name),
1733+
(ZEND_CLASS_CONST_FLAGS(parent_constant) & ZEND_ACC_PUBLIC) ? "" : " or weaker"
1734+
);
1735+
}
17371736

17381737
if (!(ZEND_CLASS_CONST_FLAGS(parent_constant) & ZEND_ACC_PRIVATE) && UNEXPECTED(ZEND_TYPE_IS_SET(parent_constant->type))) {
17391738
inheritance_status status = class_constant_types_compatible(parent_constant, child_constant);

0 commit comments

Comments
 (0)