Skip to content

Language: __halt_compiler() — stop parsing and preserve trailing bytes (Zend zend_compile.c parity) #3479

Description

@PurHur

Category

language / compiler

Problem

PHP supports __halt_compiler() to terminate compilation and leave trailing bytes in the source file (used by PHAR stubs and some codegen tools). php-parser already parses Stmt\HaltCompiler, but this compiler has no lowering — function_exists('__halt_compiler') is false and scripts using the construct fail at compile time.

Verified May 2026: trailing bytes after halt are parsed as PHP, causing syntax errors on garbage data.

php-src reference

Repro (failure today)

<?php
echo "before\n";
__halt_compiler();
?>
TRAILING_BYTES_SHOULD_NOT_BE_PARSED
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'
# parse/compile error on trailing bytes OR unknown statement (wrong)

php repro.php
# before
# (trailing bytes ignored)
Runtime Expected
Zend Compiles; runs echo "before"; ignores trailing bytes
This compiler Fails to compile or parses trailing garbage

Scope (this repo)

  • vendor/ircmaxell/php-cfg / patches — lower Stmt\HaltCompiler to compile-stop marker
  • lib/Compiler.php — ignore/reject statements after halt; optionally store remaining bytes
  • ext/standard/ — register __halt_compiler() builtin (runtime call may fatal per Zend)
  • JIT/AOT: compile-time only — self-host inventory unlikely to hit this in lib/

Done when

  • Repro prints before under php bin/vm.php; no parse error on trailing TRAILING_BYTES…
  • function_exists('__halt_compiler') returns true
  • Direct runtime call __halt_compiler() behavior matches Zend (compile error or fatal as appropriate)
  • Compliance PHPT: test/compliance/cases/language/halt_compiler.phpt
  • Optional: expose remaining bytes for PHAR tooling (Stdlib: ext/phar — Phar archive API (ext/phar parity) #3436) — not required for initial claim

Verification

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

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