Skip to content

Carry a block's next value through flat_map#1544

Merged
matz merged 1 commit into
matz:masterfrom
ryanseys:rs-next-value-flatmap
Jun 24, 2026
Merged

Carry a block's next value through flat_map#1544
matz merged 1 commit into
matz:masterfrom
ryanseys:rs-next-value-flatmap

Conversation

@ryanseys

Copy link
Copy Markdown
Contributor

Summary

flat_map evaluated the block's array as a plain tail expression, so a next [..] inside the block was lowered to a bare continue and lost.

[1, 2, 3].flat_map { |x| next [0] if x == 2; [x, x] }
# was: [1, 1, 3, 3]      (the `next [0]` dropped)
# now: [1, 1, 0, 3, 3]   (matches CRuby)

This mirrors the array map/select/reject paths, which already route the block value through the next-aware helper.

Implementation

Fill the per-iteration array temp through the shared emit_block_value_into helper (initialized to its type default) before splatting its elements into the result — so a next [..] flattens its array exactly like the tail expression does. The forward declaration of emit_block_value_into moves to the top of the file so emit_flat_map_expr (above the definition) can see it.

Testing

test/next_value_flat_map.rb covers next [..] in flat_map over int and string element arrays, plus a plain (no-next) flat_map to pin the unchanged path. Expected regenerated from ruby. Verified under ASAN+UBSAN with SPINEL_GC_STRESS=1, matching ruby.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the code generator in src/codegen_fold.c to correctly handle next statements with array values inside flat_map blocks. Specifically, it introduces a forward declaration for emit_block_value_into and uses it within emit_flat_map_expr to collect the block's next-aware value into a temporary array variable, ensuring that next [..] flattens its array like the tail expression. It also adds corresponding test cases and expected outputs. There are no review comments, so I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@ryanseys ryanseys marked this pull request as ready for review June 23, 2026 21:23
flat_map evaluated the block's array as a plain tail expression, so a
`next [..]` inside the block was lowered to a bare `continue` and lost.

Fill the per-iteration array temp through the shared `emit_block_value_into`
helper (initialized to its type default) before splatting its elements into the
result, so a `next [..]` flattens its array just like the tail does.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@matz matz force-pushed the rs-next-value-flatmap branch from d37d318 to 334b052 Compare June 24, 2026 01:20
@matz matz merged commit 81894ea into matz:master Jun 24, 2026
3 checks passed
@ryanseys ryanseys deleted the rs-next-value-flatmap branch June 24, 2026 01:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants