Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reduce/foreach state variable should not be reset each iteration #3205

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ block gen_reduce(block source, block matcher, block init, block body) {
block loop = BLOCK(gen_op_simple(DUPN),
source,
bind_alternation_matchers(matcher,
BLOCK(gen_op_bound(LOADVN, res_var),
BLOCK(gen_op_bound(LOADV, res_var),
body,
gen_op_bound(STOREV, res_var))),
gen_op_simple(BACKTRACK));
Expand All @@ -854,7 +854,7 @@ block gen_foreach(block source, block matcher, block init, block update, block e
// in the body to see
bind_alternation_matchers(matcher,
// load the loop state variable
BLOCK(gen_op_bound(LOADVN, state_var),
BLOCK(gen_op_bound(LOADV, state_var),
// generate updated state
update,
// save the updated state for value extraction
Expand Down
8 changes: 8 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ null
[{"a":1}, {"b":2}, {"a":3, "b":4}]
[-1, -1, -4]

[foreach range(5) as $x (0; .+$x | select($x!=2); [$x,.])]
null
[[0,0],[1,1],[3,4],[4,8]]

[limit(3; .[])]
[11,22,33,44,55,66,77,88,99]
[11,22,33]
Expand Down Expand Up @@ -825,6 +829,10 @@ reduce . as $n (.; .)
null
null

reduce range(5) as $x (0; .+$x | select($x!=2))
null
8

# Destructuring
. as {$a, b: [$c, {$d}]} | [$a, $c, $d]
{"a":1, "b":[2,{"d":3}]}
Expand Down
Loading