You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PHP 8.5: Recursive tokenization during token_get_all result on error #19507
error handler called: Non-canonical cast (double) is deprecated, use the (float) cast instead
T_OPEN_TAG "<?php "
T_DOUBLE_CAST "(double)"
T_WHITESPACE " "
T_VARIABLE "$x"
;
Not sure about the error handler call. Is it intended that the error handler is called for deprecations inside the code given to token_get_all?
But at least I do not expect to get the tokens of b.php inside the tokens of the code given to token_get_all.
PHP Version
PHP 8.5.0-dev (cli) (built: Aug 16 2025 23:33:23) (NTS)
Copyright (c) The PHP Group
Built by Shivam Mathur
Zend Engine v4.5.0-dev, Copyright (c) Zend Technologies
with Zend OPcache v8.5.0-dev, Copyright (c), by Zend Technologies
IIRC tokenization was error free for a long time. if it now emits, the diagnostic messages should probably be collected and optionally be available, e.g. an out parameter on token_get_all().
That the previous(?) token is returned looks worry-some to me, too, collecting instead of immediate emission may solve that, too.
if it now emits, the diagnostic messages should probably be collected and optionally be available, e.g. an out parameter on token_get_all().
I do not think this case should trigger any error in first place. if code is valid but deprecated, tokenizer should work as-is (same output as under 8.4)
if it now emits, the diagnostic messages should probably be collected and optionally be available, e.g. an out parameter on token_get_all().
I do not think this case should trigger any error in first place. if code is valid but deprecated, tokenizer should work as-is (same output as under 8.4)
Yes, triggering an error is a side-effect of emitting the diagnostic message, I'd like to keep it free from emitting those, that is what I meant by "error free" (not the type but the event itself), also as @TimWolla has pointed out we're not there yet:
The only other non-fatal in the scanner is a E_COMPILE_WARNING. See also: #18510.
A possible fix would be to delay + record these errors, and emit them after tokenization, with zend_begin_record_errors() / zend_emit_recorded_errors(). Of course we need to handle cases where errors are already being recorded.
Yeah this happens because the "event" callback is a global for the scanner (LANG_SCNG(on_event)). So when a new file gets compiled during tokenization you also receive the events from that compilation pipeline.
So we could also save+restore the old value of that global prior to invoking the compiler.
I would prefer a solution that's solely changing ext-tokenizer though.
Oh it's even better in debug mode where it breaks an engine assumption: php: /run/media/niels/MoreData/php-src/Zend/zend_compile.c:2038: zendlex: Assertion `!(executor_globals.exception) || ret == T_ERROR' failed.
Doesn't look fun, because that mean that an error handler that throws is also problematic for example.
Simplest solution is to undeprecate these things ;)
Anyway, I decided not to care about this issue.
changed the title [-]PHP 8.5: Recursive `token_get_all` result on error[/-][+]PHP 8.5: Recursive tokenization during `token_get_all` result on error[/+]on Aug 20, 2025
Turns out we do save/restore the lexer state (including on_event) everywhere we use lex_scan() / zendlex(), but on_event is not reset, so nested calls inherit the parent on_event.
Regarding exceptions: Returning T_ERROR if EG(exception) is set after calling zend_error() would safely allow exceptions to be thrown from error handlers:
@@ -1636,6 +1646,9 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("integer"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (integer) is deprecated, use the (int) cast instead");
+ if (PARSER_MODE() && EG(exception)) {+ RETURN_TOKEN(T_ERROR);+ }
}
RETURN_TOKEN(T_INT_CAST);
}
@@ -1647,6 +1660,9 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("double"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (double) is deprecated, use the (float) cast instead");
+ if (PARSER_MODE() && EG(exception)) {+ RETURN_TOKEN(T_ERROR);+ }
}
RETURN_TOKEN(T_DOUBLE_CAST);
}
@@ -1666,6 +1682,9 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("binary"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (binary) is deprecated, use the (string) cast instead");
+ if (PARSER_MODE() && EG(exception)) {+ RETURN_TOKEN(T_ERROR);+ }
}
RETURN_TOKEN(T_STRING_CAST);
}
@@ -1685,6 +1704,9 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("boolean"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (boolean) is deprecated, use the (bool) cast instead");
+ if (PARSER_MODE() && EG(exception)) {+ RETURN_TOKEN(T_ERROR);+ }
}
RETURN_TOKEN(T_BOOL_CAST);
}
Activity
devnexen commentedon Aug 17, 2025
verified on master, not happening on 8.4.x
devnexen commentedon Aug 17, 2025
@nielsdos do you think it is more a change in the engine ? Do not think it comes from the tokenizer extension.
mvorisek commentedon Aug 17, 2025
simpler reproducer: https://3v4l.org/AO49k/rfc#vgit.master
DanielEScherzer commentedon Aug 17, 2025
Probably caused by the deprecations coming from within
zend_language_scanner.lrather than during compilation, e.g.php-src/Zend/zend_language_scanner.l
Lines 1636 to 1641 in c98b173
CC @Girgias
hakre commentedon Aug 18, 2025
IIRC tokenization was error free for a long time. if it now emits, the diagnostic messages should probably be collected and optionally be available, e.g. an out parameter on token_get_all().
That the previous(?) token is returned looks worry-some to me, too, collecting instead of immediate emission may solve that, too.
keradus commentedon Aug 18, 2025
I do not think this case should trigger any error in first place. if code is valid but deprecated, tokenizer should work as-is (same output as under 8.4)
TimWolla commentedon Aug 18, 2025
The only other non-fatal in the scanner is a
E_COMPILE_WARNING. See also: #18510.hakre commentedon Aug 18, 2025
Yes, triggering an error is a side-effect of emitting the diagnostic message, I'd like to keep it free from emitting those, that is what I meant by "error free" (not the type but the event itself), also as @TimWolla has pointed out we're not there yet:
So now I have to look how close we are... .
arnaud-lb commentedon Aug 18, 2025
A possible fix would be to delay + record these errors, and emit them after tokenization, with
zend_begin_record_errors()/zend_emit_recorded_errors(). Of course we need to handle cases where errors are already being recorded.ndossche commentedon Aug 19, 2025
Yeah this happens because the "event" callback is a global for the scanner (
LANG_SCNG(on_event)). So when a new file gets compiled during tokenization you also receive the events from that compilation pipeline.So we could also save+restore the old value of that global prior to invoking the compiler.
I would prefer a solution that's solely changing ext-tokenizer though.
hakre commentedon Aug 20, 2025
@nielsdos I have seen when zend_error_cb calls the user error handler callback, something is done there as well with some globals(?) and related to execution/compiling context. couldn't it be, that it falls short there as that mechanism is probably older than the tokenizer extension?
/* User error handler may include() additional PHP files.
* If an error was generated during compilation PHP will compile
* such scripts recursively, but some CG() variables may be
* inconsistent. */
ndossche commentedon Aug 20, 2025
Backing up and restoring the scanner event handler makes sense, I'll try it
[-]PHP 8.5: Strange `token_get_all` result[/-][+]PHP 8.5: Recursive `token_get_all` result on error[/+]ndossche commentedon Aug 20, 2025
Oh it's even better in debug mode where it breaks an engine assumption:
php: /run/media/niels/MoreData/php-src/Zend/zend_compile.c:2038: zendlex: Assertion `!(executor_globals.exception) || ret == T_ERROR' failed.Doesn't look fun, because that mean that an error handler that throws is also problematic for example.
Simplest solution is to undeprecate these things ;)
Anyway, I decided not to care about this issue.
[-]PHP 8.5: Recursive `token_get_all` result on error[/-][+]PHP 8.5: Recursive tokenization during `token_get_all` result on error[/+]arnaud-lb commentedon Aug 21, 2025
Turns out we do save/restore the lexer state (including
on_event) everywhere we uselex_scan()/zendlex(), buton_eventis not reset, so nested calls inherit the parenton_event.Resetting it fixes the issue:
Regarding exceptions: Returning
T_ERRORifEG(exception)is set after callingzend_error()would safely allow exceptions to be thrown from error handlers:arnaud-lb commentedon Aug 22, 2025
Thank you @gharlan!
Fix corrupted result after recursive tokenization during token_get_all()