Skip to content

fix: reject fractional indices and step in std.slice (#797)#884

Open
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/slice-strict-integer-797
Open

fix: reject fractional indices and step in std.slice (#797)#884
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/slice-strict-integer-797

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented May 31, 2026

Motivation

std.slice and the [a:b:c] slice operator silently truncated fractional index / end / step arguments instead of rejecting them:

std.slice([0, 1, 2], 0.9, 2.9, 1)   // was: [0, 1]
std.slice("abc", 0, 3, 1.5)         // was: "abc"

Official Jsonnet 0.22.0 rejects fractional array indices at manifestation (array index was not integer: 0.9). Per the maintainer decision in #797 ("go with the strict approach — if the function expects an int, reject anything else"), this applies strict integer semantics to both array and string slicing rather than truncating.

Modification

  • Util.slice now accepts raw Option[Double] and validates integrality at a single enforcement point, erroring on NaN / Infinity / non-whole values.
  • The std.slice builtin (SetModule) and the visitSlice operator (Evaluator) pass raw doubles through instead of pre-truncating to Int (which hid the fractional part before validation could see it).
  • Regression tests:
    • slice_strict_integer.jsonnet — integers, negatives, null, and whole doubles (2.0) still work, for both std.slice and the [a:b:c] operator.
    • error.slice_fractional_index.jsonnet — fractional index rejected.

Result

sjsonnet.Error: [std.slice] slice index must be an integer, got: 0.9
sjsonnet.Error: slice index must be an integer, got: 0.5    # operator path

Full JVM (Scala 3) test suite passes; formatting verified with checkFormat.

Addresses #797.

Motivation:
std.slice and the [a:b:c] slice operator silently truncated fractional
index/end/step arguments (e.g. std.slice([0,1,2], 0.9, 2.9, 1) returned
[0, 1]). Official Jsonnet 0.22.0 rejects fractional array indices at
manifestation; the maintainer decision on databricks#797 is to apply strict integer
semantics to both array and string slicing rather than truncate.

Modification:
- Util.slice now accepts raw Option[Double] and validates integrality at a
  single enforcement point, erroring on NaN / Infinity / non-whole values.
- The std.slice builtin and the visitSlice operator pass raw doubles
  through instead of pre-truncating to Int.
- Add regression tests: slice_strict_integer (integers, negatives, null,
  and whole doubles like 2.0 still work) and error.slice_fractional_index
  (fractional index rejected with a clear message).

Result:
Fractional slice arguments now fail with
"slice <index|end|step> must be an integer, got: <n>". All existing slice
tests and the full JVM (Scala 3) test suite pass.

References: databricks#797
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.

1 participant