Conversation
…keys
A call nested in an expression can be materialized into a pre-statement
temporary. Two constructs emitted those temporaries before work that PHP
performs first, so side effects (output, mutations, exceptions) happened in
the wrong order.
echo: `echo a, b, c` prints each operand before evaluating the next one.
parseEcho() emits one php::echo() per operand, but every operand's hoisted
lines were emitted before the first php::echo():
function f(string $s): string { echo "[f:$s]"; return $s; }
echo "A", var_export(f("1"), true), "\n";
// PHP: A[f:1]'1'
// compiled: [f:1]A'1'
echo "E-", var_export(boom(), true); // boom() throws
// PHP prints "E-" before the exception; the binary printed nothing.
The pre- and post-statement lines produced while parsing an operand are now
emitted around that operand's own php::echo().
Array literals: PHP evaluates a key before its value. parseArrayMixed()
parsed the value first, so a value that is a call was materialized before
an inline key expression:
[f("k1") => strtoupper(f("v1")), f("k2") => f("v2")]
// PHP: [k1][v1][k2][v2]
// compiled: [v1][k1][v2][k2]
A side-effecting key is now materialized before its value is parsed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #143
A call nested in an expression can be materialized into a pre-statement temporary. Two constructs emitted those temporaries before work PHP performs first.
The change
echo(CompilerBase::parseEcho()): it already emits onephp::echo()per operand, but every operand's hoisted lines landed before the firstphp::echo(). The pre- and post-statement lines produced while parsing an operand are now emitted around that operand's ownphp::echo(), so each operand is evaluated after the previous one has been printed — including when it throws.ArrayExpressionTrait::parseArrayMixed()): the value was parsed before the key, so a value that is a call was materialized ahead of an inline key expression. A side-effecting key (shouldMaterializeOrderedOperand()) is now materialized withparseOrderedOperand()before the value is parsed. Literal keys are untouched (they also never reach this path with side effects inparseArray()).Verified on a binary
PHP 8.4.26 ZTS with embed, GCC 11, Linux x64.
?:,sprintf) compared with PHPtests/compiler/basic/echo-operand-order.phpt[f:1]A'1')tests/compiler/array/array-key-evaluation-order.phpt[v1][k1][v2][k2])tests/compiler(full)Found with a differential fuzzer (same session as #137 and #140).