Skip to content

Commit 7988b93

Browse files
committed
Fix missing compile error when declaring hooked props on readonly classes
Fixes GH-15419
1 parent 8853cf3 commit 7988b93

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-15419: Readonly classes may not declare properties with hooks
3+
--FILE--
4+
<?php
5+
6+
readonly class C {
7+
public int $prop { set => $value; }
8+
}
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Hooked properties cannot be readonly in %s on line %d
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-15419: Readonly classes may not declare promoted properties with hooks
3+
--FILE--
4+
<?php
5+
6+
readonly class C {
7+
public function __construct(
8+
public int $prop { set => $value; },
9+
) {}
10+
}
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Hooked properties cannot be readonly in %s on line %d

Zend/zend_compile.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8382,6 +8382,10 @@ static void zend_compile_property_hooks(
83828382
{
83838383
zend_class_entry *ce = CG(active_class_entry);
83848384

8385+
if (prop_info->flags & ZEND_ACC_READONLY) {
8386+
zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8387+
}
8388+
83858389
if (hooks->children == 0) {
83868390
zend_error_noreturn(E_COMPILE_ERROR, "Property hook list cannot be empty");
83878391
}
@@ -8601,11 +8605,6 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f
86018605
ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
86028606
}
86038607

8604-
if (hooks_ast && (flags & ZEND_ACC_READONLY)) {
8605-
zend_error_noreturn(E_COMPILE_ERROR,
8606-
"Hooked properties cannot be readonly");
8607-
}
8608-
86098608
if (type_ast) {
86108609
type = zend_compile_typename(type_ast);
86118610

0 commit comments

Comments
 (0)