Skip to content

Language: #[\Override] on trait method alias must be compile-time fatal (zend_compile.c) #6440

@PurHur

Description

@PurHur

Category

language

Problem

#[\Override] on a method that only overrides a trait-provided method (no parent class method in the inheritance chain) must fail at compile time in php-src. Partial coverage exists: trait alias + Override on child is already rejected; direct trait method redefinition in the same class still compiles.

Verified 2026-06-05 on bin/vm.php:

Scenario Zend 8.3+ This compiler
Class use T; #[\Override] public function f() redefines trait method Compile-time fatal Compiles silently
Child overrides trait alias T::f as g with #[\Override] Compile-time fatal Already rejected ✅

php-src reference

Repro

Still broken (direct trait override):

<?php
trait T { public function f(): void {} }
class C {
    use T;
    #[\Override]
    public function f(): void { echo "class\n"; }
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
cat > /tmp/override_trait_direct.php <<'"'"'PHPEOF'"'"'
<?php
trait T { public function f(): void {} }
class C {
    use T;
    #[\Override]
    public function f(): void { echo "class\n"; }
}
PHPEOF
php bin/vm.php /tmp/override_trait_direct.php   # compiles — should fatal at compile time
'

Zend expected:

Fatal error: C::f() has #[\Override] attribute, but no matching parent method exists

Already fixed (alias case — regression guard):

trait T { public function f(): void {} }
class C { use T { f as g; } }
class D extends C {
    #[\Override]
    public function g(): void {}
}

Scope

Path Work
lib/Compiler/OverrideValidator.php (or lib/Compiler.php) Treat trait-origin methods as non-parent for Override target walk
test/compliance/cases/language/ PHPT compile-fail fixtures

Done when

  • Direct trait override repro fails at compile time with Zend message (VM never runs echo)
  • Alias regression repro still fails compile (no regression)
  • test/compliance/cases/language/override_trait_method.phpt added (EXPECT compile failure)
  • JIT/AOT inherit compile-time check (no attribute bypass)

Strictness

php-src-strict — compile-time parity with Zend attribute semantics.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compilerCompiler / CFG / JITarea:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-2:languagePhase 2 – language features

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions