Skip to content

Incorrect file/line blamed for errors caused by new SomeClass #7771

Closed

Description

@dktapps

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)

Activity

  1. nikic commented on Dec 13, 2021

    @nikic
    Member

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

  2. iluuu1994 commented on Dec 13, 2021

    @iluuu1994
    Member

    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?

  3. cmb69 commented on Dec 14, 2021

    @cmb69
    Member
  4. iluuu1994 commented on Feb 5, 2022

    @iluuu1994
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions