cue icon indicating copy to clipboard operation
cue copied to clipboard

evaluator: reference to required field incorrectly reported as an optional field

Open myitcv opened this issue 1 year ago • 1 comments

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

$ cue version
cue version v0.8.0-alpha.5

go version go1.22.1
      -buildmode exe
       -compiler gc
  DefaultGODEBUG httplaxcontentlength=1,httpmuxgo121=1,tls10server=1,tlsrsakex=1,tlsunsafeekm=1
     CGO_ENABLED 1
          GOARCH arm64
            GOOS linux
             vcs git
    vcs.revision f6238e57d448aa220f8d446d85c9a0126932ebe4
        vcs.time 2024-03-12T16:50:41Z
    vcs.modified false

Does this issue reproduce with the latest release?

Yes

What did you do?

! exec cue export x.cue
cmp stderr stderr.golden

-- x.cue --
min!: number
max!: number & >min
max:  10

-- stderr.golden --
min: field is required but not present:
    ./x.cue:1:1

What did you expect to see?

A passing test.

What did you see instead?

> ! exec cue export x.cue
[stderr]
min: field is required but not present:
    ./x.cue:1:1
max: cannot reference optional field: min:
    ./x.cue:2:17
[exit status 1]
> cmp stderr stderr.golden
--- stderr
+++ stderr.golden
@@ -1,4 +1,2 @@
 min: field is required but not present:
     ./x.cue:1:1
-max: cannot reference optional field: min:
-    ./x.cue:2:17

FAIL: /tmp/testscript1308464767/repro.txtar/script.txtar:2: stderr and stderr.golden differ

i.e. a get an error about the required field not being present, as expected. But an unexpected error about max's reference to min being invalid because min is optional. It is not optional: it is required. Indeed, min has not been specified, and I know about that because of the aforementioned error.

myitcv avatar Mar 13 '24 05:03 myitcv

This causes quite unintuitive behavior that imo is a bug:

spec!: {
  sub: "world"
}

hello: *spec.sub | "nothing"

This evaluates to hello: nothing as marking the field as required effectively makes it inaccessible when referenced by hello. By removing it being required I instead get hello: world. https://cuelang.org/play/?id=PTL883WuW9L#w=function&i=cue&f=eval&o=cue

rd-niklas-gergs avatar Nov 26 '25 11:11 rd-niklas-gergs