Bug #70210 [Opn]: Parser: some ASTs may get wrong line numbers
| From: | nikic@php.net | Date: | Sat, 08 Aug 2015 06:32:12 +0000 |
| Subject: | Bug #70210 [Opn]: Parser: some ASTs may get wrong line numbers | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-195017@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70210&edit=1
ID: 70210
Updated by: nikic@php.net
Reported by: duckki at synopsys dot com
Summary: Parser: some ASTs may get wrong line numbers
Status: Open
Type: Bug
Package: *Compile Issues
PHP Version: 7.0.0beta3
Block user comment: N
Private report: N
New Comment:
Related to: https://bugs.php.net/bug.php?id=70024
This would be fixed by using bison location tracking.
Previous Comments:
------------------------------------------------------------------------
[2015-08-07 23:31:56] duckki at synopsys dot com
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 this bug report at https://bugs.php.net/bug.php?id=70210&edit=1