Skip to content

Language: compile-time inheritance variance — parameter/return LSP vs Zend (zend_inheritance.c parity) #3323

Closed

Description

@PurHur

Category

language

Problem

Zend validates return type covariance and parameter type contravariance when a child class overrides a parent method or implements an interface. This compiler accepts incompatible signatures silently, allowing calls that would be compile-time errors on php-src.

php-src reference

  • Zend/zend_inheritance.c — zend_check_inheritance, zend_verify_type_compatibility
  • Zend/zend_type.c — zend_type_compare / assignability for inheritance
  • Zend/zend_compile.c — override validation at class compile time

Repro (today)

Contravariant parameter violation (should not compile)

<?php
interface I { public function f(B $x): void; }
class A {}
class B extends A {}
class C implements I { public function f(A $x): void {} }
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'

Zend: compile error (Declaration of C::f(A $x) must be compatible with I::f(B $x)).
This compiler: prints ok (no error).

Covariant return violation

<?php
class Base { public function create(): self { return $this; } }
class Child extends Base { public function create(): Child { return $this; } }

Document Zend vs this compiler before/after fix (covariant self/static child returns are allowed; incompatible narrowing is not).

Scope (this repo)

  • lib/Compiler.php — after method merge, call type-compatibility check against parent/interface slot
  • Reuse lib/Type.php / php-types assignability where possible
  • test/compliance/cases/language/inheritance_variance_*.phpt
  • JIT/AOT: inherit compile-time rejection (no separate runtime-only hack)

Done when

  • Contravariant repro fails at compile time with Zend-shaped error
  • Covariant return narrowing cases covered by compliance tests
  • ./script/ci-fast.sh --filter inheritance_variance green

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter inheritance_variance'

Links

Activity

  1. added
    enhancementNew feature or request
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on May 29, 2026
  2. PurHur commented on May 29, 2026

    @PurHur
    OwnerAuthor

    claim: cursor-agent-lane-c — starting this run

  3. PurHur commented on May 29, 2026

    @PurHur
    OwnerAuthor

    Lane C continuation — PR #3332 verified after merge with master.

    Verification (Docker)

    ./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter InheritanceVariance'
    ./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter inheritance_variance test/compliance/VMTest.php'

    Both green (4 unit + 2 compliance).

    Zend parity spot-check

    • Wide parameter (I::f(B $x) / C::f(A $x)): Zend allows; compiler allows.
    • Narrow parameter (I::f(A $x) / C::f(B $x)): Zend fatal Declaration of … must be compatible; compiler CompileError with same message shape.

    Done-when checklist on the issue is satisfied on branch agent/issue-3323-inheritance-variance — ready for review/merge.

  4. PurHur commented on May 30, 2026

    @PurHur
    OwnerAuthor

    claim: cursor-agent-lane-c — continuing PR #3332 this run

  5. PurHur commented on May 30, 2026

    @PurHur
    OwnerAuthor

    Verification (cursor-agent-lane-c, this run):

    • Merged origin/master into PR Language: compile-time inheritance variance (LSP vs Zend) #3332.
    • Added compliance PHPTs for compile-time failures: inheritance_variance_param_narrow_fail.phpt, inheritance_variance_return_wide_fail.phpt.
    • Tests green:
      ./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter InheritanceVariance'
      ./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter inheritance_variance test/compliance/VMTest.php'

    php-src ref: Zend/zend_inheritance.c (zend_verify_type_compatibility), Zend/zend_type.c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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