php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Incorrect file/line blamed for errors caused by `new SomeClass`

Open dktapps opened this issue 4 years ago • 4 comments

Description

https://3v4l.org/UtjRl

This code should blame line 4, not line 7, since that's where the bogus constant is actually declared.


An example of where this is more problematic:

SomeClass.php

<?php

class SomeClass{
	public const INGOT = IDontExist::INGOT;
}

test.php

<?php

require 'SomeClass.php';

new SomeClass();

Resulted in this output:

Fatal error: Uncaught Error: Class "IDontExist" not found in C:\stable\test4.php:5

But I expected this output instead:

Fatal error: Uncaught Error: Class "IDontExist" not found in C:\stable\SomeClass.php:4

This frequently causes issues with my telemetry because incorrect subsystems are getting blamed for errors when third-party plugin code comes into play.

PHP Version

8.0.13

Operating System

Windows (doubtful if relevant)

dktapps avatar Dec 13 '21 20:12 dktapps

Reporting the line of the AST evaluation would indeed be preferable, and I believe the necessary information is already present, but not used.

nikic avatar Dec 13 '21 20:12 nikic

Note that this is not a fatal error and thus can be caught. https://3v4l.org/ghG5d#v8.1.0 Not sure what the full stack trace should be if this were to be changed to the position of the constant itself. Just one more frame with the constant AST itself?

iluuu1994 avatar Dec 13 '21 22:12 iluuu1994

See also https://bugs.php.net/bug.php?id=47405.

cmb69 avatar Dec 14 '21 13:12 cmb69

I created a small POC here. https://github.com/php/php-src/compare/master...iluuu1994:gh-7771?expand=1

The approach in the PR is to simply replace filename/lineno in the exception, similar to ErrorException. Since there are tons paths for throwing errors in constant expressions I avoided passing the name/lineno by storing it in the execution globals.

Unfortunately the lineno in the AST somewhere. I'm still trying to figure out why. Let me know if this approach is ok before I spend too much time on this.

iluuu1994 avatar Feb 05 '22 12:02 iluuu1994