Skip to content

Language: throw expression in assignment — $x = throw new Exception() (Zend zend_compile.c parity) #3521

Description

@PurHur

Category

language

Problem

Throw expressions outside throw; statements are not lowered. Assignment RHS $x = throw new Exception('e') fails in php-cfg:

Unknown Expr Type Expr_Throw

Distinct from null-coalesce throw (#3462) and match throw arms (#3398).

php-src reference

  • Zend/zend_compile.czend_compile_throw as expression (zend_ast throw expr)
  • Zend/zend_execute.cZEND_THROW with unused result slot
  • PHP RFC: Throw Expression (PHP 8.0)

Repro (today)

<?php
function f(): int {
    $x = throw new LogicException('abort');
    return $x;
}
try {
    f();
} catch (LogicException $e) {
    echo $e->getMessage();
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'
php repro.php

Zend: prints abort (after #3371 / #195 register LogicException).
This compiler: compile fatal Unknown Expr Type Expr_Throw.

Requires #3371 / #195 for builtin exception classes before catch path is meaningful.

Scope (this repo)

Path Notes
vendor/ircmaxell/php-cfg Map Expr_Throw in assignment/return/comma contexts
lib/Compiler.php Lower throw-expr → TYPE_THROW + unreachable merge
lib/VM.php Propagate exception, no assign to LHS
Tests test/compliance/cases/language/throw_expression_assign.phpt

Done when

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter throw_expression_assign'

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