cue icon indicating copy to clipboard operation
cue copied to clipboard

len(X) doesn't see the full struct/list's value

Open vikstrous2 opened this issue 2 years ago • 2 comments

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

$ cue version
0.4.3

Does this issue reproduce with the latest release?

yes

What did you do?

_ports_map: {}
if len(_ports_map) > 0 {
	port: _ports_map.a.port
}
if true {
    _ports_map: a: {port: "80"}
}

What did you expect to see?

port: "80"

What did you see instead?

no output

Interestingly, if we change the code to reference "a" rather than just "_ports_map" in general, we get what we expect:

_ports_map: {}
if _ports_map.a.port == "80" {
	port: _ports_map.a.port
}
if true {
    _ports_map: a: {port: "80"}
}

Clearly len(X) doesn't force the entire struct's contents to be evaluated when X is a struct (or list). This also doesn't work:

_ports_list: [...]
if len(_ports_list) > 0 {
	port: _ports_list[0].port
}
if true {
    _ports_list: ["80"]
}

Surprisingly, if I change the if condition from len(X) to just true, there's no issue, so it's just the len function that seems problematic. It seems to run too early. This works:

_ports_map: {}
if true {
	ports: _ports_map
}
if true {
    _ports_map: {port: "80"}
}

vikstrous2 avatar Jun 08 '22 15:06 vikstrous2

I thought this might have been fixed in https://review.gerrithub.io/c/cue-lang/cue/+/537831/4 but this example actually demonstrates a bug with that code (it panics), so good work finding this useful test case, thanks!

rogpeppe avatar Jun 24 '22 17:06 rogpeppe

Marked as "cycle" so this doesn't get missed.

myitcv avatar Jun 27 '22 08:06 myitcv