From daeeb16924ed350effb1ba004efd12a50e44c353 Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Mon, 14 Oct 2024 23:14:24 -0700 Subject: [PATCH] GH-16447: improve enums' error when missing interface's hooked properties Enums cannot have properties, and so can not be made to satisfy the interface at all; show a clearer error message than the suggestion to implement the hooked properties. --- Zend/tests/property_hooks/gh16447_1.phpt | 14 ++++++++++++++ Zend/tests/property_hooks/gh16447_2.phpt | 14 ++++++++++++++ Zend/tests/property_hooks/gh16447_3.phpt | 15 +++++++++++++++ Zend/tests/property_hooks/gh16447_4.phpt | 15 +++++++++++++++ Zend/zend_inheritance.c | 12 ++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 Zend/tests/property_hooks/gh16447_1.phpt create mode 100644 Zend/tests/property_hooks/gh16447_2.phpt create mode 100644 Zend/tests/property_hooks/gh16447_3.phpt create mode 100644 Zend/tests/property_hooks/gh16447_4.phpt diff --git a/Zend/tests/property_hooks/gh16447_1.phpt b/Zend/tests/property_hooks/gh16447_1.phpt new file mode 100644 index 0000000000000..73999a1204149 --- /dev/null +++ b/Zend/tests/property_hooks/gh16447_1.phpt @@ -0,0 +1,14 @@ +--TEST-- +GH-16447: Enum error messages should not suggest adding methods (get) +--FILE-- + +--EXPECTF-- +Fatal error: Enum E cannot implement interface I that contains hooked property I::$prop in %s on line %d diff --git a/Zend/tests/property_hooks/gh16447_2.phpt b/Zend/tests/property_hooks/gh16447_2.phpt new file mode 100644 index 0000000000000..6c6bbc2e4203c --- /dev/null +++ b/Zend/tests/property_hooks/gh16447_2.phpt @@ -0,0 +1,14 @@ +--TEST-- +GH-16447: Enum error messages should not suggest adding methods (set) +--FILE-- + +--EXPECTF-- +Fatal error: Enum E cannot implement interface I that contains hooked property I::$prop in %s on line %d diff --git a/Zend/tests/property_hooks/gh16447_3.phpt b/Zend/tests/property_hooks/gh16447_3.phpt new file mode 100644 index 0000000000000..3843c9cc76448 --- /dev/null +++ b/Zend/tests/property_hooks/gh16447_3.phpt @@ -0,0 +1,15 @@ +--TEST-- +GH-16447: Enum error messages should not suggest adding methods ($name get allowed) +--FILE-- + +OKAY +--EXPECT-- +OKAY diff --git a/Zend/tests/property_hooks/gh16447_4.phpt b/Zend/tests/property_hooks/gh16447_4.phpt new file mode 100644 index 0000000000000..2f3ba8e53e978 --- /dev/null +++ b/Zend/tests/property_hooks/gh16447_4.phpt @@ -0,0 +1,15 @@ +--TEST-- +GH-16447: Enum error messages should not suggest adding methods ($value get allowed on backed enum) +--FILE-- + +OKAY +--EXPECT-- +OKAY diff --git a/Zend/zend_inheritance.c b/Zend/zend_inheritance.c index f34b8d39323dd..ef54c6186caec 100644 --- a/Zend/zend_inheritance.c +++ b/Zend/zend_inheritance.c @@ -3002,6 +3002,18 @@ void zend_verify_abstract_class(zend_class_entry *ce) /* {{{ */ const zend_function *fn = prop_info->hooks[i]; if (fn && (fn->common.fn_flags & ZEND_ACC_ABSTRACT)) { zend_verify_abstract_class_function(fn, &ai); + // Short-circuit on enums that cannot have hooked + // properties + if (ce->ce_flags & ZEND_ACC_ENUM) { + zend_error_noreturn( + E_ERROR, + "Enum %s cannot implement interface %s that contains hooked property %s::$%s", + ZSTR_VAL(ce->name), + ZSTR_VAL(prop_info->ce->name), + ZSTR_VAL(prop_info->ce->name), + ZSTR_VAL(prop_info->name) + ); + } } } }