Skip to content

Regression: BackedEnum::from()/tryFrom() lookup broken — valid backing values NULL / ValueError (re-#3114, Zend/zend_enum.c) #9967

Description

@PurHur

Category

language | stdlib · php-src-strict · regression vs closed #3114

Problem

BackedEnum::tryFrom() and BackedEnum::from() must resolve a backing scalar to the canonical enum case object (E::A). Audit (2026-06-19): both methods fail for valid backing values — tryFrom(1) returns NULL and from(1) throws ValueError: 1 is not a valid backing value even when E::A = 1 exists.

This blocks enum deserialization, HTTP/query parsing, and any code using tryFrom/from at runtime. Distinct from #9889 (invalid-value error message when lookup path runs).

php-src reference

Repro

test/repro-maintainer/backed_enum_from_tryfrom.php:

<?php
enum E: int { case A = 1; case B = 2; }

var_export(E::tryFrom(1));
echo "\n";
var_export(E::from(1));
echo "\n";

enum Color: string { case Red = 'red'; }
var_export(Color::tryFrom('red'));
echo "\n";

Commands

php test/repro-maintainer/backed_enum_from_tryfrom.php
# Zend:
# \E::A
# \E::A
# \Color::Red

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php test/repro-maintainer/backed_enum_from_tryfrom.php'
# VM today:
# NULL
# ValueError: 1 is not a valid backing value for enum E
Call Zend VM (today)
E::tryFrom(1) \E::A NULL
E::from(1) \E::A ValueError
Color::tryFrom('red') \Color::Red NULL

Scope (PHP-in-PHP)

Path Work
lib/VM/BackedEnum.php caseForValue() / matchCaseForBackingValue() — ensure ClassEntry::$enumCases populated at runtime
lib/VM/EnumFromHandler.php execute() return path via BackedEnum::canonicalCaseVariable()
lib/VM/EnumSupport.php Case table / constant fallback (#9603)
Tests test/compliance/cases/language/backed_enum_from_tryfrom.phpt

No new C in runtime/ — lookup belongs in PHP VM helpers.

Done when (php-src-strict)

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 featuresstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions