Skip to content

Regression: throw expression non-throwing branch returns empty — ?? RHS must yield value (Zend/zend_compile.c) #9447

Description

@PurHur

Category

language · php-src-strict

Problem

PHP 8.0+ throw expressions work in throwing branches ($x ?? throw ... when $x is null throws), but when the non-throwing branch is taken the expression must return the left-hand value.

Verified locally (2026-06-18):

Call Zend PHP 8.2 This compiler VM
f(1) for return $x ?? throw new Exception("e"); int(1) empty string / no value (echo f(1) prints x= only)
f(null) throws throws (OK)

Distinct from parse rejection — syntax compiles; runtime lowering of ZEND_AST_THROW in coalesce/ternary contexts loses the RHS value.

php-src reference

Repro

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php -r "
function f(?int \$x): int {
    return \$x ?? throw new Exception(\"e\");
}
try {
    echo \"null=\", f(null), PHP_EOL;
} catch (Throwable \$e) {
    echo \"caught\n\";
}
echo \"one=\", f(1), PHP_EOL;
"'
# Zend: caught / one=1
# VM today: caught / one=   (empty)

Scope (this repo)

Path Work
lib/Compiler.php Throw-expression coalesce/ternary lowering
lib/VM.php Opcode execution for coalesce + throw expr
lib/JIT/ Match VM when JIT compiles throw expressions

Done when

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