Skip to content

Commit 1a2105e

Browse files
committed
internal/core/eval: fix hang in cyclic in comprehension evaluation
Fixes #486 Change-Id: I8a7e42c3eaff476c3bae23b40f7cb0ae5658975c Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7502 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 635fbdd commit 1a2105e

File tree

2 files changed

+160
-3
lines changed

2 files changed

+160
-3
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
-- in.cue --
2+
// Allow lookup in partially evaluated struct as long as the end result is
3+
// concrete.
4+
A: {
5+
a: {
6+
parent: ""
7+
children: [ for k, v in A if v.parent == k { k } ]
8+
}
9+
b: {
10+
parent: "a"
11+
children: [ for k, v in A if v.parent == k { k } ]
12+
}
13+
}
14+
15+
// TODO(errors): this should result in an incomplete error.
16+
// A simplified control flow should help here.
17+
B: {
18+
a: {
19+
parent: ""
20+
children: [ for k, v in B for _, w in v.children { k } ]
21+
}
22+
}
23+
24+
// Issue #486
25+
Issue486: {
26+
A: {
27+
a: {
28+
parent: ""
29+
children: [...string]
30+
}
31+
b: {
32+
parent: "a"
33+
children: [...string]
34+
}
35+
c: {
36+
parent: "b"
37+
children: [...string]
38+
}
39+
}
40+
41+
A: [Name=string]: {
42+
children: [
43+
for k, v in A
44+
if v.parent == Name {
45+
k
46+
}
47+
]
48+
}
49+
}
50+
-- out/eval --
51+
(struct){
52+
A: (struct){
53+
a: (struct){
54+
parent: (string){ "" }
55+
children: (#list){
56+
}
57+
}
58+
b: (struct){
59+
parent: (string){ "a" }
60+
children: (#list){
61+
}
62+
}
63+
}
64+
B: (struct){
65+
a: (struct){
66+
parent: (string){ "" }
67+
children: (#list){
68+
}
69+
}
70+
}
71+
Issue486: (struct){
72+
A: (struct){
73+
a: (struct){
74+
parent: (string){ "" }
75+
children: (#list){
76+
0: (string){ "b" }
77+
}
78+
}
79+
b: (struct){
80+
parent: (string){ "a" }
81+
children: (#list){
82+
0: (string){ "c" }
83+
}
84+
}
85+
c: (struct){
86+
parent: (string){ "b" }
87+
children: (#list){
88+
}
89+
}
90+
}
91+
}
92+
}
93+
-- out/compile --
94+
--- in.cue
95+
{
96+
A: {
97+
a: {
98+
parent: ""
99+
children: [
100+
for k, v in 〈2;A〉 if (〈0;v〉.parent == 〈0;k〉) {
101+
〈1;k〉
102+
},
103+
]
104+
}
105+
b: {
106+
parent: "a"
107+
children: [
108+
for k, v in 〈2;A〉 if (〈0;v〉.parent == 〈0;k〉) {
109+
〈1;k〉
110+
},
111+
]
112+
}
113+
}
114+
B: {
115+
a: {
116+
parent: ""
117+
children: [
118+
for k, v in 〈2;B〉 for _, w in 〈0;v〉.children {
119+
〈2;k〉
120+
},
121+
]
122+
}
123+
}
124+
Issue486: {
125+
A: {
126+
a: {
127+
parent: ""
128+
children: [
129+
...string,
130+
]
131+
}
132+
b: {
133+
parent: "a"
134+
children: [
135+
...string,
136+
]
137+
}
138+
c: {
139+
parent: "b"
140+
children: [
141+
...string,
142+
]
143+
}
144+
}
145+
A: {
146+
[string]: {
147+
children: [
148+
for k, v in 〈2;A〉 if (〈0;v〉.parent == 〈2;-〉) {
149+
〈1;k〉
150+
},
151+
]
152+
}
153+
}
154+
}
155+
}

internal/core/adt/expr.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ func (x *SelectorExpr) Source() ast.Node {
582582
}
583583

584584
func (x *SelectorExpr) resolve(c *OpContext) *Vertex {
585-
n := c.node(x.X, Partial)
585+
n := c.node(x.X, EvaluatingArcs)
586586
return c.lookup(n, x.Src.Sel.Pos(), x.Sel)
587587
}
588588

@@ -605,7 +605,7 @@ func (x *IndexExpr) Source() ast.Node {
605605

606606
func (x *IndexExpr) resolve(ctx *OpContext) *Vertex {
607607
// TODO: support byte index.
608-
n := ctx.node(x.X, Partial)
608+
n := ctx.node(x.X, EvaluatingArcs)
609609
i := ctx.value(x.Index)
610610
f := ctx.Label(i)
611611
return ctx.lookup(n, x.Src.Index.Pos(), f)
@@ -1171,8 +1171,10 @@ func (x *ForClause) Source() ast.Node {
11711171
}
11721172

11731173
func (x *ForClause) yield(c *OpContext, f YieldFunc) {
1174-
n := c.node(x.Src, Finalized)
1174+
n := c.node(x.Src, EvaluatingArcs)
11751175
for _, a := range n.Arcs {
1176+
c.Unify(c, a, Partial)
1177+
11761178
if !a.Label.IsRegular() {
11771179
continue
11781180
}

0 commit comments

Comments
 (0)