Skip to content

Commit cb1d4ba

Browse files
DanielEScherzeriluuu1994
authored andcommitted
phpGH-16315: Improve error messages when extending enums
Closes phpGH-16491
1 parent 4704f00 commit cb1d4ba

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
GH-16315: Extending built-in enum
3+
--FILE--
4+
<?php
5+
6+
class Demo extends RoundingMode {}
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Class Demo cannot extend enum RoundingMode in %s on line 3
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-16315: Extending userland enum
3+
--FILE--
4+
<?php
5+
6+
enum MyEnum {}
7+
8+
class Demo extends MyEnum {}
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Class Demo cannot extend enum MyEnum in %s on line 5

Zend/tests/enum/final.phpt

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ Enum is final
55

66
enum Foo {}
77

8-
class Bar extends Foo {}
8+
$final = new ReflectionClass(Foo::class)->isFinal();
9+
var_dump($final);
910

1011
?>
11-
--EXPECTF--
12-
Fatal error: Class Bar cannot extend final class Foo in %s on line %d
12+
--EXPECT--
13+
bool(true)

Zend/zend_inheritance.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,12 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
17571757
if (UNEXPECTED(!(parent_ce->ce_flags & ZEND_ACC_INTERFACE))) {
17581758
zend_error_noreturn(E_COMPILE_ERROR, "Interface %s cannot extend class %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
17591759
}
1760-
} else if (UNEXPECTED(parent_ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_FINAL))) {
1760+
} else if (UNEXPECTED(parent_ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_FINAL|ZEND_ACC_ENUM))) {
1761+
/* Class must not extend an enum (GH-16315); check enums first since
1762+
* enums are implemented as final classes */
1763+
if (parent_ce->ce_flags & ZEND_ACC_ENUM) {
1764+
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend enum %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
1765+
}
17611766
/* Class must not extend a final class */
17621767
if (parent_ce->ce_flags & ZEND_ACC_FINAL) {
17631768
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend final class %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));

0 commit comments

Comments
 (0)