Negative array index incorrectly returns first element
pd opened this issue · comments
Kyle Hargraves commented
In jrsonnet, any negative index of a non-empty array returns the first element. In go-jsonnet, it is an out-of-bounds error.
local xs = [1, 2];
// any negative index in jrsonnet returns xs[0]:
[xs[-1], xs[-2], xs[-42]]
jrsonnet:
[
1,
1,
1
]
go-jsonnet on the same file:
RUNTIME ERROR: Index -1 out of bounds, not within [0, 2)
bug.jsonnet:4:2-8 thunk from <$>
Array element 0
During manifestation
Negative index on an empty array does fail, but with the wrong index value:
[][-1]
jrsonnet:
// array out of bounds: 0 is not within [0,0)
go-jsonnet:
RUNTIME ERROR: Index -1 out of bounds, not within [0, 0)
bug.jsonnet:1:1-7 $
During evaluation
Yaroslav Bolyukin commented
Related to #117
Sorry, I should read properly first :D
Yes, this is a bug.
This is due to unchecked conversion from f64
(all jsonnet numbers are f64
) to usize
using num as usize
, where negative numbers are rounded to zero, in array indexing implementation.