1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

GH-16315: Improve error messages when extending enums

Closes GH-16491
This commit is contained in:
Daniel Scherzer
2024-10-17 20:09:26 -07:00
committed by Ilija Tovilo
parent 4704f00fc2
commit cb1d4ba97a
4 changed files with 32 additions and 4 deletions

View File

@@ -0,0 +1,10 @@
--TEST--
GH-16315: Extending built-in enum
--FILE--
<?php
class Demo extends RoundingMode {}
?>
--EXPECTF--
Fatal error: Class Demo cannot extend enum RoundingMode in %s on line 3

View File

@@ -0,0 +1,12 @@
--TEST--
GH-16315: Extending userland enum
--FILE--
<?php
enum MyEnum {}
class Demo extends MyEnum {}
?>
--EXPECTF--
Fatal error: Class Demo cannot extend enum MyEnum in %s on line 5

View File

@@ -5,8 +5,9 @@ Enum is final
enum Foo {}
class Bar extends Foo {}
$final = new ReflectionClass(Foo::class)->isFinal();
var_dump($final);
?>
--EXPECTF--
Fatal error: Class Bar cannot extend final class Foo in %s on line %d
--EXPECT--
bool(true)

View File

@@ -1757,7 +1757,12 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
if (UNEXPECTED(!(parent_ce->ce_flags & ZEND_ACC_INTERFACE))) {
zend_error_noreturn(E_COMPILE_ERROR, "Interface %s cannot extend class %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
}
} else if (UNEXPECTED(parent_ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_FINAL))) {
} else if (UNEXPECTED(parent_ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_FINAL|ZEND_ACC_ENUM))) {
/* Class must not extend an enum (GH-16315); check enums first since
* enums are implemented as final classes */
if (parent_ce->ce_flags & ZEND_ACC_ENUM) {
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend enum %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
}
/* Class must not extend a final class */
if (parent_ce->ce_flags & ZEND_ACC_FINAL) {
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend final class %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));