Skip to content
Merged
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
21 changes: 21 additions & 0 deletions lib/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,26 @@ protected function assertReadonlyParamOnlyInConstructor(array $params, ?CfgFunc
}
}

/**
* php-src: Zend/zend_compile.c zend_compile_closure_binding() — $this is never a legal use() name (#32152).
*
* @param list<Operand\BoundVariable> $closureUseVars
*/
protected function assertNoThisInClosureUseVars(array $closureUseVars, Op $source): void
{
foreach ($closureUseVars as $useVar) {
if ('this' !== $this->boundVariableName($useVar)) {
continue;
}
$detail = 'Cannot use $this as lexical variable';
$sourceFile = $source->getFile();
if ('' === $sourceFile) {
$sourceFile = 'unknown';
}
$this->throwCompileError($detail, $sourceFile, $source->getLine());
}
}

/**
* @param list<Operand\BoundVariable> $closureUseVars
*/
Expand Down Expand Up @@ -14819,6 +14839,7 @@ protected function compileAnonymousFunctionExpr($expr, Block $block): array
$closureUseVars[] = $useVar;
}
}
$this->assertNoThisInClosureUseVars($closureUseVars, $expr);
}
try {
$funcBlock = $this->compileCfgBlock($func->cfg, $func->params, $func, $closureUseVars);
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@
<file>./test/compliance/TraitDiamondInsteadofRequiredNotAddedJITTest.php</file>
<file>./test/compliance/TraitInsteadofParentVMTest.php</file>
<file>./test/compliance/TraitInsteadofParentJITTest.php</file>
<file>./test/compliance/ClosureUseThis32152VMTest.php</file>
<file>./test/compliance/ClosureUseThis32152JITTest.php</file>
<file>./test/compliance/UninitTypedArrayDimAssignVMTest.php</file>
<file>./test/compliance/UninitTypedArrayDimAssignJITTest.php</file>
<file>./test/compliance/UninitTypedArrayDimRwVMTest.php</file>
Expand Down
36 changes: 36 additions & 0 deletions test/compliance/ClosureUseThis32152JITTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace PHPCompiler;

require_once __DIR__.'/../BaseTest.php';

/**
* JIT: closure use($this) is Zend compile fatal (#32152, zend_compile.c).
*
* Dedicated provider — path-slash data-set names break --filter on full JITTest.
*
* @group llvm
*/
final class ClosureUseThis32152JITTest extends BaseTest
{
protected static string $DIR = __DIR__;

public static function providePHPTests(): \Generator
{
yield 'closure_use_this.phpt' => self::parsePHPT(
__DIR__.'/cases/language/closure_use_this.phpt',
'closure_use_this.phpt'
);
yield 'closure_use_this_method.phpt' => self::parsePHPT(
__DIR__.'/cases/language/closure_use_this_method.phpt',
'closure_use_this_method.phpt'
);
}

public function setUp(): void
{
$this->BIN = realpath(__DIR__.'/../../bin/jit.php');
}
}
34 changes: 34 additions & 0 deletions test/compliance/ClosureUseThis32152VMTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace PHPCompiler;

require_once __DIR__.'/../BaseTest.php';

/**
* VM: closure use($this) is Zend compile fatal (#32152, zend_compile.c).
*
* Dedicated provider — path-slash data-set names break --filter on full VMTest.
*/
final class ClosureUseThis32152VMTest extends BaseTest
{
protected static string $DIR = __DIR__;

public static function providePHPTests(): \Generator
{
yield 'closure_use_this.phpt' => self::parsePHPT(
__DIR__.'/cases/language/closure_use_this.phpt',
'closure_use_this.phpt'
);
yield 'closure_use_this_method.phpt' => self::parsePHPT(
__DIR__.'/cases/language/closure_use_this_method.phpt',
'closure_use_this_method.phpt'
);
}

public function setUp(): void
{
$this->BIN = realpath(__DIR__.'/../../bin/vm.php');
}
}
10 changes: 10 additions & 0 deletions test/compliance/cases/language/closure_use_this.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Language: closure use($this) is compile-time fatal (#32152, Zend/zend_compile.c)
--FILE--
<?php
$f = function () use ($this) { return 1; };
echo "accepted\n";
--EXPECT_EXIT--
255
--EXPECTF--
%APHP Fatal error: Cannot use $this as lexical variable in %s on line %d
16 changes: 16 additions & 0 deletions test/compliance/cases/language/closure_use_this_method.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Language: method-scope closure use($this) is compile-time fatal (#32152, Zend/zend_compile.c)
--FILE--
<?php
class C {
public function m() {
$f = function () use ($this) { return 1; };
echo "accepted\n";
return $f;
}
}
(new C())->m();
--EXPECT_EXIT--
255
--EXPECTF--
%APHP Fatal error: Cannot use $this as lexical variable in %s on line %d
8 changes: 8 additions & 0 deletions test/repro/maintainer_gap_use_this.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* Maintainer gap #32152: function () use ($this) is accepted.
* Zend: compile fatal "Cannot use $this as lexical variable" (rc=255).
* php-src: Zend/zend_compile.c zend_compile_closure_binding().
*/
$f = function () use ($this) { return 1; };
echo "accepted\n";
60 changes: 60 additions & 0 deletions test/unit/ClosureUseThisCompileFatalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\Test\Unit;

use PHPCompiler\Runtime;
use PHPUnit\Framework\TestCase;

/** @covers issue #32152 — closure use($this) is a Zend compile-time fatal */
final class ClosureUseThisCompileFatalTest extends TestCase
{
/**
* @dataProvider illegalUseThisProvider
*/
public function testUseThisFailsAtCompileTime(string $code): void
{
$runtime = new Runtime();
$this->expectException(\CompileError::class);
$this->expectExceptionMessage('Cannot use $this as lexical variable');
$runtime->parseAndCompile($code, 'closure_use_this.php');
}

/** @return iterable<string, array{string}> */
public static function illegalUseThisProvider(): iterable
{
yield 'file scope' => ['<?php $f = function () use ($this) { return 1; }; echo "accepted\n";'];
yield 'by-ref' => ['<?php $f = function () use (&$this) { return 1; }; echo "accepted\n";'];
yield 'method scope' => [
'<?php class C { public function m() { $f = function () use ($this) { return 1; }; echo "accepted\n"; } } (new C())->m();',
];
yield 'mixed uses' => ['<?php $a = 1; $f = function () use ($a, $this) { return $a; }; echo "accepted\n";'];
}

public function testLegalUseStillCompiles(): void
{
$runtime = new Runtime();
$block = $runtime->parseAndCompile(
'<?php $a = 2; $f = function () use ($a) { return $a; }; echo $f();',
'closure_use_ok.php'
);
$this->assertNotNull($block);
ob_start();
$runtime->run($block);
$this->assertSame('2', ob_get_clean());
}

public function testMethodClosureAutoBindsThisWithoutUse(): void
{
$runtime = new Runtime();
$block = $runtime->parseAndCompile(
'<?php class C { public int $n = 7; public function m() { $f = function () { return $this->n; }; echo $f(); } } (new C())->m();',
'closure_this_ok.php'
);
$this->assertNotNull($block);
ob_start();
$runtime->run($block);
$this->assertSame('7', ob_get_clean());
}
}
Loading