Skip to content

fix(attribute): mark ClassConstFetch as lazy to resolve enum attribute args - #8

Closed
yuan-dian wants to merge 2 commits into
swoole:masterfrom
yuan-dian:fix/enum-attribute-lazy-value
Closed

fix(attribute): mark ClassConstFetch as lazy to resolve enum attribute args#8
yuan-dian wants to merge 2 commits into
swoole:masterfrom
yuan-dian:fix/enum-attribute-lazy-value

Conversation

@yuan-dian

Copy link
Copy Markdown
Contributor

Bug

When a backed enum value is used as an attribute argument (e.g. #[TableId(IdType::AUTO)]), the compiled binary stores the string backing value instead of the enum instance.

Reproduction:

enum Status: string { case Active = 'active'; }

#[Attribute(Attribute::TARGET_CLASS)]
class ValidateStatus {
    public function __construct(public Status $status = Status::Active) {}
}

#[ValidateStatus(Status::Active)]
class User {}
- PHP mode: ✅ $status is Status enum instance
- Compiled mode: ❌ $status is string "active", throws TypeError
Root Cause
RuntimeAttributeFactoryLowering::requiresLazyValue() did not recognize ClassConstFetch nodes. A factory function was generated (via requiresFactory()), but without the lazy marker the PHPX bridge never invoked it — the arginfo stored the raw string instead of calling php::getEnumCase().
Fix
Add Expr\ClassConstFetch to requiresLazyValue() so the lazy marker is set and the factory is invoked at runtime.
Before: ZVAL_INTERNED_STR(..., "active")
After: typephp_attribute_set_lazy_value_argument(..., factory) → php::getEnumCase("Status", "Active")

…e args

ClassConstFetch expressions (e.g. Status::Active) in attribute arguments
were not marked as lazy values, so the PHPX bridge stored the backing
string value instead of calling the factory that resolves the enum case.

Add ClassConstFetch to requiresLazyValue() so the lazy marker is set and
the factory function is invoked at runtime via
typephp_attribute_set_lazy_value_argument().
@matyhtf matyhtf closed this Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants