Skip to content

Regression: BackedEnum::from()/tryFrom() always ValueError — valid backing scalars never match (re-#3114, Zend/zend_enum.c) #9575

Description

@PurHur

Category

language · php-src-strict

Problem

#3114 closed with VM handlers in lib/VM/EnumFromHandler.php and lib/VM/BackedEnum.php, but BackedEnum::from() / ::tryFrom() are broken at runtime: every lookup returns no match and from() throws ValueError even for exact backing scalars. tryFrom() returns null for valid values.

This blocks enum deserialization, config mapping, and any code using the standard backed-enum factory API.

php-src reference

Repro

Save as test/repro/maintainer_enum_from_regression.php:

<?php
declare(strict_types=1);

enum Color: string { case Red = 'red'; case Blue = 'blue'; }
enum Count: int { case One = 1; case Two = 2; }

// string-backed — exact match
var_dump(Color::from('red'));
var_dump(Color::tryFrom('red'));

// int-backed — int and numeric-string coercion
var_dump(Count::from(1));
var_dump(Count::tryFrom('1'));

// invalid — from throws ValueError, tryFrom null
try {
    Color::from('nope');
    echo "from_invalid_ok\n";
} catch (ValueError $e) {
    echo 'from_invalid: ', $e->getMessage(), "\n";
}
var_dump(Color::tryFrom('nope'));

Commands

php test/repro/maintainer_enum_from_regression.php
php bin/vm.php test/repro/maintainer_enum_from_regression.php

Verified (2026-06-18, host PHP 8.2 vs bin/vm.php)

Probe Zend VM today
Color::from('red') enum(Color::Red) ValueError: "red" is not a valid backing value for enum Color
Color::tryFrom('red') enum(Color::Red) NULL
Count::from(1) enum(Count::One) ValueError: 1 is not a valid backing value for enum Count
Count::tryFrom('1') enum(Count::One) NULL
Color::from('nope') ValueError ValueError (message OK)
Color::tryFrom('nope') NULL NULL

Scope (PHP-in-PHP)

Layer Path
VM lookup lib/VM/BackedEnum.php — caseForValue(), backingValuesMatch(), caseBackingScalar() vs ClassEntry::$enumCases storage
Handler lib/VM/EnumFromHandler.php
Enum registration lib/VM/EnumSupport.php, lib/Compiler.php enum compile path — ensure case backing values visible to lookup
JIT lib/JIT/BackedEnumFromJit.php, lib/JIT/EnumFromHelper.php — must match VM after fix
Tests test/compliance/cases/language/enum_from.phpt (restore/extend post-#3114)

No new C in runtime/ — fix enum case table + PHP helpers only.

Done when (php-src-strict)

Related

Activity

  1. added
    enhancementNew feature or request
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jun 18, 2026
  2. PurHur commented on Jun 18, 2026

    @PurHur
    OwnerAuthor

    claim: worker-lane-c — starting this run

  3. PurHur commented on Jun 18, 2026

    @PurHur
    OwnerAuthor

    Merged via #9598

    Root cause: BackedEnum::from() / tryFrom() VM lookup was already correct; the regression surfaced in enum_from_spaceship.phpt / var_dump(E::A <=> E::from(1)) where php-cfg emits a dead call-arg temp and the compiler only rewired boolean inline producers (#9030), not TYPE_LONG spaceship results.

    Fix: extend findInlineExprCallArgProducerSlot() to match preceding binary/InstanceOf producers with TYPE_LONG result type.

    Verification:

    php bin/vm.php test/repro/maintainer_enum_from_regression.php
    php bin/jit.php test/repro/maintainer_enum_from_regression.php
    vendor/bin/phpunit --filter enum_from  # 11/12 — VM+JIT spaceship green; pre-existing AOT try/catch ValueError on fixtures/aot/cases/enum_from.phpt unchanged
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