Skip to content
Draft
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
28 changes: 28 additions & 0 deletions Zend/tests/attributes/nodiscard/class/001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
#[\NoDiscard]: Basic test (Class).
--FILE--
<?php

#[\NoDiscard]
class Test { }
function Test() { return new (__FUNCTION__)(); }

#[\NoDiscard("this is important")]
class Test2 { }
function Test2() { return new (__FUNCTION__)(); }

Test();
Test2();

array_first([new Test(), new Test2()]);
array_last([new Test(), new Test2()]);

?>
--EXPECTF--
Warning: The returned object of class Test should either be used or intentionally ignored by casting it as (void) in %s on line %d

Warning: The returned object of class Test2 should either be used or intentionally ignored by casting it as (void), this is important in %s on line %d

Warning: The returned object of class Test should either be used or intentionally ignored by casting it as (void) in %s on line %d

Warning: The returned object of class Test2 should either be used or intentionally ignored by casting it as (void), this is important in %s on line %d
19 changes: 19 additions & 0 deletions Zend/tests/attributes/nodiscard/class/006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
#[\NoDiscard]: execute_ex overwritten (Class).
--EXTENSIONS--
zend_test
--INI--
zend_test.replace_zend_execute_ex=1
opcache.jit=disable
--FILE--
<?php

#[\NoDiscard]
class Test { }
function Test() { return new (__FUNCTION__)(); }

Test();

?>
--EXPECTF--
Warning: The returned object of class Test should either be used or intentionally ignored by casting it as (void) in %s on line %d
20 changes: 20 additions & 0 deletions Zend/tests/attributes/nodiscard/class/suppress_cast.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
#[\NoDiscard]: Casting to (void) suppresses (Class).
--FILE--
<?php

#[\NoDiscard]
class Test { }
function Test() { return new (__FUNCTION__)(); }

#[\NoDiscard("this is important")]
class Test2 { }
function Test2() { return new (__FUNCTION__)(); }

(void)Test();
(void)Test2();

?>
DONE
--EXPECT--
DONE
24 changes: 16 additions & 8 deletions Zend/zend_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,23 @@ ZEND_METHOD(Deprecated, __construct)
static zend_string *validate_nodiscard(
zend_attribute *attr, uint32_t target, zend_class_entry *scope)
{
ZEND_ASSERT(CG(in_compilation));
const zend_string *prop_info_name = CG(context).active_property_info_name;
if (prop_info_name != NULL) {
// Applied to a hook
return ZSTR_INIT_LITERAL("#[\\NoDiscard] is not supported for property hooks", 0);
switch (target) {
case ZEND_ATTRIBUTE_TARGET_FUNCTION:
case ZEND_ATTRIBUTE_TARGET_METHOD:
ZEND_ASSERT(CG(in_compilation));
const zend_string *prop_info_name = CG(context).active_property_info_name;
if (prop_info_name != NULL) {
// Applied to a hook
return ZSTR_INIT_LITERAL("#[\\NoDiscard] is not supported for property hooks", 0);
}
zend_op_array *op_array = CG(active_op_array);
op_array->fn_flags |= ZEND_ACC_NODISCARD;
return NULL;
case ZEND_ATTRIBUTE_TARGET_CLASS:
scope->ce_flags |= ZEND_ACC_NODISCARD;
return NULL;
default: ZEND_UNREACHABLE();
}
zend_op_array *op_array = CG(active_op_array);
op_array->fn_flags |= ZEND_ACC_NODISCARD;
return NULL;
}

ZEND_METHOD(NoDiscard, __construct)
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_attributes.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function __construct(?string $message = null, ?string $since = null) {}
/**
* @strict-properties
*/
#[Attribute(Attribute::TARGET_METHOD|Attribute::TARGET_FUNCTION)]
#[Attribute(Attribute::TARGET_METHOD|Attribute::TARGET_FUNCTION|Attribute::TARGET_CLASS)]
final class NoDiscard
{
public readonly ?string $message;
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_attributes_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ typedef struct _zend_oparray_context {
/* has #[\Override] attribute | | | */
#define ZEND_ACC_OVERRIDE (1 << 28) /* | X | X | */
/* | | | */
/* has #[\NoDiscard] attribute | | | */
#define ZEND_ACC_NODISCARD (1 << 29) /* X | X | | */
/* | | | */
/* Property Flags (unused: 13-27,29...) | | | */
/* =========== | | | */
/* | | | */
Expand All @@ -275,7 +278,7 @@ typedef struct _zend_oparray_context {
#define ZEND_ACC_PROTECTED_SET (1 << 11) /* | | X | */
#define ZEND_ACC_PRIVATE_SET (1 << 12) /* | | X | */
/* | | | */
/* Class Flags (unused: 31) | | | */
/* Class Flags (unused: -) | | | */
/* =========== | | | */
/* | | | */
/* Special class types | | | */
Expand Down Expand Up @@ -339,7 +342,7 @@ typedef struct _zend_oparray_context {
#define ZEND_ACC_FILE_CACHED (1 << 27) /* X | | | */
/* | | | */
/* Class cannot be serialized or unserialized | | | */
#define ZEND_ACC_NOT_SERIALIZABLE (1 << 29) /* X | | | */
#define ZEND_ACC_NOT_SERIALIZABLE (1 << 31) /* X | | | */
/* | | | */
/* Class Flags 2 (ce_flags2) (unused: 0-31) | | | */
/* ========================= | | | */
Expand Down Expand Up @@ -402,9 +405,6 @@ typedef struct _zend_oparray_context {
/* supports opcache compile-time evaluation (funcs) | | | */
#define ZEND_ACC_COMPILE_TIME_EVAL (1 << 27) /* | X | | */
/* | | | */
/* has #[\NoDiscard] attribute | | | */
#define ZEND_ACC_NODISCARD (1 << 29) /* | X | | */
/* | | | */
/* op_array uses strict mode types | | | */
#define ZEND_ACC_STRICT_TYPES (1U << 31) /* | X | | */
/* | | | */
Expand Down
18 changes: 18 additions & 0 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,24 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_nodiscard_function(const zend_functio
zend_string_release(message_suffix);
}

ZEND_API ZEND_COLD void ZEND_FASTCALL zend_nodiscard_ce(zend_class_entry *ce)
{
zend_string *message_suffix = ZSTR_EMPTY_ALLOC();

if (get_nodiscard_suffix_from_attribute(ce->attributes, ce, &message_suffix) == FAILURE) {
return;
}

int code = ce->type == ZEND_INTERNAL_CLASS ? E_WARNING : E_USER_WARNING;

zend_error(code, "The returned object of class %pS should either be used or intentionally ignored by casting it as (void)%pS",
ce->name,
message_suffix
);

zend_string_release(message_suffix);
}

ZEND_API ZEND_COLD void ZEND_FASTCALL zend_deprecated_class_constant(const zend_class_constant *c, const zend_string *constant_name)
{
zend_string *message_suffix = ZSTR_EMPTY_ALLOC();
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ extern ZEND_API const zend_internal_function zend_pass_function;
ZEND_API zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_missing_arg_error(const zend_execute_data *execute_data);
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_deprecated_function(const zend_function *fbc);
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_nodiscard_function(const zend_function *fbc);
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_nodiscard_ce(zend_class_entry *ce);
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_deprecated_class_constant(const zend_class_constant *c, const zend_string *constant_name);
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_deprecated_constant(const zend_constant *c, const zend_string *constant_name);
ZEND_API ZEND_COLD void zend_use_of_deprecated_trait(zend_class_entry *trait, const zend_string *used_by);
Expand Down
32 changes: 29 additions & 3 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -4184,6 +4184,12 @@ ZEND_VM_HOT_HANDLER(129, ZEND_DO_ICALL, ANY, ANY, SPEC(RETVAL,OBSERVER))
}

if (!RETURN_VALUE_USED(opline)) {
if (UNEXPECTED(
Z_TYPE_P(ret) == IS_OBJECT
&& (Z_OBJCE_P(ret)->ce_flags & ZEND_ACC_NODISCARD)
)) {
zend_nodiscard_ce(Z_OBJCE_P(ret));
}
i_zval_ptr_dtor(ret);
}

Expand Down Expand Up @@ -4325,6 +4331,12 @@ ZEND_VM_C_LABEL(fcall_by_name_end):
}

if (!RETURN_VALUE_USED(opline)) {
if (UNEXPECTED(
Z_TYPE_P(ret) == IS_OBJECT
&& (Z_OBJCE_P(ret)->ce_flags & ZEND_ACC_NODISCARD)
)) {
zend_nodiscard_ce(Z_OBJCE_P(ret));
}
i_zval_ptr_dtor(ret);
}
}
Expand Down Expand Up @@ -4451,6 +4463,12 @@ ZEND_VM_C_LABEL(fcall_end):
}

if (!RETURN_VALUE_USED(opline)) {
if (UNEXPECTED(
Z_TYPE_P(ret) == IS_OBJECT
&& (Z_OBJCE_P(ret)->ce_flags & ZEND_ACC_NODISCARD)
)) {
zend_nodiscard_ce(Z_OBJCE_P(ret));
}
i_zval_ptr_dtor(ret);
}
}
Expand Down Expand Up @@ -4560,9 +4578,17 @@ ZEND_VM_INLINE_HANDLER(62, ZEND_RETURN, CONST|TMP|CV, ANY, SPEC(OBSERVER))
}
} else if (!return_value) {
if (OP1_TYPE & (IS_VAR|IS_TMP_VAR)) {
if (Z_REFCOUNTED_P(retval_ptr) && !Z_DELREF_P(retval_ptr)) {
SAVE_OPLINE();
rc_dtor_func(Z_COUNTED_P(retval_ptr));
if (Z_REFCOUNTED_P(retval_ptr)) {
if (
Z_TYPE_P(retval_ptr) == IS_OBJECT
&& UNEXPECTED(Z_OBJCE_P(retval_ptr)->ce_flags & ZEND_ACC_NODISCARD)
) {
zend_nodiscard_ce(Z_OBJCE_P(retval_ptr));
}
if (!Z_DELREF_P(retval_ptr)) {
SAVE_OPLINE();
rc_dtor_func(Z_COUNTED_P(retval_ptr));
}
}
}
} else {
Expand Down
Loading
Loading