Skip to content

Fix missing compile error when declaring hooked props on readonly classes #15439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ PHP NEWS
(zeriyoshi)
. Fixed bug GH-15438 (Hooks on constructor promoted properties without
visibility are ignored). (ilutov)
. Fixed bug GH-15419 (Missing readonly+hook incompatibility check for readonly
classes). (ilutov)

15 Aug 2024, PHP 8.4.0beta3

Expand Down
12 changes: 12 additions & 0 deletions Zend/tests/property_hooks/gh15419_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
GH-15419: Readonly classes may not declare properties with hooks
--FILE--
<?php

readonly class C {
public int $prop { set => $value; }
}

?>
--EXPECTF--
Fatal error: Hooked properties cannot be readonly in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/property_hooks/gh15419_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
GH-15419: Readonly classes may not declare promoted properties with hooks
--FILE--
<?php

readonly class C {
public function __construct(
public int $prop { set => $value; },
) {}
}

?>
--EXPECTF--
Fatal error: Hooked properties cannot be readonly in %s on line %d
9 changes: 4 additions & 5 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8385,6 +8385,10 @@ static void zend_compile_property_hooks(
{
zend_class_entry *ce = CG(active_class_entry);

if (prop_info->flags & ZEND_ACC_READONLY) {
zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
}

if (hooks->children == 0) {
zend_error_noreturn(E_COMPILE_ERROR, "Property hook list cannot be empty");
}
Expand Down Expand Up @@ -8608,11 +8612,6 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f
ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
}

if (hooks_ast && (flags & ZEND_ACC_READONLY)) {
zend_error_noreturn(E_COMPILE_ERROR,
"Hooked properties cannot be readonly");
}

if (type_ast) {
type = zend_compile_typename(type_ast);

Expand Down
Loading