Skip to content

Fix GH-10377: Unable to have an anonymous readonly class #10381

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

Closed
wants to merge 1 commit into from
Closed
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
42 changes: 42 additions & 0 deletions Zend/tests/gh10377.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
GH-10377 (Unable to have an anonymous readonly class)
--FILE--
<?php

$readonly_anon = new readonly class {
public int $field;
function __construct() {
$this->field = 2;
}
};

$anon = new class {
public int $field;
function __construct() {
$this->field = 2;
}
};

var_dump($readonly_anon->field);
try {
$readonly_anon->field = 123;
} catch (Error $e) {
echo $e->getMessage() . "\n";
}
var_dump($readonly_anon->field);

var_dump($anon->field);
try {
$anon->field = 123;
} catch (Error $e) {
echo $e->getMessage() . "\n";
}
var_dump($anon->field);

?>
--EXPECT--
int(2)
Cannot modify readonly property class@anonymous::$field
int(2)
int(2)
int(123)
15 changes: 15 additions & 0 deletions Zend/tests/gh10377_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
GH-10377 (Unable to have an anonymous readonly class) - usage variation: dynamic properties attribute
--FILE--
<?php

$readonly_anon = new #[AllowDynamicProperties] readonly class {
public int $field;
function __construct() {
$this->field = 2;
}
};

?>
--EXPECTF--
Fatal error: Cannot apply #[AllowDynamicProperties] to readonly class class@anonymous in %s on line %d
10 changes: 10 additions & 0 deletions Zend/tests/gh10377_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
GH-10377 (Unable to have an anonymous readonly class) - usage variation: abstract modifier
--FILE--
<?php

$x = new abstract class {};

?>
--EXPECTF--
Fatal error: Cannot use the abstract modifier on an anonymous class in %s on line %d
10 changes: 10 additions & 0 deletions Zend/tests/gh10377_3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
GH-10377 (Unable to have an anonymous readonly class) - usage variation: final modifier
--FILE--
<?php

$x = new final class {};

?>
--EXPECTF--
Fatal error: Cannot use the final modifier on an anonymous class in %s on line %d
10 changes: 10 additions & 0 deletions Zend/tests/gh10377_4.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
GH-10377 (Unable to have an anonymous readonly class) - usage variation: multiple readonly modifiers
--FILE--
<?php

$x = new readonly readonly class {};

?>
--EXPECTF--
Fatal error: Multiple readonly modifiers are not allowed in %s on line %d
19 changes: 19 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,25 @@ uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
}
/* }}} */

uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
{
uint32_t new_flags = flags | new_flag;
if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
zend_throw_exception(zend_ce_compile_error,
"Cannot use the abstract modifier on an anonymous class", 0);
return 0;
}
if (new_flag & ZEND_ACC_FINAL) {
zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
return 0;
}
if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
return 0;
}
return new_flags;
}

uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
{
uint32_t new_flags = flags | new_flag;
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ typedef enum {
zend_ast *zend_ast_append_str(zend_ast *left, zend_ast *right);
zend_ast *zend_negate_num_string(zend_ast *ast);
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag);
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag);
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target);

uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t flags);
Expand Down
22 changes: 17 additions & 5 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);

%type <num> returns_ref function fn is_reference is_variadic property_modifiers
%type <num> method_modifiers class_const_modifiers member_modifier optional_cpp_modifiers
%type <num> class_modifiers class_modifier use_type backup_fn_flags
%type <num> class_modifiers class_modifier anonymous_class_modifiers anonymous_class_modifiers_optional use_type backup_fn_flags

%type <ptr> backup_lex_pos
%type <str> backup_doc_comment
Expand Down Expand Up @@ -601,6 +601,18 @@ class_modifiers:
{ $$ = zend_add_class_modifier($1, $2); if (!$$) { YYERROR; } }
;

anonymous_class_modifiers:
class_modifier
{ $$ = zend_add_anonymous_class_modifier(0, $1); if (!$$) { YYERROR; } }
| anonymous_class_modifiers class_modifier
{ $$ = zend_add_anonymous_class_modifier($1, $2); if (!$$) { YYERROR; } }
;

anonymous_class_modifiers_optional:
%empty { $$ = 0; }
| anonymous_class_modifiers { $$ = $1; }
;

class_modifier:
T_ABSTRACT { $$ = ZEND_ACC_EXPLICIT_ABSTRACT_CLASS; }
| T_FINAL { $$ = ZEND_ACC_FINAL; }
Expand Down Expand Up @@ -1095,12 +1107,12 @@ non_empty_for_exprs:
;

anonymous_class:
T_CLASS { $<num>$ = CG(zend_lineno); } ctor_arguments
anonymous_class_modifiers_optional T_CLASS { $<num>$ = CG(zend_lineno); } ctor_arguments
extends_from implements_list backup_doc_comment '{' class_statement_list '}' {
zend_ast *decl = zend_ast_create_decl(
ZEND_AST_CLASS, ZEND_ACC_ANON_CLASS, $<num>2, $6, NULL,
$4, $5, $8, NULL, NULL);
$$ = zend_ast_create(ZEND_AST_NEW, decl, $3);
ZEND_AST_CLASS, ZEND_ACC_ANON_CLASS | $1, $<num>3, $7, NULL,
$5, $6, $9, NULL, NULL);
$$ = zend_ast_create(ZEND_AST_NEW, decl, $4);
}
;

Expand Down