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
35 changes: 35 additions & 0 deletions phpunit/code/class-constant-expression-inline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

class ClassConstantExpressionInline
{
public const CAP_A = 1;
public const CAP_B = 2;

public const ALL_MASK = self::CAP_A | self::CAP_B;

public function readLeaf(): int
{
return self::CAP_A;
}

public function readMask(): int
{
return self::ALL_MASK;
}
}

interface ClassConstantExpressionInlineFlags
{
public const FLAG_A = 1;
public const FLAG_B = 2;

public const COMBINED = self::FLAG_A | self::FLAG_B;
}

class ClassConstantExpressionInlineFlagUser implements ClassConstantExpressionInlineFlags
{
public function readCombined(): int
{
return self::COMBINED;
}
}
56 changes: 56 additions & 0 deletions phpunit/src/ClassConstantExpressionInlineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use TypePhp\CompilerTest;

final class ClassConstantExpressionInlineTest extends \BaseTest
{
public function testLeafConstantIsInlinedButExpressionConstantUsesZendLookup(): void
{
$code = $this->compileFixture();

$leaf = $this->methodBody($code, 'php_classconstantexpressioninline__readleaf');
self::assertDoesNotMatchRegularExpression('/php::(constant|classConstant)\(/', $leaf);
self::assertMatchesRegularExpression('/= \(1L+\);/', $leaf);

$mask = $this->methodBody($code, 'php_classconstantexpressioninline__readmask');
self::assertStringContainsString('php::constant(', $mask);
self::assertDoesNotMatchRegularExpression('/php::Var\(tmp_var_\d+\)/', $mask);
}

public function testInterfaceConstantFetchOnImplementingClassUsesZendLookup(): void
{
$code = $this->compileFixture();

$combined = $this->methodBody(
$code,
'php_classconstantexpressioninlineflaguser__readcombined'
);
self::assertStringContainsString('php::constant(', $combined);
self::assertDoesNotMatchRegularExpression('/php::Var\(tmp_var_\d+\)/', $combined);
}

private function compileFixture(): string
{
global $translator;

$compiler = CompilerTest::create(TYPEPHP_ROOT_PATH);
$translator = $compiler;
$source = TYPEPHP_ROOT_PATH . '/phpunit/code/class-constant-expression-inline.php';
$compiler->addFiles([$source]);
$compiler->prepareFile($source);
$generated = $compiler->convertFile($source);
$code = file_get_contents($generated);

self::assertIsString($code);
return $code;
}

private function methodBody(string $code, string $function): string
{
$pattern = '/' . preg_quote($function, '/') . '\([^)]*\) \{[\s\S]*?\n\}/';
preg_match($pattern, $code, $matches);

self::assertSame(1, count($matches), "Missing body for {$function}");
return $matches[0];
}
}
18 changes: 18 additions & 0 deletions src/CompilerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3375,6 +3375,13 @@ protected function findNativeClassConst(
if (!$interfaceConstDef->codegenFinalized) {
return false;
}
// Same materialize-temporary guard as the class-constant
// branch below: inlining a tmp_var_N initializer would
// reference locals that do not exist in the fetching function
// (C2065 / C2593).
if ($interfaceConstDef->arrayExpr !== '' || str_contains($interfaceConstDef->value, 'tmp_var_')) {
return false;
}
$expr->setAttribute('nativeConst', $interfaceConstDef);
return $interfaceConstDef->value;
}
Expand All @@ -3400,6 +3407,17 @@ protected function findNativeClassConst(
if (!$constDef->codegenFinalized) {
return false;
}
// A constant initializer that parse lowered into materialize
// temporaries (tmp_var_N) cannot be inlined: those temporaries are
// declared and assigned only in arrayExpr's prelude, which is
// emitted together with array constants. Inlining the value string
// into the fetching function references locals that do not exist
// there (C2065 / C2593). Fall back to the Zend class-constant
// lookup instead: it reads the already-evaluated literal that was
// registered for the constant and is cached in the convert phase.
if ($constDef->arrayExpr !== '' || str_contains($constDef->value, 'tmp_var_')) {
return false;
}
$expr->setAttribute('nativeConst', $constDef);
return $constDef->value;
}
Expand Down
Loading