Skip to content

Allow new expressions in class constant initializers (#10198) - #10206

Merged
PurHur merged 1 commit into
masterfrom
agent/issue-10198-class-const-new-expression
Jun 20, 2026
Merged

PurHur merged 1 commit into
masterfrom
agent/issue-10198-class-const-new-expression

Conversation

@PurHur

@PurHur PurHur commented Jun 20, 2026

Copy link
Copy Markdown
Owner

Summary

  • Enable PHP 8.3+ new expressions in class constant initializers by removing the stale compile-time rejection in NewWithoutParensCompileCheck (static property new defaults remain forbidden).
  • Flip CompilerVersion::supportsClassConstObjectExpressions() to true on 8.3+ target; existing VM/JIT materialization path (finalizePendingNewClassConst, ClassConstMaterializer) handles runtime object constants with shared identity.
  • Update compliance/unit tests from expect-255 to php-src-strict success guards.

php-src reference

  • Zend/zend_compile.c — zend_compile_const_expr(), ZEND_AST_NEW in constant context (PHP 8.3+)
  • Zend/zend_constants.c — object-typed class constants

Verification

Issue repro:

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php test/repro/maintainer_gap_new_in_constant.php'
int(1)

Object identity:

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php -r "
class C { public function __construct(public int \$n = 0) {} }
class Holder { public const X = new C(1); }
var_dump(Holder::X->n);
echo Holder::X === Holder::X ? \"same\\n\" : \"diff\\n\";
"'
int(1)
same

PHPUnit:

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && ./vendor/bin/phpunit test/compliance/VMTest.php --filter "class_const_new|class_const_object|const_expr_new|new_in_constant"'
# OK (9 tests)

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && ./vendor/bin/phpunit test/compliance/JITTest.php --filter "class_const_object"'
# OK (2 tests)

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && ./vendor/bin/phpunit --filter "NewWithoutParensCompileCheckTest|CompilerVersionGateTest|ClassConstObject"'
# OK (16 tests)

Closes #10198

Made with Cursor

PHP 8.3+ permits object class constants; remove the compile-time rejection
that blocked valid code while keeping static-property `new` defaults forbidden.

Co-authored-by: Cursor <cursoragent@cursor.com>
@PurHur
PurHur merged commit 15b87d5 into master Jun 20, 2026
@PurHur
PurHur deleted the agent/issue-10198-class-const-new-expression branch June 20, 2026 00:49
PurHur added a commit that referenced this pull request Jun 22, 2026
…10559)

php-src rejects all `new` in class const expressions at compile time;
#10206 incorrectly re-enabled them. Restore the gate in NewWithoutParensCompileCheck
and retire supportsClassConstObjectExpressions.

Co-authored-by: PurHur <PurHur@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
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.

Language: class constant new expression — compile rejects valid PHP 8.3+ (Zend/zend_compile.c)

1 participant