Skip to content

Language: property/parameter/static defaults with enum case ->value — compile-time constant folding (zend_compile.c, extends #3803) #7399

Description

@PurHur

Category

language

Problem

Zend treats E::Case->value (and similar enum-case member fetches) as a compile-time constant for default values of properties, static properties, and parameters. This compiler rejects them with #3803 ("Property/Parameter default must be a compile-time constant") even though the same expression works in const G = E::A->value; at file scope.

Blocks idiomatic PHP 8.1+ defaults that mirror backing values without magic numbers.

php-src reference

Repro (failure today)

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

class C {
    public int $n = E::A->value;           // compile error VM; Zend OK
    public static int $s = E::A->value;    // compile error VM; Zend OK
}
function f(int $n = E::A->value): int { return $n; } // compile error VM; Zend OK
var_dump((new C())->n, C::$s, f());
docker info >/dev/null
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php test/repro/maintainer_enum_value_property_default.php  # parseAndCompile #3803
php bin/vm.php test/repro/maintainer_static_default_enum_value.php
php bin/vm.php test/repro/maintainer_param_default_enum_value.php
/usr/bin/php test/repro/maintainer_param_default_enum_value.php        # int(1)
'
Form Zend VM
const G = E::A->value; int(1) int(1)
public int $n = E::A->value compiles #3803 fatal
public static int $s = E::A->value compiles #3803 fatal
function f($n = E::A->value) compiles #3803 fatal

php-src-strict: unit enum cases without ->value remain compile errors; only backed-case ->value (and other Zend-allowed constant exprs) fold.

Scope (this repo)

Layer Path
Compiler lib/Compiler.php — extend default-value folder for EnumCaseFetch->value
CFG constant expression evaluator shared with file-scope const
VM ensure default slots materialize before instance/static init
Tests test/compliance/cases/language/enum_case_value_default.phpt

PHP-in-PHP: fold in compiler — no C runtime default tables.

Done when

  • All three default forms in repro compile and print int(1) on VM
  • Unit enum E { case A; } with ->value default still compile-errors like Zend
  • ./script/ci-fast.sh --filter enum_case_value_default 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:compilerCompiler / CFG / JITarea: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