Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions phpunit/code/native-condition-temporaries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

function nativeCondition(array $values): int
{
if ($values[0] === 1) {
return 1;
}
if ($values[0] === 2 || $values[1] === 3) {
return 2;
}
return 0;
}

function dynamicConditionValue(mixed $value): mixed
{
return $value;
}

function dynamicCondition(array $values): bool
{
if (dynamicConditionValue($values[0])) {
return true;
}
return false;
}
28 changes: 28 additions & 0 deletions phpunit/src/NativeConditionTemporaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use TypePhp\CompilerTest;

final class NativeConditionTemporaryTest extends BaseTest
{
public function testBooleanConditionsWithOperandCleanupRemainUnboxed(): void
{
global $translator;
$compiler = CompilerTest::create(TYPEPHP_ROOT_PATH);
$translator = $compiler;
$source = TYPEPHP_ROOT_PATH . '/phpunit/code/native-condition-temporaries.php';
$compiler->addFiles([$source]);
$compiler->prepareFile($source);
$code = file_get_contents($compiler->convertFile($source));

preg_match_all('/(tmp_var_\d+) = php::toBool\(php::same\(/', $code, $matches);
self::assertCount(2, $matches[1]);
foreach ($matches[1] as $temporary) {
self::assertStringContainsString('php::Bool ' . $temporary . ' = 0;', $code);
}

$dynamicBody = explode('php::Bool php_dynamiccondition(', $code, 2)[1];
$dynamicBody = explode("\n}", $dynamicBody, 2)[0];
self::assertStringContainsString('php::Var tmp_var_1;', $dynamicBody);
self::assertStringContainsString('tmp_var_1 = php_dynamicconditionvalue(', $dynamicBody);
}
}
11 changes: 10 additions & 1 deletion src/CompilerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1984,7 +1984,13 @@ protected function genConditionWithCapturedStmts(NodeAbstract $cond, string $ope
$code = '';
$code .= $this->formatCapturedStmtLines($beforeStmts);
if ($afterStmts) {
$tmpVar = $this->addTmpVar(Type::VAR);
// Boolean results own no zval resources and can survive operand
// cleanup in native storage without changing evaluation order.
$type = $this->detectTypeOfExpr($cond) === Type::BOOL ? Type::BOOL : Type::VAR;
$tmpVar = $this->addTmpVar($type);
if ($type === Type::BOOL) {
$condExpr = $this->convertBoolExpr($condExpr);
}
$code .= $this->getIndent() . $tmpVar . ' = ' . $condExpr . ';' . PHP_EOL;
$code .= $this->formatCapturedStmtLines($afterStmts);
$condExpr = $tmpVar;
Expand Down Expand Up @@ -4501,6 +4507,9 @@ protected function genStaticVarInitLambda(Node\Stmt\StaticVar $var, string $varN

$this->context = new FunctionContext();
$this->context->arguments = $oriCtx->localVars;
// Outer locals are captured arguments. New initializer temporaries
// must not reuse their names and inherit an incompatible scalar type.
$this->context->tmpVarIndex = $oriCtx->tmpVarIndex;

$code = '([&](){' . PHP_EOL;
$body = $this->getIndent() . $varName . ' = ' . $this->parseExpr($var->default) . ';';
Expand Down
6 changes: 5 additions & 1 deletion src/Parser/BinaryOpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,11 @@ protected function parseShortCircuitLogicalOp(NodeAbstract $left, NodeAbstract $
$this->indentLevel++;
$code .= $this->formatCapturedStmtLines($rightBeforeStmts);
if ($rightAfterStmts) {
$rightTmpVar = $this->addTmpVar(Type::VAR);
$type = $this->detectTypeOfExpr($right) === Type::BOOL ? Type::BOOL : Type::VAR;
$rightTmpVar = $this->addTmpVar($type);
if ($type === Type::BOOL) {
$rightExpr = $this->convertBoolExpr($rightExpr);
}
$code .= $this->getIndent() . $rightTmpVar . ' = ' . $rightExpr . ';' . PHP_EOL;
$code .= $this->formatCapturedStmtLines($rightAfterStmts);
$rightExpr = $rightTmpVar;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Static initializer temporaries do not reuse outer native condition snapshots
--FILE--
<?php
function conditionBeforeStatic(array $args): void {
if ($args === []) { return; }
static $table = [[[7, 'value']]];
echo $table[0][0][1], "\n";
}
function main(): void { conditionBeforeStatic([1]); conditionBeforeStatic([2]); }
?>
--EXPECT--
value
value
Loading