internal/core/eval: allow lists resulting from dependent expressions

Fixes #494

Change-Id: I4d9e92193156e39e8677ba091250a73881d141b0
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6962
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
diff --git a/internal/core/eval/eval.go b/internal/core/eval/eval.go
index 3298f7d..647d4b8 100644
--- a/internal/core/eval/eval.go
+++ b/internal/core/eval/eval.go
@@ -420,13 +420,18 @@
 func (n *nodeContext) postDisjunct() {
 	ctx := n.ctx
 
-	// Use maybeSetCache for cycle breaking
-	for n.maybeSetCache(); n.expandOne(); n.maybeSetCache() {
+	for {
+		// Use maybeSetCache for cycle breaking
+		for n.maybeSetCache(); n.expandOne(); n.maybeSetCache() {
+		}
+
+		if aList := n.addLists(ctx); aList != nil {
+			n.updateNodeType(adt.ListKind, aList)
+		} else {
+			break
+		}
 	}
 
-	if aList := n.addLists(ctx); aList != nil {
-		n.updateNodeType(adt.ListKind, aList)
-	}
 	if n.aStruct != nil {
 		n.updateNodeType(adt.StructKind, n.aStruct)
 	}
@@ -1600,6 +1605,11 @@
 	max := 0
 	var maxNode adt.Expr
 
+	if m, ok := n.node.Value.(*adt.ListMarker); ok {
+		isOpen = m.IsOpen
+		max = len(n.node.Arcs)
+	}
+
 	for _, l := range n.vLists {
 		oneOfTheLists = l
 
@@ -1747,10 +1757,21 @@
 
 	n.openList = isOpen
 
-	n.node.SetValue(c, adt.Partial, &adt.ListMarker{
-		Src:    ast.NewBinExpr(token.AND, sources...),
-		IsOpen: isOpen,
-	})
+	if m, ok := n.node.Value.(*adt.ListMarker); !ok {
+		n.node.SetValue(c, adt.Partial, &adt.ListMarker{
+			Src:    ast.NewBinExpr(token.AND, sources...),
+			IsOpen: isOpen,
+		})
+	} else {
+		if expr, _ := m.Src.(ast.Expr); expr != nil {
+			sources = append(sources, expr)
+		}
+		m.Src = ast.NewBinExpr(token.AND, sources...)
+		m.IsOpen = m.IsOpen && isOpen
+	}
+
+	n.lists = n.lists[:0]
+	n.vLists = n.vLists[:0]
 
 	return oneOfTheLists
 }