Skip to content

Improve class inheritance error messages #7307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Zend/tests/enum/final.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Zend/tests/generators/errors/generator_extend_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Zend/tests/traits/error_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Zend/tests/traits/error_009.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Zend/tests/weakrefs/weakrefs_004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 9 additions & 9 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/imap/tests/imap_final.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion ext/reflection/tests/ReflectionAttribute_final.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion ext/standard/tests/class_object/bug78638.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion ext/xml/tests/bug78563_final.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/classes/class_final.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/classes/interface_and_extends.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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