Skip to content

Reject new in class constant initializers to match Zend (#10391) - #10559

Merged
PurHur merged 1 commit into
masterfrom
agent/issue-10391-class-const-new-reject
Jun 22, 2026
Merged

PurHur merged 1 commit into
masterfrom
agent/issue-10391-class-const-new-reject

Conversation

@PurHur

@PurHur PurHur commented Jun 22, 2026

Copy link
Copy Markdown
Owner

Summary

  • Restore compile-time rejection of all new expressions in class constant initializers via NewWithoutParensCompileCheck (php-src: Zend/zend_compile.c zend_compile_const_expr()).
  • Reverts the incorrect Allow new expressions in class constant initializers (#10198) #10206 allowance; CompilerVersion::supportsClassConstObjectExpressions() now always returns false.
  • Updates compliance/unit/AOT guards to expect compile exit 255.

Closes #10391

php-src reference

  • Zend/zend_compile.c — class constant new expression validation
  • Zend/zend_constants.c — constant object materialization (indirect via global const only)

PHP implementation

  • lib/Compiler/NewWithoutParensCompileCheck.php — walks Const_ terminals and rejects nested New_
  • lib/CompilerVersion.php — deprecates supportsClassConstObjectExpressions()

Verification

# Issue repro — Zend fatal vs VM compile error (exit 255)
php test/repro/maintainer_gap_new_in_constant.php 2>&1 | tail -1
php bin/vm.php test/repro/maintainer_gap_new_in_constant.php 2>&1 | tail -1

# Maintainer parity script
php test/repro-maintainer/parity_new_without_parens.php

# Targeted PHPUnit
php vendor/bin/phpunit test/unit/NewWithoutParensCompileCheckTest.php
php vendor/bin/phpunit test/compliance/VMTest.php --filter class_const_new

Output (2026-06-22)

# Zend
PHP Fatal error:  New expressions are not supported in this context in .../maintainer_gap_new_in_constant.php on line 8

# VM
parseAndCompile failure: target=.../maintainer_gap_new_in_constant.php: New expressions are not supported in this context

# parity_new_without_parens.php
PASS: class const
PASS: static property
PASS: instance property
ok

# PHPUnit
OK (8 tests, 14 assertions)  # NewWithoutParensCompileCheckTest
OK (4 tests, 4 assertions)   # VMTest --filter class_const_new

Made with Cursor

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: 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.

Regression: class const new expression compiles and runs — must match PHP 8.3 compile gate (re-#10312, Zend/zend_constants.c)

1 participant