Skip to content

Stdlib: pack() on backed enum operands — must warn and pack backing int, not VmJson::export fatal (ext/standard/pack.c) #5713

Description

@PurHur

Category

stdlib

Problem

pack() forwards value operands through VmJson::export() in ext/standard/pack.php. Backed enum case arguments make the VM die with json_encode() value type not supported in this compiler build instead of matching Zend: E_WARNING (“Object of class E could not be converted to int”) and pack the backing scalar (pack('i', E::A) → 4-byte int 1).

Distinct from #4951 (hash() TypeError wording) and enum array coercion tickets.

php-src reference

Repro

test/repro/parity_pack_backed_enum.php:

<?php
declare(strict_types=1);

enum E: int { case A = 1; }

try {
    $p = pack('i', E::A);
    echo 'len=', strlen($p), "\n";
} catch (Throwable $e) {
    echo get_class($e), ': ', $e->getMessage(), "\n";
}
docker info >/dev/null
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro/parity_pack_backed_enum.php 2>&1 | head -5
php bin/vm.php test/repro/parity_pack_backed_enum.php 2>&1 | head -5'
Runtime Expected
Zend Warning + len=4 (32-bit int packing of 1)
VM today Fatal / LogicException via VmJson::export

Scope (PHP-in-PHP)

Path Work
ext/standard/pack.php Coerce enum cases to backing via BackedEnum::backingValue before pack
lib/AOT/runtime/phpc_pack.c JIT/AOT: same coercion; no enum branch in C long-term (#4675 machine formats)
Tests test/compliance/cases/stdlib/pack_backed_enum.phpt

Done when

  • pack('i', E::A) succeeds on VM with same byte length as Zend (little-endian host)
  • Warning text matches Zend subset (contains “could not be converted” / enum class name)
  • Unit enum / object operands still TypeError or warn per format — no VmJson::export fatal
  • ./script/ci-fast.sh --filter pack_backed_enum green

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

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions