Skip to content

Language: compile-time inheritance variance (LSP vs Zend) - #3332

Merged
PurHur merged 4 commits into
masterfrom
agent/issue-3323-inheritance-variance
May 30, 2026
Merged

PurHur merged 4 commits into
masterfrom
agent/issue-3323-inheritance-variance

Conversation

@PurHur

@PurHur PurHur commented May 29, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds lib/Compiler/InheritanceVariance.php to validate parameter contravariance and return covariance when a class overrides a parent or interface method in the same compile unit.
  • Wired into Compiler::compile() so incompatible overrides fail at compile time with Zend-shaped messages (Declaration of … must be compatible with …).
  • php-src reference: Zend/zend_inheritance.c (zend_verify_type_compatibility), Zend/zend_type.c.

Notes

  • PHP 8.2 allows wider parameter types (e.g. parent B $x, child A $x when B extends A); narrower parameters and wider returns are rejected (matching Zend).
  • Checks are limited to class/interface/trait definitions in the current script (no autoloaded external parents).

Test plan

./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'

Closes #3323

Made with Cursor

PurHur and others added 2 commits May 29, 2026 20:05
Validate parameter contravariance and return covariance against parent
classes and interfaces at compile time, matching Zend zend_inheritance.c.

Co-authored-by: Cursor <cursoragent@cursor.com>
PurHur and others added 2 commits May 30, 2026 01:52
…#3323)

Guard narrow parameter and widened return override violations that
InheritanceVariance rejects at parseAndCompile time.

Co-authored-by: Cursor <cursoragent@cursor.com>
@PurHur
PurHur merged commit 5182131 into master May 30, 2026
@PurHur
PurHur deleted the agent/issue-3323-inheritance-variance branch May 30, 2026 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant