From 81b5cca329a392270cfae49ce0a442f3f33c051a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Sun, 22 Nov 2026 21:39:56 +0100 Subject: [PATCH 1/2] Support #[\NoDiscard] on classes --- .../tests/attributes/nodiscard/class/001.phpt | 28 ++ .../tests/attributes/nodiscard/class/006.phpt | 19 ++ .../nodiscard/class/suppress_cast.phpt | 20 ++ .../nodiscard/{ => function}/001.phpt | 0 .../nodiscard/{ => function}/002.phpt | 0 .../nodiscard/{ => function}/003.phpt | 0 .../nodiscard/{ => function}/005.phpt | 0 .../nodiscard/{ => function}/006.phpt | 0 .../nodiscard/{ => function}/007.phpt | 0 .../nodiscard/{ => function}/008.phpt | 0 .../nodiscard/{ => function}/009.phpt | 0 .../nodiscard/{ => function}/010.phpt | 0 .../{ => function}/error_code_001.phpt | 0 .../{ => function}/suppress_assign.phpt | 0 .../{ => function}/suppress_cast.phpt | 0 .../throwing_error_handler_001.phpt | 0 .../{ => function}/unsupported_clone.phpt | 0 .../unsupported_constructor.phpt | 0 .../unsupported_never_function.phpt | 0 .../unsupported_property_hook_get.phpt | 0 .../unsupported_property_hook_set.phpt | 0 .../unsupported_void_function.phpt | 0 Zend/zend_attributes.c | 24 +- Zend/zend_attributes.stub.php | 2 +- Zend/zend_attributes_arginfo.h | 4 +- Zend/zend_compile.h | 6 +- Zend/zend_execute.c | 18 ++ Zend/zend_execute.h | 1 + Zend/zend_vm_def.h | 32 +- Zend/zend_vm_execute.h | 276 +++++++++++++++--- 30 files changed, 377 insertions(+), 53 deletions(-) create mode 100644 Zend/tests/attributes/nodiscard/class/001.phpt create mode 100644 Zend/tests/attributes/nodiscard/class/006.phpt create mode 100644 Zend/tests/attributes/nodiscard/class/suppress_cast.phpt rename Zend/tests/attributes/nodiscard/{ => function}/001.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/002.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/003.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/005.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/006.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/007.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/008.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/009.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/010.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/error_code_001.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/suppress_assign.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/suppress_cast.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/throwing_error_handler_001.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/unsupported_clone.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/unsupported_constructor.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/unsupported_never_function.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/unsupported_property_hook_get.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/unsupported_property_hook_set.phpt (100%) rename Zend/tests/attributes/nodiscard/{ => function}/unsupported_void_function.phpt (100%) diff --git a/Zend/tests/attributes/nodiscard/class/001.phpt b/Zend/tests/attributes/nodiscard/class/001.phpt new file mode 100644 index 000000000000..ecbab1e6f777 --- /dev/null +++ b/Zend/tests/attributes/nodiscard/class/001.phpt @@ -0,0 +1,28 @@ +--TEST-- +#[\NoDiscard]: Basic test (Class). +--FILE-- + +--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 diff --git a/Zend/tests/attributes/nodiscard/class/006.phpt b/Zend/tests/attributes/nodiscard/class/006.phpt new file mode 100644 index 000000000000..ccd55bbc543d --- /dev/null +++ b/Zend/tests/attributes/nodiscard/class/006.phpt @@ -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-- + +--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 diff --git a/Zend/tests/attributes/nodiscard/class/suppress_cast.phpt b/Zend/tests/attributes/nodiscard/class/suppress_cast.phpt new file mode 100644 index 000000000000..da67366546c4 --- /dev/null +++ b/Zend/tests/attributes/nodiscard/class/suppress_cast.phpt @@ -0,0 +1,20 @@ +--TEST-- +#[\NoDiscard]: Casting to (void) suppresses (Class). +--FILE-- + +DONE +--EXPECT-- +DONE diff --git a/Zend/tests/attributes/nodiscard/001.phpt b/Zend/tests/attributes/nodiscard/function/001.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/001.phpt rename to Zend/tests/attributes/nodiscard/function/001.phpt diff --git a/Zend/tests/attributes/nodiscard/002.phpt b/Zend/tests/attributes/nodiscard/function/002.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/002.phpt rename to Zend/tests/attributes/nodiscard/function/002.phpt diff --git a/Zend/tests/attributes/nodiscard/003.phpt b/Zend/tests/attributes/nodiscard/function/003.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/003.phpt rename to Zend/tests/attributes/nodiscard/function/003.phpt diff --git a/Zend/tests/attributes/nodiscard/005.phpt b/Zend/tests/attributes/nodiscard/function/005.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/005.phpt rename to Zend/tests/attributes/nodiscard/function/005.phpt diff --git a/Zend/tests/attributes/nodiscard/006.phpt b/Zend/tests/attributes/nodiscard/function/006.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/006.phpt rename to Zend/tests/attributes/nodiscard/function/006.phpt diff --git a/Zend/tests/attributes/nodiscard/007.phpt b/Zend/tests/attributes/nodiscard/function/007.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/007.phpt rename to Zend/tests/attributes/nodiscard/function/007.phpt diff --git a/Zend/tests/attributes/nodiscard/008.phpt b/Zend/tests/attributes/nodiscard/function/008.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/008.phpt rename to Zend/tests/attributes/nodiscard/function/008.phpt diff --git a/Zend/tests/attributes/nodiscard/009.phpt b/Zend/tests/attributes/nodiscard/function/009.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/009.phpt rename to Zend/tests/attributes/nodiscard/function/009.phpt diff --git a/Zend/tests/attributes/nodiscard/010.phpt b/Zend/tests/attributes/nodiscard/function/010.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/010.phpt rename to Zend/tests/attributes/nodiscard/function/010.phpt diff --git a/Zend/tests/attributes/nodiscard/error_code_001.phpt b/Zend/tests/attributes/nodiscard/function/error_code_001.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/error_code_001.phpt rename to Zend/tests/attributes/nodiscard/function/error_code_001.phpt diff --git a/Zend/tests/attributes/nodiscard/suppress_assign.phpt b/Zend/tests/attributes/nodiscard/function/suppress_assign.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/suppress_assign.phpt rename to Zend/tests/attributes/nodiscard/function/suppress_assign.phpt diff --git a/Zend/tests/attributes/nodiscard/suppress_cast.phpt b/Zend/tests/attributes/nodiscard/function/suppress_cast.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/suppress_cast.phpt rename to Zend/tests/attributes/nodiscard/function/suppress_cast.phpt diff --git a/Zend/tests/attributes/nodiscard/throwing_error_handler_001.phpt b/Zend/tests/attributes/nodiscard/function/throwing_error_handler_001.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/throwing_error_handler_001.phpt rename to Zend/tests/attributes/nodiscard/function/throwing_error_handler_001.phpt diff --git a/Zend/tests/attributes/nodiscard/unsupported_clone.phpt b/Zend/tests/attributes/nodiscard/function/unsupported_clone.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/unsupported_clone.phpt rename to Zend/tests/attributes/nodiscard/function/unsupported_clone.phpt diff --git a/Zend/tests/attributes/nodiscard/unsupported_constructor.phpt b/Zend/tests/attributes/nodiscard/function/unsupported_constructor.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/unsupported_constructor.phpt rename to Zend/tests/attributes/nodiscard/function/unsupported_constructor.phpt diff --git a/Zend/tests/attributes/nodiscard/unsupported_never_function.phpt b/Zend/tests/attributes/nodiscard/function/unsupported_never_function.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/unsupported_never_function.phpt rename to Zend/tests/attributes/nodiscard/function/unsupported_never_function.phpt diff --git a/Zend/tests/attributes/nodiscard/unsupported_property_hook_get.phpt b/Zend/tests/attributes/nodiscard/function/unsupported_property_hook_get.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/unsupported_property_hook_get.phpt rename to Zend/tests/attributes/nodiscard/function/unsupported_property_hook_get.phpt diff --git a/Zend/tests/attributes/nodiscard/unsupported_property_hook_set.phpt b/Zend/tests/attributes/nodiscard/function/unsupported_property_hook_set.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/unsupported_property_hook_set.phpt rename to Zend/tests/attributes/nodiscard/function/unsupported_property_hook_set.phpt diff --git a/Zend/tests/attributes/nodiscard/unsupported_void_function.phpt b/Zend/tests/attributes/nodiscard/function/unsupported_void_function.phpt similarity index 100% rename from Zend/tests/attributes/nodiscard/unsupported_void_function.phpt rename to Zend/tests/attributes/nodiscard/function/unsupported_void_function.phpt diff --git a/Zend/zend_attributes.c b/Zend/zend_attributes.c index 3ed8739c3ed6..bd3d8263c3cc 100644 --- a/Zend/zend_attributes.c +++ b/Zend/zend_attributes.c @@ -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) diff --git a/Zend/zend_attributes.stub.php b/Zend/zend_attributes.stub.php index c412d39688ac..f3a6ecad3dc8 100644 --- a/Zend/zend_attributes.stub.php +++ b/Zend/zend_attributes.stub.php @@ -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; diff --git a/Zend/zend_attributes_arginfo.h b/Zend/zend_attributes_arginfo.h index 2c4bfc99e1ef..edf1b1ea0e47 100644 --- a/Zend/zend_attributes_arginfo.h +++ b/Zend/zend_attributes_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit zend_attributes.stub.php instead. - * Stub hash: dc2b1de9f4d91162f0e9ab236272f79019c5b5ab */ + * Stub hash: 305e50f24064870fafe0f48d80028212b2e8be72 */ #include "zend_attributes.h" @@ -274,7 +274,7 @@ static zend_class_entry *register_class_NoDiscard(void) zend_string *attribute_name_Attribute_class_NoDiscard_0 = zend_string_init_interned("Attribute", sizeof("Attribute") - 1, true); zend_attribute *attribute_Attribute_class_NoDiscard_0 = zend_add_class_attribute(class_entry, attribute_name_Attribute_class_NoDiscard_0, 1); zend_string_release_ex(attribute_name_Attribute_class_NoDiscard_0, true); - ZVAL_LONG(&attribute_Attribute_class_NoDiscard_0->args[0].value, ZEND_ATTRIBUTE_TARGET_METHOD | ZEND_ATTRIBUTE_TARGET_FUNCTION); + ZVAL_LONG(&attribute_Attribute_class_NoDiscard_0->args[0].value, ZEND_ATTRIBUTE_TARGET_METHOD | ZEND_ATTRIBUTE_TARGET_FUNCTION | ZEND_ATTRIBUTE_TARGET_CLASS); return class_entry; } diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h index e82d4212116f..f1d3126876bf 100644 --- a/Zend/zend_compile.h +++ b/Zend/zend_compile.h @@ -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...) | | | */ /* =========== | | | */ /* | | | */ @@ -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 | | */ /* | | | */ diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 7d270c3e249d..29cf453e6b3e 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -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(); diff --git a/Zend/zend_execute.h b/Zend/zend_execute.h index 82d4af25d914..6496dadf325f 100644 --- a/Zend/zend_execute.h +++ b/Zend/zend_execute.h @@ -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); diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 00ad5c5ffbb7..63f160bd5df5 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -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); } @@ -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); } } @@ -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); } } @@ -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 { diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 406e2e416caa..5017d40f4760 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -1356,6 +1356,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_D } if (!0) { + 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); } @@ -1426,6 +1432,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_D } if (!1) { + 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); } @@ -1495,6 +1507,12 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_ } 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); } @@ -1690,6 +1708,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_D } if (!0) { + 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); } } @@ -1808,6 +1832,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_D } if (!1) { + 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); } } @@ -1924,6 +1954,12 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_ } 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); } } @@ -2054,6 +2090,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_D } if (!0) { + 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); } } @@ -2189,6 +2231,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_D } if (!1) { + 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); } } @@ -2320,6 +2368,12 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_ } 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); } } @@ -4776,9 +4830,17 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_R } } else if (!return_value) { if (IS_CONST & (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 { @@ -4857,9 +4919,17 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_ } } else if (!return_value) { if (opline->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 { @@ -17218,9 +17288,17 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_R } } else if (!return_value) { if (IS_TMP_VAR & (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 { @@ -39985,9 +40063,17 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_R } } else if (!return_value) { if (IS_CV & (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 { @@ -54346,6 +54432,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DO_ICA } if (!0) { + 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); } @@ -54416,6 +54508,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DO_ICA } if (!1) { + 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); } @@ -54485,6 +54583,12 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DO_IC } 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); } @@ -54680,6 +54784,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DO_FCA } if (!0) { + 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); } } @@ -54798,6 +54908,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DO_FCA } if (!1) { + 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); } } @@ -54914,6 +55030,12 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DO_FC } 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); } } @@ -55044,6 +55166,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DO_FCA } if (!0) { + 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); } } @@ -55179,6 +55307,12 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DO_FCA } if (!1) { + 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); } } @@ -55310,6 +55444,12 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_DO_FC } 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); } } @@ -57648,9 +57788,17 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_RETURN } } else if (!return_value) { if (IS_CONST & (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 { @@ -57729,9 +57877,17 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_RETUR } } else if (!return_value) { if (opline->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 { @@ -69988,9 +70144,17 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_RETURN } } else if (!return_value) { if (IS_TMP_VAR & (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 { @@ -92655,9 +92819,17 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_RETURN } } else if (!return_value) { if (IS_CV & (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 { @@ -111141,9 +111313,17 @@ ZEND_API void execute_ex(zend_execute_data *ex) } } else if (!return_value) { if (IS_CONST & (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 { @@ -111224,9 +111404,17 @@ ZEND_API void execute_ex(zend_execute_data *ex) } } else if (!return_value) { if (opline->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 { @@ -112860,9 +113048,17 @@ ZEND_API void execute_ex(zend_execute_data *ex) } } else if (!return_value) { if (IS_TMP_VAR & (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 { @@ -114749,9 +114945,17 @@ ZEND_API void execute_ex(zend_execute_data *ex) } } else if (!return_value) { if (IS_CV & (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 { From 75edc17ad0b8327c634543ced5aea21ba808b8db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Sun, 22 Nov 2026 22:14:22 +0100 Subject: [PATCH 2/2] Fix overloaded class flag --- Zend/zend_compile.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h index f1d3126876bf..7534c472e9b1 100644 --- a/Zend/zend_compile.h +++ b/Zend/zend_compile.h @@ -278,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 | | | */ @@ -342,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) | | | */ /* ========================= | | | */