Skip to content

Commit 0eb8d7a

Browse files
committed
Add #[\NoDiscard] attribute definition
1 parent 1ce79eb commit 0eb8d7a

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
#[\NoDiscard]: NoDiscard::$message is readonly.
3+
--FILE--
4+
<?php
5+
6+
$d = new \NoDiscard("foo");
7+
$d->message = 'bar';
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Uncaught Error: Cannot modify readonly property NoDiscard::$message in %s:%d
12+
Stack trace:
13+
#0 {main}
14+
thrown in %s on line %d
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
#[\NoDiscard]: __construct() respects that properties are readonly.
3+
--FILE--
4+
<?php
5+
6+
$d = new \NoDiscard("foo");
7+
$d->__construct("bar");
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Uncaught Error: Cannot modify readonly property NoDiscard::$message in %s:%d
12+
Stack trace:
13+
#0 %s(%d): NoDiscard->__construct('bar')
14+
#1 {main}
15+
thrown in %s on line %d

Zend/zend_attributes.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ZEND_API zend_class_entry *zend_ce_sensitive_parameter;
3131
ZEND_API zend_class_entry *zend_ce_sensitive_parameter_value;
3232
ZEND_API zend_class_entry *zend_ce_override;
3333
ZEND_API zend_class_entry *zend_ce_deprecated;
34+
ZEND_API zend_class_entry *zend_ce_nodiscard;
3435

3536
static zend_object_handlers attributes_object_handlers_sensitive_parameter_value;
3637

@@ -193,6 +194,29 @@ ZEND_METHOD(Deprecated, __construct)
193194
}
194195
}
195196

197+
ZEND_METHOD(NoDiscard, __construct)
198+
{
199+
zend_string *message = NULL;
200+
zval value;
201+
202+
ZEND_PARSE_PARAMETERS_START(0, 1)
203+
Z_PARAM_OPTIONAL
204+
Z_PARAM_STR_OR_NULL(message)
205+
ZEND_PARSE_PARAMETERS_END();
206+
207+
if (message) {
208+
ZVAL_STR(&value, message);
209+
} else {
210+
ZVAL_NULL(&value);
211+
}
212+
zend_update_property_ex(zend_ce_nodiscard, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_MESSAGE), &value);
213+
214+
/* The assignment might fail due to 'readonly'. */
215+
if (UNEXPECTED(EG(exception))) {
216+
RETURN_THROWS();
217+
}
218+
}
219+
196220
static zend_attribute *get_attribute(HashTable *attributes, zend_string *lcname, uint32_t offset)
197221
{
198222
if (attributes) {
@@ -520,6 +544,9 @@ void zend_register_attribute_ce(void)
520544

521545
zend_ce_deprecated = register_class_Deprecated();
522546
attr = zend_mark_internal_attribute(zend_ce_deprecated);
547+
548+
zend_ce_nodiscard = register_class_NoDiscard();
549+
attr = zend_mark_internal_attribute(zend_ce_nodiscard);
523550
}
524551

525552
void zend_attributes_shutdown(void)

Zend/zend_attributes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extern ZEND_API zend_class_entry *zend_ce_sensitive_parameter;
4747
extern ZEND_API zend_class_entry *zend_ce_sensitive_parameter_value;
4848
extern ZEND_API zend_class_entry *zend_ce_override;
4949
extern ZEND_API zend_class_entry *zend_ce_deprecated;
50+
extern ZEND_API zend_class_entry *zend_ce_nodiscard;
5051

5152
typedef struct {
5253
zend_string *name;

Zend/zend_attributes.stub.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,14 @@ final class Deprecated
8484

8585
public function __construct(?string $message = null, ?string $since = null) {}
8686
}
87+
88+
/**
89+
* @strict-properties
90+
*/
91+
#[Attribute(Attribute::TARGET_METHOD|Attribute::TARGET_FUNCTION)]
92+
final class NoDiscard
93+
{
94+
public readonly ?string $message;
95+
96+
public function __construct(?string $message = null) {}
97+
}

Zend/zend_attributes_arginfo.h

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)