Skip to content

Language: instanceof with union types — (A|B) RHS (Zend zend_type.c parity) #3461

Description

@PurHur

Category

language

Problem

PHP 8.0+ allows instanceof against a union of class/interface names: $obj instanceof (A|B). This compiler treats the parenthesized union as a bogus constant fetch and fails at runtime.

php-src reference

  • Zend/zend_compile.c — zend_compile_instanceof() / instanceof_list handling
  • Zend/zend_type.c — zend_type_apply_mask(), union type lists
  • Zend/zend_execute.c — ZEND_INSTANCEOF, instanceof_function_ex()

Repro

<?php
interface A {}
interface B {}
class C implements A, B {}
var_dump((new C) instanceof (A|B));
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'
php repro.php   # Zend: bool(true)

This compiler (May 2026): LogicException: Unknown constant fetch
Zend PHP: bool(true)

Scope (this repo)

Layer Files
php-cfg / parser Union type in instanceof RHS if not already emitted
Compiler lib/Compiler.php — compileExpr for Expr_Instanceof
VM lib/VM.php — walk implemented interfaces/classes for union members
JIT lib/JIT.php — after VM PHPT green
Tests test/compliance/cases/language/instanceof_union.phpt

Done when

  • Repro prints bool(true) on VM; false when object implements neither arm
  • Compile error for invalid union members (Zend subset)
  • ./script/ci-fast.sh --filter instanceof_union green
  • docs/capabilities-syntax.md row or note for union instanceof (regenerate matrix script)

Verification

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

Related

Activity

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