Skip to content

array_shift inside a by-ref foreach diverges from php #41

Open

Description

@blacktrs

Summary

array_shift inside a by-ref foreach diverges from php.

Repro

<?php
$a = [1, 2, 3, 4];
foreach ($a as &$v) { array_shift($a); }
unset($v);
echo json_encode($a), "\n";

Expected (php 8.5)

[]

Actual (manticore 10affa68, macOS arm64)

[3,3]

Tracking

Known-bug repro: tests/aot/repro/wrong/foreach_byref_array_shift.php — bash tests/aot/xfail.sh -k foreach_byref_array_shift 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 Oct 5, 2026
  2. added
    needs-design
    area:runtime
    on Oct 13, 2026
  3. blacktrs commented on Oct 13, 2026

    @blacktrs
    ContributorAuthor

    Scope: needs a live-iterator registry. php adjusts the position of live foreach iterators when array_shift renumbers the array being iterated by reference. We have no registry of active by-ref foreach positions, so there is nothing to adjust. Fix: register by-ref foreach cursors per array and let array_shift/unshift/splice patch them. Found in batch E; skipped.

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:runtimebugneeds-designwrong-answer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions