doFoldKnownClass folds class_exists() whenever the name is a literal the
symbol table knows. That table also holds traits, so a trait name folded
to true while PHP answers false:
trait Helper {}
class_exists('Helper'); // folded to true
$name = 'Helper';
class_exists($name); // reaches php::fn::class_exists, answers false
The same program therefore gives two different answers for the same
trait, decided only by whether the argument is a literal.
The runtime side is already right, and deliberately so: traits are
compile-time AST templates in TypePHP, which is why
tests/compiler/stdlib/class_exists.phpt expects trait_exists() to be
false. Only the constant fold disagreed - with PHP and with the
compiler's own runtime.
A trait name now folds to false. Classes and enums keep folding to true,
which matches PHP: an enum is a class, a trait is not.
class_exists.phpt gains the literal and non-literal trait cases, and
ClassExistsTraitFoldTest pins the fold decision in the generated C++.
master
parent
b493ac79c5
commit
c4ce700868
5 changed files with 101 additions and 0 deletions
@ -0,0 +1,22 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* This file is part of TypePHP(AOT). |
||||||
|
* |
||||||
|
* @link https://www.swoole.com/aot/ |
||||||
|
* @contact service@swoole.com |
||||||
|
*/ |
||||||
|
|
||||||
|
class Real |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
enum Suit |
||||||
|
{ |
||||||
|
case Hearts; |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
var_dump(class_exists('Real')); |
||||||
|
var_dump(class_exists('Suit')); |
||||||
|
} |
||||||
@ -0,0 +1,16 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* This file is part of TypePHP(AOT). |
||||||
|
* |
||||||
|
* @link https://www.swoole.com/aot/ |
||||||
|
* @contact service@swoole.com |
||||||
|
*/ |
||||||
|
|
||||||
|
trait Helper |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
var_dump(class_exists('Helper')); |
||||||
|
} |
||||||
@ -0,0 +1,50 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* This file is part of TypePHP(AOT). |
||||||
|
* |
||||||
|
* @link https://www.swoole.com/aot/ |
||||||
|
* @contact service@swoole.com |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace TypePhp\Tests; |
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase; |
||||||
|
use TypePhp\CompilerTest; |
||||||
|
|
||||||
|
/** |
||||||
|
* @internal |
||||||
|
* @coversNothing |
||||||
|
*/ |
||||||
|
class ClassExistsTraitFoldTest extends TestCase |
||||||
|
{ |
||||||
|
public function testTraitNameFoldsToFalse(): void |
||||||
|
{ |
||||||
|
$cpp = $this->compileToCpp('class-exists-trait.php'); |
||||||
|
|
||||||
|
// The trait is known at compile time, so the call is still folded - |
||||||
|
// just to the answer PHP gives, which is false for a trait. |
||||||
|
self::assertStringNotContainsString('php::fn::class_exists(', $cpp); |
||||||
|
self::assertStringContainsString('= false;', $cpp); |
||||||
|
} |
||||||
|
|
||||||
|
public function testClassAndEnumNamesStillFoldToTrue(): void |
||||||
|
{ |
||||||
|
$cpp = $this->compileToCpp('class-exists-class-and-enum.php'); |
||||||
|
|
||||||
|
self::assertStringNotContainsString('php::fn::class_exists(', $cpp); |
||||||
|
self::assertStringNotContainsString('= false;', $cpp); |
||||||
|
} |
||||||
|
|
||||||
|
private function compileToCpp(string $file): string |
||||||
|
{ |
||||||
|
global $translator; |
||||||
|
|
||||||
|
$compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); |
||||||
|
$translator = $compiler; |
||||||
|
$source = TYPEPHP_ROOT_PATH . '/phpunit/code/' . $file; |
||||||
|
$compiler->addFiles([$source]); |
||||||
|
$compiler->prepareFile($source); |
||||||
|
|
||||||
|
return file_get_contents($compiler->convertFile($source)); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue