Bug #70210 [NEW]: Parser: some ASTs may get wrong line numbers
From: duckki at synopsys dot com
Operating system:
PHP version: 7.0.0beta3
Package: *Compile Issues
Bug Type: Bug
Bug description:Parser: some ASTs may get wrong line numbers
Description:
------------
I've noticed some AST nodes (arrays, ++, yield, etc.) may have the wrong
line numbers. Here's an example PHP code:
1: <?php
2: yield
3:
4: // gap
5:
6: function() { yield; };
PHP 7.0.0beta3 will report:
Fatal error: The "yield" expression can only be used inside a function
in wrong-line-number.php on line 6
That message is wrong. The yield on line 6 is correct. The fatal error
is actually on line 2.
We can see the AST has the wrong line number. Here's the output from
php-ast.
AST_STMT_LIST @ 1
0: AST_YIELD @ 6
0: AST_CLOSURE @ 6-6
flags: 0
name: {closure}
0: AST_PARAM_LIST @ 6
1: null
2: AST_STMT_LIST @ 6
0: AST_YIELD @ 6
0: null
1: null
3: null
1: null
The first AST_YIELD has the line number 6. After quick inspection, I see
the parser often delay the construction of AST node and capture the line
number after parsing 1~2 tokens, which may be a line or more below the
actual beginning of the AST node.
The consequences are incorrect line numbers from runtime errors.
Test script:
---------------
<?php
yield
// gap
function() { yield; };
Expected result:
----------------
Fatal error: The "yield" expression can only be used inside a function
in wrong-line-number.php on line 2
Actual result:
--------------
Fatal error: The "yield" expression can only be used inside a function
in wrong-line-number.php on line 6
--
Edit bug report at https://bugs.php.net/bug.php?id=70210&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70210&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70210&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70210&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70210&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70210&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70210&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70210&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70210&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70210&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70210&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70210&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70210&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70210&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70210&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70210&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70210&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70210&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70210&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70210&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70210&r=mysqlcfg
Thread (2 messages)
- duckki at synopsys dot com