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
22 changes: 13 additions & 9 deletions src/Generator/CallArgumentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ protected function parseNativeCallArgs(
// iteration instead of accumulating arguments.
$variadicVar = $this->addTmpVar(Type::ARRAY);
$this->context->beforeStmtLines[] = $variadicVar . '.unset();';
if ($argInfo->byRef) {
// The aggregation array owns the second reference to
// every caller slot, so queue its release before the
// per-argument RefWrap::commit() lines are appended:
// committing while the second reference is alive
// trips the typed-reference escape check. The guard
// also releases during C++ exception unwinding into
// a PHP catch block.
$cleanupGuard = $this->genTmpVarName();
$this->context->beforeStmtLines[] = 'php::ArrayCleanupGuard ' . $cleanupGuard
. '{' . $variadicVar . '};';
$this->context->afterStmtLines[] = $cleanupGuard . '.cleanup();';
}
}
if ($arg->unpack) {
$method = $argInfo->byRef ? 'mergeReferences' : 'merge';
Expand All @@ -338,15 +351,6 @@ protected function parseNativeCallArgs(
}
if ($variadicVar !== null) {
$resolvedArgs[$variadicArgIndex] = $variadicVar;
if ($functionDef->argInfoList[$variadicArgIndex]->byRef) {
// The aggregation array owns the second reference to every
// caller slot. Release it after the full PHP statement and
// also during C++ exception unwinding into a PHP catch block.
$cleanupGuard = $this->genTmpVarName();
$this->context->beforeStmtLines[] = 'php::ArrayCleanupGuard ' . $cleanupGuard
. '{' . $variadicVar . '};';
$this->context->afterStmtLines[] = $cleanupGuard . '.cleanup();';
}
}
ksort($resolvedArgs);
return implode(', ', $resolvedArgs);
Expand Down
45 changes: 45 additions & 0 deletions tests/compiler/variadic/by-reference-variadic-local.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
by-ref variadic writes back to local variable arguments
--FILE--
<?php
class Box {
public int $p = 5;
public function bump(int &...$vals): void {
foreach ($vals as &$v) { $v += 10; }
}
}
function bump(int &...$vals): void {
foreach ($vals as &$v) { $v += 10; }
}
function bumpu(&...$vals): void {
foreach ($vals as &$v) { $v = $v + 10; }
}
function main(): void
{
$a = 1;
bump($a);
var_dump($a);
for ($i = 0; $i < 3; $i++) {
bump($a);
}
var_dump($a);
$c = 2;
bump(vals: $c);
var_dump($c);
bumpu($a);
var_dump($a);
$b = new Box();
$b->bump($b->p);
var_dump($b->p);
$arr = [7];
bump($arr[0]);
var_dump($arr[0]);
}
?>
--EXPECT--
int(11)
int(41)
int(12)
int(51)
int(15)
int(17)
Loading