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
24 changes: 23 additions & 1 deletion lib/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2170,7 +2170,8 @@ protected function compileOps(array $ops, Block $block): void {
// Interfaces before classes so same-file `class C implements I` / later `interface I`
// resolves at DECLARE_CLASS like Zend early-binding (#25624).
// Enums stay in source order so enum_exists() before declaration matches Zend (#5013).
// Serializable / forbidden-implements stay in source order for DECLARE side effects (#18781, #25109).
// Serializable / forbidden-implements / trait-use stay in source order for DECLARE
// side effects (#18781, #25109, #25912).
foreach ($ops as $child) {
if ($child instanceof Op\Stmt\Interface_) {
$block->addOpCode($this->compileInterface($child, $block));
Expand Down Expand Up @@ -6926,6 +6927,7 @@ protected function defersRuntimeInterfaceImplementationCheck(Op\Stmt\ClassLike $
* - Forbidden implements (DateTimeInterface / reserved): fatals at DECLARE (#18781)
* - Serializable: E_DEPRECATED + class_exists timing match Zend (#22000, #25109)
* - Any implements: DECLARE must run after prior spl_autoload_register (#25624)
* - Trait use: abstract residuals / trait bind fatals at DECLARE after prior stmts (#25912)
*/
protected function requiresSourceOrderClassRegistration(Op\Stmt\ClassLike $class): bool
{
Expand All @@ -6937,11 +6939,31 @@ protected function requiresSourceOrderClassRegistration(Op\Stmt\ClassLike $class
return true;
}

// Keep trait-using classes in source order so preceding opcodes (echo, etc.) run
// before zend_verify_abstract_class / trait bind fatals (#25912).
if ($class instanceof Op\Stmt\Class_ && $this->classDeclaresTraitUse($class)) {
return true;
}

// Keep implements classes in source order so autoload callbacks registered above
// the declaration are visible (Zend early-binds types, not user autoload timing).
return $class instanceof Op\Stmt\Class_ && [] !== $class->implements;
}

/**
* True when the class body contains a `use Trait` statement (#25912).
*/
protected function classDeclaresTraitUse(Op\Stmt\Class_ $class): bool
{
foreach ($class->stmts->children as $member) {
if ($member instanceof Op\Stmt\TraitUse) {
return true;
}
}

return false;
}

protected function staticNameFromOperand(Operand $op): ?string
{
if ($op instanceof Operand\Literal && is_string($op->value)) {
Expand Down
18 changes: 12 additions & 6 deletions lib/VM.php
Original file line number Diff line number Diff line change
Expand Up @@ -7922,7 +7922,11 @@ function () use ($frame, $op, $arg1, $strict, $arraySpec, $vmContext): void {
'parentName' => $parentName,
];
}
$this->flushDeferredParentInheritance();
try {
$this->flushDeferredParentInheritance($frame);
} catch (\CompileError $e) {
$this->raiseClassDeclareCompileFatal($e, $frame);
}
$this->flushDeferredTraitUses($frame);
$this->flushDeferredClassConstants();
break;
Expand Down Expand Up @@ -9702,7 +9706,9 @@ function () use ($frame, $op, $arg1, $strict, $arraySpec, $vmContext): void {
}
if ([] !== $this->context->deferredParentInheritance) {
try {
$this->finalizeDeferredParentInheritance();
$this->finalizeDeferredParentInheritance($frame);
} catch (\CompileError $deferredCompileError) {
$this->raiseClassDeclareCompileFatal($deferredCompileError, $frame);
} catch (\Error $deferredParentError) {
$catchFrame = $this->dispatchVmError($deferredParentError->getMessage(), $frame);
if (null !== $catchFrame) {
Expand Down Expand Up @@ -17605,7 +17611,7 @@ protected function flushDeferredTraitUses(?Frame $warningFrame = null): void
$this->context->deferredTraitUses = $remaining;
}

protected function flushDeferredParentInheritance(): void
protected function flushDeferredParentInheritance(?Frame $frame = null): void
{
if ([] === $this->context->deferredParentInheritance) {
return;
Expand All @@ -17627,14 +17633,14 @@ protected function flushDeferredParentInheritance(): void
$this->assertAllowedBySealedParents($entry->name, $entry->parentLc, $entry->interfaces);
$this->inheritFromParent($entry);
$this->linkStaticPropertyHooks($entry);
VM\ClassValidator::finalizeClassDefinition($entry, $this->context);
VM\ClassValidator::finalizeClassDefinition($entry, $this->context, $frame);
}
$this->context->deferredParentInheritance = $remaining;
}

protected function finalizeDeferredParentInheritance(): void
protected function finalizeDeferredParentInheritance(?Frame $frame = null): void
{
$this->flushDeferredParentInheritance();
$this->flushDeferredParentInheritance($frame);
if ([] === $this->context->deferredParentInheritance) {
return;
}
Expand Down
9 changes: 6 additions & 3 deletions lib/VM/ClassValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ private static function validateInterfaceImplementation(ClassEntry $entry, Conte
[$ifaceLc, $method] = $missing[0];
$ifaceName = $context->classes[$ifaceLc]->name ?? $ifaceLc;

throw new \LogicException(
// php-src: zend_verify_abstract_class — E_COMPILE_ERROR at DECLARE (#25912).
throw new \CompileError(
"Class {$entry->name} contains {$count} abstract method"
.(1 === $count ? '' : 's')
." and must therefore be declared abstract or implement the remaining methods ({$ifaceName}::{$method})"
Expand All @@ -153,7 +154,8 @@ private static function validateAbstractMethodsResolved(ClassEntry $entry): void
$count = count($entry->abstractMethods);
$first = array_key_first($entry->abstractMethods);

throw new \LogicException(
// php-src: zend_verify_abstract_class — E_COMPILE_ERROR at DECLARE (#25912).
throw new \CompileError(
"Class {$entry->name} contains {$count} abstract method"
.(1 === $count ? '' : 's')
." and must therefore be declared abstract or implement the remaining methods ({$entry->name}::{$first})"
Expand All @@ -173,7 +175,8 @@ private static function validateAbstractPropertyHooksResolved(ClassEntry $entry,
$missing
));

throw new \LogicException(
// php-src: zend_verify_abstract_class — E_COMPILE_ERROR at DECLARE (#25912).
throw new \CompileError(
"Class {$entry->name} contains {$count} abstract method"
.(1 === $count ? '' : 's')
." and must therefore be declared abstract or implement the remaining methods ({$list})"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Language: trait abstract method unimplemented — DECLARE-time fatal after prior stmts (#25912, Zend/zend_inheritance.c)
--INI--
display_errors=1
--FILE--
<?php
trait TAbs {
abstract public function f();
}
echo "before\n";
class C {
use TAbs;
}
echo "accepted\n";
--EXPECTF--
before

Fatal error: Class C contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (C::f) in %s on line %d
--EXPECT_EXIT--
255
22 changes: 22 additions & 0 deletions test/repro/maintainer_gap_trait_abstract_unimplemented_fatal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Issue #25912 — concrete class using trait with unimplemented abstract method.
*
* Zend runs preceding statements, then E_COMPILE_ERROR at class DECLARE
* (Zend/zend_compile.c / zend_inheritance.c zend_verify_abstract_class).
*
* Expected (php-src-strict):
* stdout: before
* stderr: PHP Fatal error: Class C contains 1 abstract method… (C::f)
* exit 255; no "accepted"
*/
trait TAbs
{
abstract public function f();
}
echo "before\n";
class C
{
use TAbs;
}
echo "accepted\n";
74 changes: 74 additions & 0 deletions test/unit/TraitAbstractUnimplementedFatalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\Test\Unit;

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

/**
* @covers issue #25912
*
* php-src: Zend/zend_inheritance.c — zend_verify_abstract_class at class DECLARE.
*/
final class TraitAbstractUnimplementedFatalTest extends TestCase
{
public function testPrecedingEchoRunsThenCompileFatal(): void
{
$runtime = new Runtime();
$code = <<<'PHP'
<?php
trait TAbs {
abstract public function f();
}
echo "before\n";
class C {
use TAbs;
}
echo "accepted\n";
PHP;
$block = $runtime->parseAndCompile($code, 'trait_abstract_unimpl.php');
$this->assertNotNull($block);

ob_start();
try {
// bubbleUncaught=false — ScriptExit like bin/vm.php CLI (#25912).
$runtime->run($block, false);
ob_end_clean();
$this->fail('Expected ScriptExit for unimplemented trait abstract method');
} catch (\CompileError $e) {
$out = ob_get_clean();
$this->assertStringContainsString('before', (string) $out);
$this->assertStringNotContainsString('accepted', (string) $out);
$this->assertStringContainsString('Class C contains 1 abstract method', $e->getMessage());
$this->assertStringContainsString('(C::f)', $e->getMessage());
} catch (ScriptExit $e) {
$out = ob_get_clean();
$this->assertSame(255, $e->status);
$this->assertStringContainsString('before', (string) $out);
$this->assertStringNotContainsString('accepted', (string) $out);
// Fatal body goes to stderr as "PHP Fatal error:" (display_errors=0); CLI repro covers that.
}
}

public function testImplementedTraitAbstractCompilesAndRuns(): void
{
$runtime = new Runtime();
$code = <<<'PHP'
<?php
trait TAbs {
abstract public function f(): int;
}
class C {
use TAbs;
public function f(): int { return 7; }
}
echo C::class, ":", (new C())->f(), "\n";
PHP;
ob_start();
$runtime->run($runtime->parseAndCompile($code, 'trait_abstract_ok.php'));
$this->assertSame("C:7\n", ob_get_clean());
}
}
Loading