Skip to content

Language: typed trait constants (PHP 8.3) — const type enforcement when importing traits (Zend/zend_compile.c) #6012

Description

@PurHur

Category

language

Problem

PHP 8.3 adds typed class constants. Typed constants inside traits must behave like Zend when imported into a class:

  • The constant must carry its declared type.
  • The value must be validated against the declared type at compile time (or at least fail with the same fatal/error behavior as Zend).
  • If a class overrides a trait constant, type compatibility rules must match Zend.

This repo already tracks typed interface constants (#5980) and interface constant inheritance compatibility (#5982). Traits are a distinct path (trait import/flattening) and need separate parity coverage.

php-src reference

  • Zend/zend_compile.c — typed constant declaration and validation
  • Zend/zend_inheritance.c — trait constant import and conflict resolution

Repro

Save as test/repro-maintainer/typed_trait_constants.php:

<?php
trait TGood {
    public const int X = 1;
}

final class C {
    use TGood;
}

echo C::X, "\n";

Also validate error parity (value/type mismatch) with test/repro-maintainer/typed_trait_constants_mismatch.php:

<?php
trait TBad {
    public const int X = '1';
}

final class C {
    use TBad;
}

Run:

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro-maintainer/typed_trait_constants.php
php bin/vm.php test/repro-maintainer/typed_trait_constants.php

php test/repro-maintainer/typed_trait_constants_mismatch.php
php bin/vm.php test/repro-maintainer/typed_trait_constants_mismatch.php
'

Expected (Zend):

  • First script prints 1.
  • Second script fails with a compile-time fatal/error indicating the constant value is not of type int.

Observed (this compiler today):

  • Either parse/compile failure for typed constants in traits, or missing type enforcement, or incorrect constant metadata.

Scope (this repo)

  • lib/Compiler.php — lowering typed const declarations inside traits
  • lib/VM/ — trait constant import into class entry, preserving declared type metadata
  • lib/VM/Constant* / metadata structures — ensure typed const type is carried through
  • Tests:
    • test/compliance/cases/language/typed_trait_constants.phpt
    • test/compliance/cases/language/typed_trait_constants_mismatch.phpt

Done when

  • Typed constants in traits compile and are accessible via C::X on VM
  • Mismatch script fails with the same error class and message shape as Zend
  • Trait + class override compatibility matches Zend (add a third PHPT if needed)
  • ./script/ci-fast.sh --filter typed_trait_constants is green

Labels

php-src-strict (default) — error shapes and type enforcement must match Zend.

Links

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