Skip to content
Myr

fix: `foreach` should not break init backtracking with `DUPN` (#3266)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Simplifies the compilation of `foreach` to avoid the problem in #3227 by removing unnecessary `FORK ... BACKTRACK`. Does not address `reduce`, which is less trivial.

authored by Nicole Wren · committed by GitHub · parent f65d088 · Browse files · 2 files+27 added−39 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+27 added−39 removed

  • tests:+8 added−0 removed
  • other:+19 added−39 removed

2 files+27 added−39 removed

modified:src/compile.c+19 added-39 removed
841 unmodified lines
842 gen_op_bound(LOADVN, res_var));843}844845block gen_foreach(block source, block matcher, block init, block update, block extract) {846 block output = gen_op_targetlater(JUMP);846 block state_var = gen_op_var_fresh(STOREV, "foreach");848 block loop = BLOCK(gen_op_simple(DUPN),849 // get a value from the source expression:850 source,851 // destructure the value into variable(s) for all the code852 // in the body to see853 bind_alternation_matchers(matcher,854 // load the loop state variable855 BLOCK(gen_op_bound(LOADV, state_var),856 // generate updated state857 update,858 // save the updated state for value extraction859 gen_op_simple(DUP),860 // save new state861 gen_op_bound(STOREV, state_var),862 // extract an output...863 extract,864 // ...and output it by jumping865 // past the BACKTRACK that comes866 // right after the loop body,867 // which in turn is there868 // because...869 //870 // (Incidentally, extract can also871 // backtrack, e.g., if it calls872 // empty, in which case we don't873 // get here.)874 output)));875 block foreach = BLOCK(gen_op_simple(DUP),876 init,877 state_var,878 gen_op_target(FORK, loop),879 loop,880 // ...at this point `foreach`'s original input881 // will be on top of the stack, and we don't882 // want to output it, so we backtrack.883 gen_op_simple(BACKTRACK));884 inst_set_target(output, foreach); // make that JUMP go bast the BACKTRACK at the end of the loop885 return foreach;847 return BLOCK(gen_op_simple(DUP),848 init,849 state_var,850 gen_op_simple(DUP),851 // get a value from the source expression:852 source,853 // destructure the value into variable(s) for all the code854 // in the body to see855 bind_alternation_matchers(matcher,856 // load the loop state variable857 BLOCK(gen_op_bound(LOADV, state_var),858 // generate updated state859 update,860 // save the updated state for value extraction861 gen_op_simple(DUP),862 // save new state863 gen_op_bound(STOREV, state_var),864 // extract an output...865 extract)));866}867868block gen_definedor(block a, block b) {869 // var found := false
509 unmodified lines
modified:tests/jq.test+8 added
2317 unmodified lines
23182319try ["OK", setpath([[1]]; 1)] catch ["KO", .]2320[]2321["KO","Cannot update field at array index of array"]23222323# regression test for #32272324foreach .[] as $x (0, 1; . + $x)2325[1, 2]23261232732328223294