Skip to content

Language: Enum::tryFrom()/from() — valid backing values return NULL / ValueError (Zend/zend_enum.c) #9603

Description

@PurHur

Category

language · php-src-strict

Problem

Backed enum static factories Enum::tryFrom() and Enum::from() do not resolve valid backing scalars. Zend returns the matching enum case object (or null / ValueError for invalid input). This compiler returns null for every tryFrom() call and raises ValueError on valid from() input.

This breaks deserialization, config parsing, and any code using tryFrom/from for string/int backed enums.

php-src reference

Repro (verified 2026-06-18)

<?php
enum E: string {
    case A = 'a';
}

var_dump(E::tryFrom('b')); // Zend: NULL
var_dump(E::tryFrom('a')); // Zend: enum(E::A)

try {
    E::from('a');
    echo "from ok\n";
} catch (ValueError $e) {
    echo 'from fail: ', $e->getMessage(), "\n";
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php repro_enum_from.php
php bin/vm.php repro_enum_from.php
'
Call Zend PHP 8.2 bin/vm.php (today)
E::tryFrom('a') enum(E::A) NULL ❌
E::tryFrom('b') NULL NULL
E::from('a') enum(E::A) ValueError ❌

Int-backed enums show the same pattern (E::tryFrom(1) → NULL).

Scope (this repo)

Layer Path Notes
VM lib/VM/EnumFromHandler.php, enum case tables on ClassEntry Backing-value → case object lookup
Compiler lib/Compiler.php enum emission Ensure case table keys match runtime lookup
JIT/AOT enum static call lowering Same semantics as VM when in scope
Tests test/compliance/cases/language/enum_try_from.phpt, enum_from.phpt

PHP-in-PHP first: lookup logic in lib/VM/ PHP; no new C runtime.

Done when

Verification

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

Related

No activity

Activity on this issue will appear here.

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