Skip to content

Language: enum_from() — BackedEnum::from / tryFrom (PHP 8.1) #3114

Description

@PurHur

Category

language

Problem

Backed enums compile (#1356), but BackedEnum::from() / tryFrom() validation is missing or incomplete. Zend maps a scalar to the matching case or throws ValueError / returns null.

php-src reference

Repro (today)

Save as repro_enum_from.php (same shape as test/compliance/cases/language/enum_basic.phpt):

<?php
enum Color: string {
    case Red = 'red';
    case Blue = 'blue';
}
echo Color::from('red')->name;
echo Color::tryFrom('nope') === null ? 'null' : 'bad';
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro_enum_from.php'
Runtime Expected output
Zend PHP Rednull
This compiler (May 2026) LogicException / wrong case / compile or php-cfg parse failure before dispatch

Observed (2026-05-29 harness)

On some harness images, enum TUs fail early in php-cfg with:

PHPCfg\Op\Stmt\Enum_::__construct(): Argument #3 ($implements) must be of type array, PHPCfg\Block given

Worker baseline before implementing from/tryFrom:

./script/ci-fast.sh --filter VmEnumBasicTest
./script/ci-fast.sh --filter enum_basic

If enum parse fails in Docker, run script/apply-patches.sh in the image and re-check; do not stub from() with internal_*.

Scope

Area Path
VM lib/VM.php — backed enum case table lookup
Compiler lib/Compiler.php — Enum::from / tryFrom static call lowering
Tests test/compliance/cases/language/enum_from.phpt, enum_try_from.phpt
JIT/AOT after VM green; coordinate #3076 AOT enum metadata

Done when

  • from('red') returns Color::Red; invalid scalar → ValueError with Zend-like message
  • tryFrom('nope') returns null without throw
  • ./script/ci-fast.sh --filter enum_from green
  • No regression on enum_basic.phpt / VmEnumBasicTest

Links

Labels

implementation-ready

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