Skip to content

Commit e4b9b6a

Browse files
committed
fix crash on assignment operators with multiple values
1 parent afc4c6c commit e4b9b6a

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

cli/test.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4517,13 +4517,15 @@
45174517
expected: |
45184518
{"bar":42,"foo":42}
45194519
4520-
- name: assignment operator query with multiple paths
4520+
- name: assignment operator query with multiple paths and values
45214521
args:
45224522
- -c
4523-
- '(.foo,.bar,.baz) = .bar + 1'
4524-
input: '{"bar":42}'
4523+
- '(.foo,.bar,.baz) = .bar[]'
4524+
input: '{"bar":[0,1,2]}'
45254525
expected: |
4526-
{"bar":43,"baz":43,"foo":43}
4526+
{"bar":0,"baz":0,"foo":0}
4527+
{"bar":1,"baz":1,"foo":1}
4528+
{"bar":2,"baz":2,"foo":2}
45274529
45284530
- name: assignment operator array index negative error
45294531
args:

compiler.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ func (c *compiler) compileFunc(e *Func) error {
10141014
// Appends the compiled code for the assignment operator (`=`) to maximize
10151015
// performance. Originally the operator was implemented as follows.
10161016
//
1017-
// def _assign(p; $x): reduce path(p) as $p (.; setpath($p; $x));
1017+
// def _assign(p; $x): reduce path(p) as $q (.; setpath($q; $x));
10181018
//
10191019
// To overcome the difficulty of reducing allocations on `setpath`, we use the
10201020
// `allocator` type and track the allocated addresses during the reduction.
@@ -1023,8 +1023,9 @@ func (c *compiler) compileAssign() {
10231023
scope := c.newScope()
10241024
v, p := [2]int{scope.id, 0}, [2]int{scope.id, 1}
10251025
x, a := [2]int{scope.id, 2}, [2]int{scope.id, 3}
1026+
q := [2]int{scope.id, 4} // Cannot reuse p due to backtracking in x.
10261027
c.appends(
1027-
&code{op: opscope, v: [3]int{scope.id, 4, 2}},
1028+
&code{op: opscope, v: [3]int{scope.id, 5, 2}},
10281029
&code{op: opstore, v: v}, // def _assign(p; $x):
10291030
&code{op: opstore, v: p},
10301031
&code{op: opstore, v: x},
@@ -1044,10 +1045,10 @@ func (c *compiler) compileAssign() {
10441045
&code{op: opcallpc},
10451046
&code{op: opload, v: v},
10461047
&code{op: oppathend},
1047-
&code{op: opstore, v: p}, // as $p (.;
1048-
&code{op: opload, v: a}, // setpath($p; $x)
1048+
&code{op: opstore, v: q}, // as $q (.;
1049+
&code{op: opload, v: a}, // setpath($q; $x)
10491050
&code{op: opload, v: x},
1050-
&code{op: opload, v: p},
1051+
&code{op: opload, v: q},
10511052
&code{op: opload, v: v},
10521053
&code{op: opcall, v: [3]interface{}{funcSetpathWithAllocator, 3, "_setpath"}},
10531054
&code{op: opstore, v: v},

0 commit comments

Comments
 (0)