Skip to content

Compiler: Return type declarations on user functions (VM + JIT) #205

@PurHur

Description

@PurHur

Problem

Modern PHP web code uses declared return types on functions and methods:

function slug(string $s): string { return strtolower($s); }
private function renderHome(): void { /* … */ }

#55 tracks emitting non-void values in JIT; this issue adds reading and enforcing : return types from php-cfg in the compiler (VM checks, JIT typed returns, AOT metadata).

examples/003-MiniWebApp/src/Router.php already uses : void on dispatch() and private render* methods — they will fail type checks once #145 methods compile unless returns are wired here.

Goal

  • VM: coerce/check return value at TYPE_RETURN against declared type (respect Language: declare(strict_types=1) enforcement in VM #156 strict_types when enabled)
  • JIT: LLVM return value uses declared scalar types where possible
  • AOT: same metadata as JIT
  • Explicit (): void — reject non-null returns

Implementation hints

Area Files Notes
Compiler lib/Compiler.php compileFunction, compileClassBody Read Func::returnType / method return type from PHPCfg
VM lib/VM.php TYPE_RETURN TypeError on mismatch when strict_types=1
JIT lib/JIT.php return lowering Pair with #55 — void vs value returns
Lint lib/Lint/UnsupportedRegistry.php Until done, map unsupported return kinds
Tests test/compliance/cases/return_type_*.phpt valid string return, void, strict failure

MiniWebApp

After #145 lands, Router::dispatch(): void must not require stripping return types from the reference app.

Acceptance criteria

docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \
  php bin/vm.php -r 'declare(strict_types=1); function f(): int { return "x"; } f();'

Throws TypeError in VM (message parity optional v1).

docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \
  php bin/vm.php -r 'function g(): string { return "ok"; } echo g();'

Prints ok.

Verification (local / Docker only)

./script/ci-local.sh --filter return_type

Dependencies

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions