Skip to content

fix(core): foreach value ownership#418

Draft
Guikingone wants to merge 1 commit into
illegalstudio:mainfrom
Guikingone:fix/405-foreach-value-ownership
Draft

fix(core): foreach value ownership#418
Guikingone wants to merge 1 commit into
illegalstudio:mainfrom
Guikingone:fix/405-foreach-value-ownership

Conversation

@Guikingone

Copy link
Copy Markdown
Collaborator

See #405

@Guikingone
Guikingone force-pushed the fix/405-foreach-value-ownership branch from 0c18130 to 4ea3424 Compare July 2, 2026 16:16
Fixes illegalstudio#405: appending a foreach value into a new array and returning it
corrupted the array because the foreach value was always typed as Mixed,
forcing array_to_mixed conversion and a type mismatch with the caller's
expected element layout.

Three changes:

1. foreach_value_type now returns the concrete element type (Str, Int,
   Bool, Float, Callable) for typed arrays instead of always Mixed.
   Non-scalar element types (Iterable, Object, etc.) still widen to Mixed.

2. lower_iter_current_value persists the borrowed string slot from an
   indexed array into an owned heap copy when the result type is Str,
   so the subsequent release_if_owned does not free the source array's
   element.  For DynamicIterable/Hash paths that always produce a Mixed
   box, a new unbox_dynamic_iter_current_value_if_needed helper unboxes
   the Mixed cell into the concrete result type.

3. initialize_foreach_concrete_local_if_needed initializes concrete-typed
   foreach variables (Str, Int, Bool, Float) to a safe empty value before
   the first iteration, preventing the first release from reading
   uninitialized stack memory.

Regression tests cover foreach value append with function return,
literal array source, inline append, iterating the result, and integer
arrays.
@Guikingone
Guikingone force-pushed the fix/405-foreach-value-ownership branch from 4ea3424 to edac4e6 Compare July 4, 2026 17:29
@github-actions github-actions Bot added area:eir Touches EIR definitions, lowering, validation, or passes. size:s Small pull request. type:fix Corrects broken or incompatible behavior. labels Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:eir Touches EIR definitions, lowering, validation, or passes. size:s Small pull request. type:fix Corrects broken or incompatible behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant