Skip to content

A literal passed to a by-reference parameter through an erased callable corrupts the closure's return value instead of throwing "could not be passed by reference" #597

Open

Description

@blacktrs

Summary

A literal passed to a by-reference parameter through an erased callable corrupts the closure's return value instead of throwing "could not be passed by reference".

Repro

<?php
function g(mixed $f): void {
    try { $r = $f(strtoupper('a')); echo "ret ", var_export($r, true), "\n"; } catch (\Throwable $e) { echo get_class($e), "\n"; }
    try { $r = $f(1); echo "ret ", var_export($r, true), "\n"; } catch (\Throwable $e) { echo get_class($e), ': ', \preg_replace('/\{closure[^}]*\}/', '{closure}', $e->getMessage()), "\n"; }
}
g(function (&$x) { $x = 5; return 'closure'; });

Expected (php)

ret 'closure'
Error: {closure}(): Argument #1 ($x) could not be passed by reference

Actual (manticore 9ccbe2ba, Darwin arm64)

ret ''losure'
ret ''losure'

Tracking

Known-bug repro: tests/aot/repro/wrong/erased_invoke_literal_to_byref_corrupts.php — bash tests/aot/xfail.sh -k erased_invoke_literal_to_byref_corrupts reports XFAIL while this is open and XPASS once it is fixed; then move the repro into tests/aot/cases/ and close this issue.

Activity

  1. added
    bug
    wrong-answer
    on Nov 8, 2026
  2. blacktrs commented on Nov 12, 2026

    @blacktrs
    ContributorAuthor

    Root cause found during the #655 sweep (not fixed, needs design): (1) the outlined erased-invoke helper (__mir_erinv_*) hands a by-value argument word it only BORROWS to a by-ref closure slot; the closure's write drops it, so the caller's temporary is released twice — that is the corruption. (2) php's "Argument #N ($x) could not be passed by reference" needs which arguments are literals (the helper only sees values, modes r/v) and the parameter name (absent from the closure prefix metadata). Fixing (1) alone does not flip the repro.

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

    bugwrong-answer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions