Skip to content

Commit b393659

Browse files
committed
Fix GH-10377: Unable to have an anonymous readonly class
This fixes the oversight that an anonymous class should be able to be readonly. Other identifiers such as final and abstract do not make sense. As we still want nice errors for when users try to use these modifiers, or use multiple modifiers, we introduce a new function zend_add_anonymous_class_modifier that will perform verification for anonymous class modifiers, just like zend_add_class_modifier does for non-anonymous classes.
1 parent 6660599 commit b393659

8 files changed

+124
-5
lines changed

Zend/tests/gh10377.phpt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
GH-10377 (Unable to have an anonymous readonly class)
3+
--FILE--
4+
<?php
5+
6+
$readonly_anon = new readonly class {
7+
public int $field;
8+
function __construct() {
9+
$this->field = 2;
10+
}
11+
};
12+
13+
$anon = new class {
14+
public int $field;
15+
function __construct() {
16+
$this->field = 2;
17+
}
18+
};
19+
20+
var_dump($readonly_anon->field);
21+
try {
22+
$readonly_anon->field = 123;
23+
} catch (Error $e) {
24+
echo $e->getMessage() . "\n";
25+
}
26+
var_dump($readonly_anon->field);
27+
28+
var_dump($anon->field);
29+
try {
30+
$anon->field = 123;
31+
} catch (Error $e) {
32+
echo $e->getMessage() . "\n";
33+
}
34+
var_dump($anon->field);
35+
36+
?>
37+
--EXPECT--
38+
int(2)
39+
Cannot modify readonly property class@anonymous::$field
40+
int(2)
41+
int(2)
42+
int(123)

Zend/tests/gh10377_1.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GH-10377 (Unable to have an anonymous readonly class) - usage variation: dynamic properties attribute
3+
--FILE--
4+
<?php
5+
6+
$readonly_anon = new #[AllowDynamicProperties] readonly class {
7+
public int $field;
8+
function __construct() {
9+
$this->field = 2;
10+
}
11+
};
12+
13+
?>
14+
--EXPECTF--
15+
Fatal error: Cannot apply #[AllowDynamicProperties] to readonly class class@anonymous in %s on line %d

Zend/tests/gh10377_2.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
GH-10377 (Unable to have an anonymous readonly class) - usage variation: abstract modifier
3+
--FILE--
4+
<?php
5+
6+
$x = new abstract class {};
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot use the abstract modifier on an anonymous class in %s on line %d

Zend/tests/gh10377_3.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
GH-10377 (Unable to have an anonymous readonly class) - usage variation: final modifier
3+
--FILE--
4+
<?php
5+
6+
$x = new final class {};
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot use the final modifier on an anonymous class in %s on line %d

Zend/tests/gh10377_4.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
GH-10377 (Unable to have an anonymous readonly class) - usage variation: multiple readonly modifiers
3+
--FILE--
4+
<?php
5+
6+
$x = new readonly readonly class {};
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Multiple readonly modifiers are not allowed in %s on line %d

Zend/zend_compile.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,25 @@ uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
812812
}
813813
/* }}} */
814814

815+
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
816+
{
817+
uint32_t new_flags = flags | new_flag;
818+
if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
819+
zend_throw_exception(zend_ce_compile_error,
820+
"Cannot use the abstract modifier on an anonymous class", 0);
821+
return 0;
822+
}
823+
if (new_flag & ZEND_ACC_FINAL) {
824+
zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
825+
return 0;
826+
}
827+
if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
828+
zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
829+
return 0;
830+
}
831+
return new_flags;
832+
}
833+
815834
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
816835
{
817836
uint32_t new_flags = flags | new_flag;

Zend/zend_compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ void zend_emit_final_return(bool return_one);
810810
zend_ast *zend_ast_append_str(zend_ast *left, zend_ast *right);
811811
zend_ast *zend_negate_num_string(zend_ast *ast);
812812
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag);
813+
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag);
813814
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag);
814815
bool zend_handle_encoding_declaration(zend_ast *ast);
815816

Zend/zend_language_parser.y

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
283283
%type <num> returns_ref function fn is_reference is_variadic variable_modifiers
284284
%type <num> method_modifiers non_empty_member_modifiers member_modifier
285285
%type <num> optional_property_modifiers property_modifier
286-
%type <num> class_modifiers class_modifier use_type backup_fn_flags
286+
%type <num> class_modifiers class_modifier anonymous_class_modifiers anonymous_class_modifiers_optional use_type backup_fn_flags
287287

288288
%type <ptr> backup_lex_pos
289289
%type <str> backup_doc_comment
@@ -602,6 +602,18 @@ class_modifiers:
602602
{ $$ = zend_add_class_modifier($1, $2); if (!$$) { YYERROR; } }
603603
;
604604

605+
anonymous_class_modifiers:
606+
class_modifier
607+
{ $$ = zend_add_anonymous_class_modifier(0, $1); if (!$$) { YYERROR; } }
608+
| anonymous_class_modifiers class_modifier
609+
{ $$ = zend_add_anonymous_class_modifier($1, $2); if (!$$) { YYERROR; } }
610+
;
611+
612+
anonymous_class_modifiers_optional:
613+
%empty { $$ = 0; }
614+
| anonymous_class_modifiers
615+
;
616+
605617
class_modifier:
606618
T_ABSTRACT { $$ = ZEND_ACC_EXPLICIT_ABSTRACT_CLASS; }
607619
| T_FINAL { $$ = ZEND_ACC_FINAL; }
@@ -1078,12 +1090,12 @@ non_empty_for_exprs:
10781090
;
10791091

10801092
anonymous_class:
1081-
T_CLASS { $<num>$ = CG(zend_lineno); } ctor_arguments
1093+
anonymous_class_modifiers_optional T_CLASS { $<num>$ = CG(zend_lineno); } ctor_arguments
10821094
extends_from implements_list backup_doc_comment '{' class_statement_list '}' {
10831095
zend_ast *decl = zend_ast_create_decl(
1084-
ZEND_AST_CLASS, ZEND_ACC_ANON_CLASS, $<num>2, $6, NULL,
1085-
$4, $5, $8, NULL, NULL);
1086-
$$ = zend_ast_create(ZEND_AST_NEW, decl, $3);
1096+
ZEND_AST_CLASS, ZEND_ACC_ANON_CLASS | $1, $<num>3, $7, NULL,
1097+
$5, $6, $9, NULL, NULL);
1098+
$$ = zend_ast_create(ZEND_AST_NEW, decl, $4);
10871099
}
10881100
;
10891101

0 commit comments

Comments
 (0)