cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.14k stars 294 forks source link

cue/eval: list.Range fails inappropriately when called in definition #2175

Open rogpeppe opened 1 year ago

rogpeppe commented 1 year ago

What version of CUE are you using (cue version)?

$ cue version
cue version v0.0.0-20221207090135-ccdac14a7cb8

       -compiler gc
     CGO_ENABLED 1
          GOARCH amd64
            GOOS linux
         GOAMD64 v1
             vcs git
    vcs.revision ccdac14a7cb8b8eda5a101d4ededadb2c579db33
        vcs.time 2022-12-07T09:01:35Z
    vcs.modified false

Does this issue reproduce with the latest release?

What did you do?

exec cue export x.cue
cmp stdout expect.json

-- x.cue --
package c

import "list"

#T: {
    p: [...]
    r: list.Range(len(p)-1, 0, -1)
}

t: #T
t: p: [1, 2]

-- expect.json --
{
    "t": {
        "p": [
            1,
            2
        ],
        "r": [
            1
        ]
    }
}

What did you expect to see?

A passing testscript test.

What did you see instead?

> exec cue export x.cue
[stderr]
#T.r: error in call to list.Range: end must be less than start when step is negative:
    ./x.cue:7:5
[exit status 1]
FAIL: /tmp/testscript4234227699/x.txtar/script.txtar:1: unexpected command failure

The argument to list.Range is treated as final even though it isn't being emitted.

rogpeppe commented 1 year ago

A similar issue occurs just using indexing:

exec cue export x.cue

-- x.cue --
package c

#T: {
    p: [...]
    _q: [for x in p {x}]
    r: _q[0]
}

I'd expect that to be OK because the p field can later be instantiated with a non-zero-length list, but I see this error:

> exec cue export x.cue
[stderr]
#T.r: index out of range [0] with length 0:
    ./x.cue:6:8
[exit status 1]