Skip to content

Language: backed enum arithmetic with int/float must TypeError — E::A + 1 (Zend/zend_operators.c) #9568

Description

@PurHur

Category

language — php-src-strict enum operators

Problem

Backed enum cases are not silently coerced to their backing scalar for +, -, *, /, %, and related numeric operators. Zend throws TypeError: Unsupported operand types: E + int.

This compiler evaluates E::A + 1 as 2 when E::A backs 1, breaking enum operator parity.

Related but narrower: #8972 (array_reduce accumulator), #9546 (===).

php-src reference

Repro (failure today)

Save as test/repro-maintainer/enum_numeric_arithmetic.php:

<?php
declare(strict_types=1);

enum E: int { case A = 1; }

foreach (['+', '-', '*'] as $op) {
    try {
        $expr = 'E::A ' . $op . ' 1';
        echo $expr, '=', eval('return ' . $expr . ';'), "\n";
    } catch (Throwable $e) {
        echo $expr, ' ', get_class($e), "\n";
    }
}

Minimal single-op repro:

<?php
enum E: int { case A = 1; }
try { echo E::A + 1; } catch (Throwable $e) { echo get_class($e), "\n"; }
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro-maintainer/enum_numeric_arithmetic.php
php bin/vm.php test/repro-maintainer/enum_numeric_arithmetic.php'
Check Zend VM (today)
E::A + 1 TypeError prints 2 ❌
E::A - 1 TypeError coerced math ❌
E::A * 2 TypeError coerced math ❌

Scope (PHP-in-PHP)

Layer Path
VM lib/VM/Variable.php binary ops / lib/VM.php opcode handlers — reject enum+scalar before backing coercion
JIT/AOT lib/JIT/JitValueCompare.php, arithmetic lowering — same guard
Shared central enum-operand helper (pair with #9546)

Done when

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

    IMPORTANTCritical patharea: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