Improve class inheritance error messages (#7307)

This commit is contained in:
Máté Kocsis 2021-07-27 09:42:37 +02:00 committed by GitHub
parent a374230c15
commit 663536d7d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 20 additions and 20 deletions

View file

@ -9,4 +9,4 @@ class Bar extends Foo {}
?>
--EXPECTF--
Fatal error: Class Bar may not inherit from final class (Foo) in %s on line %d
Fatal error: Class Bar cannot extend final class Foo in %s on line %d

View file

@ -7,4 +7,4 @@ class ExtendedGenerator extends Generator { }
?>
--EXPECTF--
Fatal error: Class ExtendedGenerator may not inherit from final class (Generator) in %s on line %d
Fatal error: Class ExtendedGenerator cannot extend final class Generator in %s on line %d

View file

@ -25,4 +25,4 @@ class A extends foo {
?>
--EXPECTF--
Fatal error: Class A cannot extend from trait foo in %s on line %d
Fatal error: Class A cannot extend trait foo in %s on line %d

View file

@ -9,4 +9,4 @@ class foo extends abc { }
?>
--EXPECTF--
Fatal error: Class foo cannot extend from trait abc in %s on line %d
Fatal error: Class foo cannot extend trait abc in %s on line %d

View file

@ -5,4 +5,4 @@ WeakReference no inheritance
class Test extends WeakReference {}
?>
--EXPECTF--
Fatal error: Class Test may not inherit from final class (WeakReference) in %s on line %d
Fatal error: Class Test cannot extend final class WeakReference in %s on line %d

View file

@ -1424,19 +1424,19 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
if (UNEXPECTED(ce->ce_flags & ZEND_ACC_INTERFACE)) {
/* Interface can only inherit other interfaces */
if (UNEXPECTED(!(parent_ce->ce_flags & ZEND_ACC_INTERFACE))) {
zend_error_noreturn(E_COMPILE_ERROR, "Interface %s may not inherit from class (%s)", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
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))) {
/* Class declaration must not extend traits or interfaces */
if (parent_ce->ce_flags & ZEND_ACC_INTERFACE) {
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend from interface %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
} else if (parent_ce->ce_flags & ZEND_ACC_TRAIT) {
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend from trait %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 may not inherit from final class (%s)", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend final class %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
}
/* Class declaration must not extend traits or interfaces */
if ((parent_ce->ce_flags & ZEND_ACC_INTERFACE) || (parent_ce->ce_flags & ZEND_ACC_TRAIT)) {
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend %s %s",
ZSTR_VAL(ce->name), parent_ce->ce_flags & ZEND_ACC_INTERFACE ? "interface" : "trait", ZSTR_VAL(parent_ce->name)
);
}
}

View file

@ -7,4 +7,4 @@ imap
class T extends IMAP\Connection {}
--EXPECTF--
Fatal error: Class T may not inherit from final class (IMAP\Connection) in %s on line %d
Fatal error: Class T cannot extend final class IMAP\Connection in %s on line %d

View file

@ -7,4 +7,4 @@ class T extends ReflectionAttribute {}
?>
--EXPECTF--
Fatal error: Class T may not inherit from final class (ReflectionAttribute) in %s on line %d
Fatal error: Class T cannot extend final class ReflectionAttribute in %s on line %d

View file

@ -6,4 +6,4 @@ $c = new class('bar') extends __PHP_Incomplete_Class {
};
?>
--EXPECTF--
Fatal error: Class __PHP_Incomplete_Class@anonymous may not inherit from final class (__PHP_Incomplete_Class) in %s on line %d
Fatal error: Class __PHP_Incomplete_Class@anonymous cannot extend final class __PHP_Incomplete_Class in %s on line %d

View file

@ -12,4 +12,4 @@ class Dummy extends Xmlparser {
?>
===DONE===
--EXPECTF--
Fatal error: Class Dummy may not inherit from final class (XMLParser) in %s on line %d
Fatal error: Class Dummy cannot extend final class XMLParser in %s on line %d

View file

@ -17,4 +17,4 @@ class derived extends base {
echo "Done\n"; // shouldn't be displayed
?>
--EXPECTF--
Fatal error: Class derived may not inherit from final class (base) in %s on line %d
Fatal error: Class derived cannot extend final class base in %s on line %d

View file

@ -21,4 +21,4 @@ $o->show();
?>
===DONE===
--EXPECTF--
Fatal error: Class Tester cannot extend from interface Test in %sinterface_and_extends.php on line %d
Fatal error: Class Tester cannot extend interface Test in %sinterface_and_extends.php on line %d