Skip to content
Closed
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
17 changes: 17 additions & 0 deletions phpunit/code/declared-scalar-property-read-final.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

final class DeclaredScalarFinalBox
{
public int $count = 0;
public float $ratio = 0.0;

public function scaled(): float
{
return $this->ratio * $this->count;
}
}

function declared_scalar_read_final(DeclaredScalarFinalBox $box): float
{
return $box->ratio + $box->count;
}
18 changes: 18 additions & 0 deletions phpunit/code/declared-scalar-property-read-hooked-subclass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

class DeclaredScalarHookedBase
{
public int $count = 1;
}

class DeclaredScalarHookedChild extends DeclaredScalarHookedBase
{
public int $count = 2 {
get => 42;
}
}

function declared_scalar_read_hooked(DeclaredScalarHookedBase $box): int
{
return $box->count;
}
15 changes: 15 additions & 0 deletions phpunit/code/declared-scalar-property-read-loop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

final class DeclaredScalarLoopBox
{
public int $count = 1;

public function sum(int $iterations): int
{
$total = 0;
for ($i = 0; $i < $iterations; $i++) {
$total += $this->count;
}
return $total;
}
}
14 changes: 14 additions & 0 deletions phpunit/code/declared-scalar-property-read-narrowed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

final class DeclaredScalarNarrowedBox
{
public int $count = 0;
}

function declared_scalar_read_narrowed(mixed $value): int
{
if ($value instanceof DeclaredScalarNarrowedBox) {
return $value->count;
}
return -1;
}
11 changes: 11 additions & 0 deletions phpunit/code/declared-scalar-property-read-nullable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

final class DeclaredScalarNullableBox
{
public ?int $count = null;
}

function declared_scalar_read_nullable(DeclaredScalarNullableBox $box): ?int
{
return $box->count;
}
11 changes: 11 additions & 0 deletions phpunit/code/declared-scalar-property-read-open.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class DeclaredScalarOpenBox
{
public int $count = 0;
}

function declared_scalar_read_open(DeclaredScalarOpenBox $box): int
{
return $box->count;
}
15 changes: 15 additions & 0 deletions phpunit/code/declared-scalar-property-read-readonly-arith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

final readonly class DeclaredScalarReadonlyArith
{
public function __construct(
public float $width,
public float $height,
) {
}
}

function declared_scalar_readonly_arith(DeclaredScalarReadonlyArith $box): float
{
return $box->width < $box->height ? $box->width / 2.0 : $box->height / 2.0;
}
13 changes: 13 additions & 0 deletions phpunit/code/declared-scalar-property-read-readonly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

final class DeclaredScalarReadonlyBox
{
public function __construct(public readonly int $count)
{
}
}

function declared_scalar_read_readonly(DeclaredScalarReadonlyBox $box): int
{
return $box->count;
}
16 changes: 16 additions & 0 deletions phpunit/code/declared-scalar-property-read-subclass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class DeclaredScalarSubclassBase
{
public int $count = 1;
}

class DeclaredScalarSubclassChild extends DeclaredScalarSubclassBase
{
public int $count = 2;
}

function declared_scalar_read_subclass(DeclaredScalarSubclassBase $box): int
{
return $box->count;
}
156 changes: 156 additions & 0 deletions phpunit/src/DeclaredScalarPropertyReadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* This file is part of TypePHP(AOT).
*
* @link https://www.swoole.com/aot/
* @contact service@swoole.com
*/

use TypePhp\CompilerTest;
use TypePhp\Exception\TestError;

/**
* @internal
* @coversNothing
*/
class DeclaredScalarPropertyReadTest extends BaseTest
{
private function compileCode(string $file): string
{
global $translator;

$compiler = CompilerTest::create(TYPEPHP_ROOT_PATH);
$translator = $compiler;
$testFile = __DIR__ . '/../code/' . $file;
$compiler->addFiles([$testFile]);
$compiler->prepareFile($testFile);
$compiler->convertFile($testFile);

return TYPEPHP_ROOT_PATH . '/build/phpunit/code/' . basename($file, '.php') . '.cc';
}

public function testFinalClassReceiverUsesTypedOffsetRead(): void
{
try {
$outputFile = $this->compileCode('declared-scalar-property-read-final.php');
} catch (TestError $e) {
$this->fail($e->getMessage());
}

$code = file_get_contents($outputFile);
$this->assertStringContainsString('attrInt(get_persistent_prop(', $code);
$this->assertStringContainsString('attrFloat(get_persistent_prop(', $code);
$this->assertStringContainsString('this_.attrInt(', $code);
$this->assertStringContainsString('this_.attrFloat(', $code);
$this->assertStringNotContainsString('attr(get_persistent_prop(', $code);
}

public function testNonFinalReceiverWithoutSubclassesUsesTypedOffsetRead(): void
{
try {
$outputFile = $this->compileCode('declared-scalar-property-read-open.php');
} catch (TestError $e) {
$this->fail($e->getMessage());
}

$code = file_get_contents($outputFile);
$this->assertStringContainsString('attrInt(get_persistent_prop(', $code);
$this->assertStringNotContainsString('attr(get_persistent_prop(', $code);
}

public function testSubclassRedeclaringThePropertyKeepsTypedOffsetRead(): void
{
try {
$outputFile = $this->compileCode('declared-scalar-property-read-subclass.php');
} catch (TestError $e) {
$this->fail($e->getMessage());
}

$code = file_get_contents($outputFile);
$this->assertStringContainsString('attrInt(get_persistent_prop(', $code);
$this->assertStringNotContainsString('attr(get_persistent_prop(', $code);
}

public function testSubclassAddingAPropertyHookFallsBackToVariantRead(): void
{
try {
$outputFile = $this->compileCode('declared-scalar-property-read-hooked-subclass.php');
} catch (TestError $e) {
$this->fail($e->getMessage());
}

$code = file_get_contents($outputFile);
$this->assertStringContainsString('attr(get_persistent_prop(', $code);
$this->assertStringNotContainsString('attrInt(get_persistent_prop(', $code);
}

public function testLoopReadHoistsBeforeTheTypedOffsetFallback(): void
{
try {
$outputFile = $this->compileCode('declared-scalar-property-read-loop.php');
} catch (TestError $e) {
$this->fail($e->getMessage());
}

$code = file_get_contents($outputFile);
$this->assertStringContainsString(
'php::Int &_object_prop_this___count = this_.attrInt(',
$code
);
}

public function testNullablePropertyKeepsVariantRead(): void
{
try {
$outputFile = $this->compileCode('declared-scalar-property-read-nullable.php');
} catch (TestError $e) {
$this->fail($e->getMessage());
}

$code = file_get_contents($outputFile);
$this->assertStringContainsString('attr(get_persistent_prop(', $code);
$this->assertStringNotContainsString('attrInt(get_persistent_prop(', $code);
}

public function testReadonlyPropertyKeepsCheckedRead(): void
{
try {
$outputFile = $this->compileCode('declared-scalar-property-read-readonly.php');
} catch (TestError $e) {
$this->fail($e->getMessage());
}

$code = file_get_contents($outputFile);
$this->assertStringContainsString('typephp_read_property_scoped(', $code);
$this->assertStringNotContainsString('attrInt(get_persistent_prop(', $code);
}

public function testReadonlyPropertyKeepsTheConversionOnArithmetic(): void
{
try {
$outputFile = $this->compileCode('declared-scalar-property-read-readonly-arith.php');
} catch (TestError $e) {
$this->fail($e->getMessage());
}

$code = file_get_contents($outputFile);
// The read is materialized as a Variant, so the division still has to
// be converted instead of being treated as an already native float.
$this->assertStringContainsString('typephp_read_property_scoped(', $code);
$this->assertStringContainsString('php::toFloat(((tmp_var_', $code);
$this->assertStringNotContainsString('attrFloat(get_persistent_prop(', $code);
}

public function testNarrowedMixedReceiverKeepsVariantRead(): void
{
try {
$outputFile = $this->compileCode('declared-scalar-property-read-narrowed.php');
} catch (TestError $e) {
$this->fail($e->getMessage());
}

$code = file_get_contents($outputFile);
$this->assertStringContainsString('attr(get_persistent_prop(', $code);
$this->assertStringNotContainsString('attrInt(get_persistent_prop(', $code);
}
}
5 changes: 4 additions & 1 deletion phpunit/src/MatchExpressionCodegenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ public function testMatchArmPropertyReadConvertsAtTypedBoundary(): void
{
$fnCode = $this->extractFunction('php_testmatcharmpropertyread');
self::assertStringContainsString('[&]() -> php::Int {', $fnCode);
self::assertStringContainsString('php::toInt(obj.attr(', $fnCode);
// The arm already reads the slot as an int, so the conversion happens
// at the lambda boundary instead of around the property fetch.
self::assertStringContainsString('obj.attrInt(get_persistent_prop(', $fnCode);
self::assertStringContainsString('php::toInt([&]() -> php::Int {', $fnCode);

$ternaryCode = $this->extractFunction('php_testmatcharmternarypropertyread');
self::assertStringContainsString('[&]() -> php::Int {', $ternaryCode);
Expand Down
24 changes: 13 additions & 11 deletions phpunit/src/NativePropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ public function testUnsetObjectDisablesPropertySlotsButKeepsNativeMethodCall():

$code = file_get_contents($outputFile);
$this->assertStringNotContainsString('_object_prop_object__value', $code);
$this->assertStringContainsString('object.attr(', $code);
// The receiver is unstable, so the slot is re-read through the typed
// accessor instead of being hoisted into a long-lived reference.
$this->assertStringContainsString('object.attrInt(', $code);
$this->assertStringContainsString('php_nativemethodunsetkeepsoptimization__read(object)', $code);
}

Expand Down Expand Up @@ -252,20 +254,20 @@ public function testNativePropertyWritesAndCompoundAssignsConvertLoweredVariantS
}

$code = file_get_contents($outputFile);
$this->assertStringContainsString('_object_prop_this___number += (php::toInt(', $code);
$this->assertStringContainsString('_object_prop_this___amount += (php::toFloat(', $code);
$this->assertStringContainsString('typephp_static_int_ref(_typephp_static_property_0().direct_ptr()) += (php::toInt(', $code);
$this->assertStringContainsString('typephp_static_float_ref(_typephp_static_property_1().direct_ptr()) += (php::toFloat(', $code);
$this->assertStringContainsString('_object_prop_this___number += (source.attrInt(', $code);
$this->assertStringContainsString('_object_prop_this___amount += (source.attrFloat(', $code);
$this->assertStringContainsString('typephp_static_int_ref(_typephp_static_property_0().direct_ptr()) += (source.attrInt(', $code);
$this->assertStringContainsString('typephp_static_float_ref(_typephp_static_property_1().direct_ptr()) += (source.attrFloat(', $code);
$this->assertStringContainsString('typephp_static_int_ref(_typephp_static_property_0().direct_ptr()) *= (1.5);', $code);
$this->assertStringContainsString('typephp_static_int_ref(_typephp_static_property_0().direct_ptr()) /= (0.5);', $code);
$this->assertStringContainsString('typephp_static_int_ref(_typephp_static_property_0().direct_ptr()) *= (php::toFloat(', $code);
$this->assertStringContainsString('typephp_static_int_ref(_typephp_static_property_0().direct_ptr()) /= (php::toFloat(', $code);
$this->assertStringContainsString('typephp_static_int_ref(_typephp_static_property_0().direct_ptr()) *= (source.attrFloat(', $code);
$this->assertStringContainsString('typephp_static_int_ref(_typephp_static_property_0().direct_ptr()) /= (source.attrFloat(', $code);
$this->assertStringNotContainsString('php::toInt(1.5)', $code);
$this->assertStringNotContainsString('php::toInt(0.5)', $code);
$this->assertMatchesRegularExpression('/_object_prop_this___number = php::toInt\(\(\(tmp_var_\d+\) \+ \(1L+\)\)\);/', $code);
$this->assertMatchesRegularExpression('/_object_prop_this___amount = php::toFloat\(\(\(tmp_var_\d+\) \+ \(0\.5\)\)\);/', $code);
$this->assertStringContainsString('_object_prop_this___number = php::toInt(-(source.attr(', $code);
$this->assertMatchesRegularExpression('/_object_prop_this___number = php::toInt\(\(php::toBool\(cond\)\) \? \(source\.attr\(.+?\) : \(0L+\)\);/', $code);
$this->assertMatchesRegularExpression('/_object_prop_this___number = \(\(tmp_var_\d+\) \+ \(1L+\)\);/', $code);
$this->assertMatchesRegularExpression('/_object_prop_this___amount = \(\(tmp_var_\d+\) \+ \(0\.5\)\);/', $code);
$this->assertStringContainsString('_object_prop_this___number = -(source.attrInt(', $code);
$this->assertMatchesRegularExpression('/_object_prop_this___number = \(php::toBool\(cond\)\) \? \(source\.attrInt\(.+?\)\) : \(0L+\);/', $code);
$this->assertMatchesRegularExpression('/_object_prop_this___number = php::toInt\(tmp_var_\d+\);/', $code);
}

Expand Down
4 changes: 3 additions & 1 deletion phpunit/src/ReadonlyTypedPropertyReadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public function testReadonlyScalarReadsRestoreDeclaredStorageType(): void
// initialized from it directly.
$this->assertStringContainsString('php::toInt(typephp_read_property_scoped(this_,', $code);
$this->assertStringContainsString('php::toFloat(typephp_read_property_scoped(this_,', $code);
$this->assertStringContainsString('switch (tmp_var_0)', $code);
// The read keeps its Variant storage, so the discriminant compares
// through PHP's loose equality instead of a native switch.
$this->assertStringContainsString('php::equals(tmp_var_0, 0LL)', $code);
// Readonly properties stay out of the hoisted scalar reference path.
$this->assertStringNotContainsString('_object_prop_this___status', $code);
$this->assertStringNotContainsString('_object_prop_this___ratio', $code);
Expand Down
Loading
Loading