Skip to content

Nested write into an Array(Mixed) element ($a[$i][$j] = ...) is silently lost: the parent keeps the stale inner array #529

Description

@mirchaemanuel

Summary

A nested write into an Array(Mixed) element — $a[$i][$j] = ... — is silently lost: the program compiles and runs, but the parent array keeps the stale inner array (a correctness miscompile), and the mutated copy leaks.

This is the only nested-write shape that compiles at all: on a homogeneous typed nested array the same statement is a compile-time error (error[4:1]: Nested array assignment requires a Mixed or ArrayAccess target), so the Mixed path below is the supported target shape — and it miscompiles.

Environment

  • elephc main e63d5d337 (v0.26.0-158), macOS arm64 (Apple Silicon); PHP 8.4.23 as behavioral reference.

Repro

<?php
// Heterogeneous elements force Array(Mixed), the supported nested-write target shape.
$a = [['x', 'y0'], ['x', 'y1'], ['x', 'y2'], 7];
$a[2][1] = 'patched';
echo $a[2][1] . "\n";
echo $a[1][1] . "\n";
elephc --heap-debug write_read.php && ./write_read

Observed

y2
y1
HEAP DEBUG: allocs=24 frees=10 live_blocks=14 live_bytes=744 peak_live_bytes=1144
HEAP DEBUG: leak summary: live_blocks=14 live_bytes=744

The write is silently lost (y2 instead of patched), plus 14 leaked blocks.

Expected (PHP 8.4.23, verbatim)

patched
y1

Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions