Fix lineno for more inheritance errors

And also include explicit linenos in tests.
This commit is contained in:
Nikita Popov 2019-03-27 13:02:28 +01:00
parent 251f293cb7
commit d1e5006c14
20 changed files with 56 additions and 24 deletions

View File

@ -11,4 +11,4 @@ class Bar extends Foo {
protected function __construct(){}
}
--EXPECTF--
Fatal error: Access level to Bar::__construct() must be public (as in class Foo) in %s
Fatal error: Access level to Bar::__construct() must be public (as in class Foo) in %s on line 8

View File

@ -15,4 +15,4 @@ class Baz extends Bar {
protected function __construct(){}
}
--EXPECTF--
Fatal error: Access level to Baz::__construct() must be public (as in class Bar) in %s
Fatal error: Access level to Baz::__construct() must be public (as in class Bar) in %s on line 12

View File

@ -17,4 +17,4 @@ class C extends B {
?>
--EXPECTF--
Fatal error: Access level to C::test() must be protected (as in class B) or weaker in %s on line %d
Fatal error: Access level to C::test() must be protected (as in class B) or weaker in %s on line 12

View File

@ -17,4 +17,4 @@ interface a extends d, w { }
?>
--EXPECTF--
Fatal error: Cannot make non static method c::B() static in class d in %s on line %d
Fatal error: Cannot make non static method c::B() static in class d in %s on line 4

View File

@ -16,4 +16,4 @@ class a extends b {
--EXPECTF--
Warning: The magic method __set() must have public visibility and cannot be static in %s on line %d
Fatal error: Access level to a::__set() must be public (as in class b) in %s on line %d
Fatal error: Access level to a::__set() must be public (as in class b) in %s on line 8

View File

@ -1473,6 +1473,24 @@ ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) {
va_end(args);
}
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_at_noreturn(
int type, const char *filename, uint32_t lineno, const char *format, ...)
{
va_list args;
if (!filename) {
uint32_t dummy_lineno;
get_filename_lineno(type, &filename, &dummy_lineno);
}
va_start(args, format);
zend_error_va_list(type, filename, lineno, format, args);
va_end(args);
/* Should never reach this. */
abort();
}
/* }}} */
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...)
{
const char *filename;

View File

@ -296,6 +296,7 @@ ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) ZEND_ATTRI
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
/* If filename is NULL the default filename is used. */
ZEND_API ZEND_COLD void zend_error_at(int type, const char *filename, uint32_t lineno, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 4, 5);
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_at_noreturn(int type, const char *filename, uint32_t lineno, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 4, 5);
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2);

View File

@ -546,13 +546,19 @@ static ZEND_COLD zend_string *zend_get_function_declaration(const zend_function
}
/* }}} */
static zend_always_inline uint32_t func_lineno(zend_function *fn) {
return fn->common.type == ZEND_USER_FUNCTION ? fn->op_array.line_start : 0;
}
static void do_inheritance_check_on_method(zend_function *child, zend_function *parent, zend_class_entry *ce, zval *child_zv) /* {{{ */
{
uint32_t child_flags;
uint32_t parent_flags = parent->common.fn_flags;
if (UNEXPECTED(parent_flags & ZEND_ACC_FINAL)) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot override final method %s::%s()", ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name));
zend_error_at_noreturn(E_COMPILE_ERROR, NULL, func_lineno(child),
"Cannot override final method %s::%s()",
ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name));
}
child_flags = child->common.fn_flags;
@ -560,15 +566,21 @@ static void do_inheritance_check_on_method(zend_function *child, zend_function *
*/
if (UNEXPECTED((child_flags & ZEND_ACC_STATIC) != (parent_flags & ZEND_ACC_STATIC))) {
if (child_flags & ZEND_ACC_STATIC) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot make non static method %s::%s() static in class %s", ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name), ZEND_FN_SCOPE_NAME(child));
zend_error_at_noreturn(E_COMPILE_ERROR, NULL, func_lineno(child),
"Cannot make non static method %s::%s() static in class %s",
ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name), ZEND_FN_SCOPE_NAME(child));
} else {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot make static method %s::%s() non static in class %s", ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name), ZEND_FN_SCOPE_NAME(child));
zend_error_at_noreturn(E_COMPILE_ERROR, NULL, func_lineno(child),
"Cannot make static method %s::%s() non static in class %s",
ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name), ZEND_FN_SCOPE_NAME(child));
}
}
/* Disallow making an inherited method abstract. */
if (UNEXPECTED((child_flags & ZEND_ACC_ABSTRACT) > (parent_flags & ZEND_ACC_ABSTRACT))) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot make non abstract method %s::%s() abstract in class %s", ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name), ZEND_FN_SCOPE_NAME(child));
zend_error_at_noreturn(E_COMPILE_ERROR, NULL, func_lineno(child),
"Cannot make non abstract method %s::%s() abstract in class %s",
ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name), ZEND_FN_SCOPE_NAME(child));
}
if (parent_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_CHANGED)) {
@ -615,7 +627,9 @@ static void do_inheritance_check_on_method(zend_function *child, zend_function *
}
/* Prevent derived classes from restricting access that was available in parent classes (except deriving from non-abstract ctors) */
if ((child_flags & ZEND_ACC_PPP_MASK) > (parent_flags & ZEND_ACC_PPP_MASK)) {
zend_error_noreturn(E_COMPILE_ERROR, "Access level to %s::%s() must be %s (as in class %s)%s", ZEND_FN_SCOPE_NAME(child), ZSTR_VAL(child->common.function_name), zend_visibility_string(parent_flags), ZEND_FN_SCOPE_NAME(parent), (parent_flags&ZEND_ACC_PUBLIC) ? "" : " or weaker");
zend_error_at_noreturn(E_COMPILE_ERROR, NULL, func_lineno(child),
"Access level to %s::%s() must be %s (as in class %s)%s",
ZEND_FN_SCOPE_NAME(child), ZSTR_VAL(child->common.function_name), zend_visibility_string(parent_flags), ZEND_FN_SCOPE_NAME(parent), (parent_flags&ZEND_ACC_PUBLIC) ? "" : " or weaker");
}
if (UNEXPECTED(!zend_do_perform_implementation_check(child, parent))) {
@ -639,8 +653,7 @@ static void do_inheritance_check_on_method(zend_function *child, zend_function *
error_level = E_WARNING;
error_verb = "should";
}
zend_error_at(error_level, NULL,
child->common.type == ZEND_USER_FUNCTION ? child->op_array.line_start : 0,
zend_error_at(error_level, NULL, func_lineno(child),
"Declaration of %s %s be compatible with %s",
ZSTR_VAL(child_prototype), error_verb, ZSTR_VAL(method_prototype));
zend_string_efree(child_prototype);

View File

@ -16,4 +16,4 @@ class test extends base {
?>
--EXPECTF--
Fatal error: Cannot override final method base::__clone() in %sclone_005.php on line %d
Fatal error: Cannot override final method base::__clone() in %sclone_005.php on line 11

View File

@ -12,4 +12,4 @@ Ensure implicit final inherited old-style constructor cannot be overridden.
--EXPECTF--
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; A has a deprecated constructor in %s on line %d
Fatal error: Cannot override final method A::A() in %s on line %d
Fatal error: Cannot override final method A::A() in %s on line 6

View File

@ -20,4 +20,4 @@ class fail extends pass {
echo "Done\n"; // Shouldn't be displayed
?>
--EXPECTF--
Fatal error: Cannot override final method pass::show() in %s on line %d
Fatal error: Cannot override final method pass::show() in %s on line 12

View File

@ -21,4 +21,4 @@ fail::show();
echo "Done\n"; // shouldn't be displayed
?>
--EXPECTF--
Fatal error: Cannot make static method pass::show() non static in class fail in %s on line %d
Fatal error: Cannot make static method pass::show() non static in class fail in %s on line 10

View File

@ -22,4 +22,4 @@ fail::show();
echo "Done\n"; // shouldn't be displayed
?>
--EXPECTF--
Fatal error: Cannot make non static method pass::show() static in class fail in %s on line %d
Fatal error: Cannot make non static method pass::show() static in class fail in %s on line 10

View File

@ -28,4 +28,4 @@ class fail extends same {
echo "Done\n"; // shouldn't be displayed
?>
--EXPECTF--
Fatal error: Access level to fail::f0() must be public (as in class same) in %s on line %d
Fatal error: Access level to fail::f0() must be public (as in class same) in %s on line 22

View File

@ -28,4 +28,4 @@ class fail extends same {
echo "Done\n"; // shouldn't be displayed
?>
--EXPECTF--
Fatal error: Access level to fail::f0() must be public (as in class same) in %s on line %d
Fatal error: Access level to fail::f0() must be public (as in class same) in %s on line 22

View File

@ -28,4 +28,4 @@ class fail extends same {
echo "Done\n"; // shouldn't be displayed
?>
--EXPECTF--
Fatal error: Access level to fail::f1() must be public (as in class same) in %s on line %d
Fatal error: Access level to fail::f1() must be public (as in class same) in %s on line 22

View File

@ -28,4 +28,4 @@ class fail extends same {
echo "Done\n"; // shouldn't be displayed
?>
--EXPECTF--
Fatal error: Access level to fail::f1() must be public (as in class same) in %s on line %d
Fatal error: Access level to fail::f1() must be public (as in class same) in %s on line 22

View File

@ -28,4 +28,4 @@ class fail extends same {
echo "Done\n"; // shouldn't be displayed
?>
--EXPECTF--
Fatal error: Access level to fail::f2() must be public (as in class same) in %s on line %d
Fatal error: Access level to fail::f2() must be public (as in class same) in %s on line 22

View File

@ -28,4 +28,4 @@ class fail extends same {
echo "Done\n"; // shouldn't be displayed
?>
--EXPECTF--
Fatal error: Access level to fail::f2() must be public (as in class same) in %s on line %d
Fatal error: Access level to fail::f2() must be public (as in class same) in %s on line 22

View File

@ -28,4 +28,4 @@ class fail extends same {
echo "Done\n"; // shouldn't be displayed
?>
--EXPECTF--
Fatal error: Access level to fail::f3() must be protected (as in class same) or weaker in %s on line %d
Fatal error: Access level to fail::f3() must be protected (as in class same) or weaker in %s on line 22