Skip to content

Commit 41d91fe

Browse files
committed
fix(compiler): preserve scalar storage for checked property operands
Readonly property reads already convert their checked Zend result to a native scalar. Ordered operand lowering boxed that scalar again while its AST still advertised native storage, so float division in a ternary arm could fail C++ compilation. Choose the temporary storage from the emitted property result type. Keep checked reads, initialization diagnostics, native switch lowering and varint_types integer storage intact. Cover float, int and bool arithmetic, receiver ordering, null and uninitialized receivers, and boxed integers. Isolate the readonly issue identified in PR #188 (90dde0e) without adopting its broad direct-slot property read optimization.
1 parent c5599c5 commit 41d91fe

5 files changed

Lines changed: 152 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use varint_types;
4+
5+
final class ReadonlyVarIntArithmeticState
6+
{
7+
public function __construct(
8+
public readonly int $count,
9+
public readonly float $width,
10+
) {
11+
}
12+
}
13+
14+
function readonlyVarIntIncrement(ReadonlyVarIntArithmeticState $state): int
15+
{
16+
return $state->count + 1;
17+
}
18+
19+
function readonlyVarIntWidth(ReadonlyVarIntArithmeticState $state): float
20+
{
21+
return $state->width + 0.5;
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
final class ReadonlyArithmeticState
4+
{
5+
public function __construct(
6+
public readonly float $width,
7+
public readonly float $height,
8+
public readonly int $count,
9+
public readonly bool $enabled,
10+
) {
11+
}
12+
13+
public function half(): float
14+
{
15+
return $this->width / 2.0;
16+
}
17+
}
18+
19+
function readonlyPropertyHalf(ReadonlyArithmeticState $state): float
20+
{
21+
return $state->width < $state->height ? $state->width / 2.0 : $state->height / 2.0;
22+
}
23+
24+
function readonlyPropertyIncrement(ReadonlyArithmeticState $state): int
25+
{
26+
return $state->count + 1;
27+
}
28+
29+
function readonlyPropertyBoolComparison(ReadonlyArithmeticState $state): bool
30+
{
31+
return $state->enabled === true;
32+
}

‎phpunit/src/ReadonlyTypedPropertyReadTest.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,27 @@ public function testReadonlyReadsRestoreStorageForOtherReceivers(): void
6363
$this->assertStringNotContainsString('_object_prop_', $code);
6464
$this->assertStringNotContainsString('AttrMode::Update', $code);
6565
}
66+
67+
public function testReadonlyScalarArithmeticKeepsConvertedOperandsUnboxed(): void
68+
{
69+
$code = file_get_contents($this->compileFixture('readonly-typed-property-arithmetic.php'));
70+
71+
$this->assertStringContainsString('typephp_read_property_scoped(', $code);
72+
$this->assertStringContainsString('php::Float tmp_var_', $code);
73+
$this->assertStringContainsString('php::Int tmp_var_', $code);
74+
$this->assertStringContainsString('php::Bool tmp_var_', $code);
75+
$this->assertStringNotContainsString('php::Var tmp_var_', $code);
76+
$this->assertStringNotContainsString('.attrInt(', $code);
77+
$this->assertStringNotContainsString('.attrFloat(', $code);
78+
}
79+
80+
public function testReadonlyArithmeticPreservesVarIntStorage(): void
81+
{
82+
$code = file_get_contents($this->compileFixture('readonly-typed-property-arithmetic-varint.php'));
83+
84+
$this->assertStringContainsString('php::Var tmp_var_', $code);
85+
$this->assertStringContainsString('php::Float tmp_var_', $code);
86+
$this->assertStringNotContainsString('php::Int tmp_var_', $code);
87+
$this->assertStringContainsString('typephp_read_property_scoped(', $code);
88+
}
6689
}

‎src/Parser/BinaryOpTrait.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,13 @@ protected function getOrderedOperandTmpType(NodeAbstract $expr, string $value):
860860
}
861861

862862
if ($expr instanceof Expr\PropertyFetch) {
863+
// A checked property read can already return a converted scalar
864+
// even though its source is a dynamic Zend property. Snapshot the
865+
// emitted value in native storage so its lowered type stays valid.
866+
$lowered = $this->getLoweredExpressionType($expr);
867+
if ($lowered !== null && $this->usesNativeScalarStorage($lowered)) {
868+
return $lowered;
869+
}
863870
$nativePropertyVar = $this->getNativePropertyVar($expr);
864871
if ($nativePropertyVar !== null && $nativePropertyVar === $value) {
865872
$info = $this->getObjectPropInfoByVar($nativePropertyVar);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
Readonly scalar property arithmetic preserves native operands, checked reads and evaluation order
3+
--FILE--
4+
<?php
5+
final class ReadonlyArithmeticBox
6+
{
7+
public function __construct(
8+
public readonly float $width,
9+
public readonly float $height,
10+
public readonly int $count,
11+
public readonly bool $enabled,
12+
) {}
13+
14+
public function ratio(): float { return $this->width / $this->height; }
15+
}
16+
17+
final class UninitializedReadonlyArithmeticBox
18+
{
19+
public readonly float $width;
20+
}
21+
22+
function halfReadonlyBox(ReadonlyArithmeticBox $box): float
23+
{
24+
return $box->width < $box->height ? $box->width / 2.0 : $box->height / 2.0;
25+
}
26+
27+
function nextReadonlyCount(ReadonlyArithmeticBox $box): int { return $box->count + 1; }
28+
function readonlyEnabled(ReadonlyArithmeticBox $box): bool { return $box->enabled === true; }
29+
function readonlyReceiver(ReadonlyArithmeticBox $box): ReadonlyArithmeticBox { echo "receiver\n"; return $box; }
30+
function readonlyDivisor(): float { echo "divisor\n"; return 2.0; }
31+
function orderedReadonlyHalf(ReadonlyArithmeticBox $box): float
32+
{
33+
return readonlyReceiver($box)->width / readonlyDivisor();
34+
}
35+
function uninitializedReadonlyHalf(UninitializedReadonlyArithmeticBox $box): float { return $box->width / 2.0; }
36+
function nullableReadonlyHalf(?ReadonlyArithmeticBox $box): float { return $box->width / 2.0; }
37+
38+
function main(): void
39+
{
40+
$box = new ReadonlyArithmeticBox(6.0, 12.0, 4, true);
41+
var_dump(halfReadonlyBox($box), $box->ratio(), nextReadonlyCount($box), readonlyEnabled($box));
42+
var_dump(halfReadonlyBox(new ReadonlyArithmeticBox(12.0, 6.0, 0, false)));
43+
var_dump(readonlyEnabled(new ReadonlyArithmeticBox(1.0, 2.0, 0, false)));
44+
var_dump(orderedReadonlyHalf($box));
45+
try {
46+
uninitializedReadonlyHalf(new UninitializedReadonlyArithmeticBox());
47+
} catch (Error $error) {
48+
echo "uninitialized: ", $error->getMessage(), "\n";
49+
}
50+
try {
51+
nullableReadonlyHalf(null);
52+
} catch (Error $error) {
53+
echo "null receiver\n";
54+
}
55+
}
56+
?>
57+
--EXPECT--
58+
float(3)
59+
float(0.5)
60+
int(5)
61+
bool(true)
62+
float(3)
63+
bool(false)
64+
receiver
65+
divisor
66+
float(3)
67+
uninitialized: Typed property UninitializedReadonlyArithmeticBox::$width must not be accessed before initialization
68+
null receiver

0 commit comments

Comments
 (0)