Skip to content

Commit b48b401

Browse files
More tests, cleanup, fix bug around constants being redefined
1 parent af53d9e commit b48b401

9 files changed

+109
-3
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
If a constant is redefined, attributes remain unchanged (no attributes)
3+
--FILE--
4+
<?php
5+
6+
const MY_CONST = "No attributes";
7+
8+
#[\MyAttribute]
9+
const MY_CONST = "Has attributes";
10+
11+
echo MY_CONST . "\n";
12+
13+
$reflection = new ReflectionConstant( 'MY_CONST' );
14+
var_dump( $reflection->getAttributes() )
15+
16+
?>
17+
--EXPECTF--
18+
Warning: Constant MY_CONST already defined in %s on line %d
19+
No attributes
20+
array(0) {
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
If a constant is redefined, attributes remain unchanged (attributes change)
3+
--FILE--
4+
<?php
5+
6+
#[\MyAttribute]
7+
const MY_CONST = "Has attributes (1)";
8+
9+
#[\MyOtherAttribute]
10+
const MY_CONST = "Has attributes (2)";
11+
12+
echo MY_CONST . "\n";
13+
14+
$reflection = new ReflectionConstant( 'MY_CONST' );
15+
var_dump( $reflection->getAttributes() )
16+
17+
?>
18+
--EXPECTF--
19+
Warning: Constant MY_CONST already defined in %s on line %d
20+
Has attributes (1)
21+
array(1) {
22+
[0]=>
23+
object(ReflectionAttribute)#%d (1) {
24+
["name"]=>
25+
string(11) "MyAttribute"
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
If a constant is redefined, attributes remain unchanged (had attributes)
3+
--FILE--
4+
<?php
5+
6+
#[\MyAttribute]
7+
const MY_CONST = "Has attributes";
8+
9+
const MY_CONST = "No attributes";
10+
11+
echo MY_CONST . "\n";
12+
13+
$reflection = new ReflectionConstant( 'MY_CONST' );
14+
var_dump( $reflection->getAttributes() )
15+
16+
?>
17+
--EXPECTF--
18+
Warning: Constant MY_CONST already defined in %s on line %d
19+
Has attributes
20+
array(1) {
21+
[0]=>
22+
object(ReflectionAttribute)#%d (1) {
23+
["name"]=>
24+
string(11) "MyAttribute"
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Error when attribute does not target constants (internal attribute)
3+
--FILE--
4+
<?php
5+
6+
#[Attribute]
7+
const EXAMPLE = 'Foo';
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Attribute "Attribute" cannot target constant (allowed targets: class) in %s on line %d

Zend/tests/attributes/constants/must_target_const.phpt renamed to Zend/tests/attributes/constants/must_target_const-userland.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Error when attribute does not target constants
2+
Error when attribute does not target constants (useland attribute)
33
--FILE--
44
<?php
55

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Verify processing of named arguments
3+
--DESCRIPTION--
4+
Since attributes on constants do not have arguments checked until runtime,
5+
check that the validation logic still works
6+
--FILE--
7+
<?php
8+
9+
#[MyAttribute(foo: "bar", foo: "baz")]
10+
const EXAMPLE = 'Foo';
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Duplicate named parameter $foo in %s on line %d

Zend/zend_compile.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11348,8 +11348,7 @@ void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynami
1134811348
if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
1134911349
/* Replace with compiled AST zval representation. */
1135011350
zval ast_zv;
11351-
zend_ast_ref *copy_ref = zend_ast_copy(*ast_ptr);
11352-
ZVAL_AST(&ast_zv, copy_ref);
11351+
ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
1135311352
zend_ast_destroy(*ast_ptr);
1135411353
*ast_ptr = zend_ast_create_zval(&ast_zv);
1135511354
}

Zend/zend_vm_def.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8275,6 +8275,10 @@ ZEND_VM_HANDLER(210, ZEND_DECLARE_ATTRIBUTED_CONST, CONST, CONST)
82758275
c.name = zend_string_copy(Z_STR_P(name));
82768276

82778277
if (zend_register_constant(&c) == FAILURE) {
8278+
FREE_OP1();
8279+
FREE_OP2();
8280+
/* two opcodes used, second one is the data with attributes */
8281+
ZEND_VM_NEXT_OPCODE_EX(1, 2);
82788282
}
82798283

82808284
zval *attribs = GET_OP_DATA_ZVAL_PTR(BP_VAR_R);

Zend/zend_vm_execute.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)