internal/core/adt: remove all closedness IDs in ToData

The current algorithm missed some, causing closedness
to be misalligned down the line.

Fixes #526

Change-Id: I177a8f65ae5e8322328baf4224b69ce52c6a5d1e
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7142
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
diff --git a/cmd/cue/cmd/testdata/script/issue526.txt b/cmd/cue/cmd/testdata/script/issue526.txt
new file mode 100644
index 0000000..a226171
--- /dev/null
+++ b/cmd/cue/cmd/testdata/script/issue526.txt
@@ -0,0 +1,12 @@
+cue cmd gengithub
+
+-- x.cue --
+package x
+
+test:      #Workflow
+#Workflow: 1 | [_]
+
+-- x_tool.cue --
+package x
+
+command: gengithub: {}
diff --git a/internal/core/adt/composite.go b/internal/core/adt/composite.go
index bb63010..5dfaa12 100644
--- a/internal/core/adt/composite.go
+++ b/internal/core/adt/composite.go
@@ -279,17 +279,50 @@
 		arcs[i] = a.ToDataAll()
 	}
 	w := *v
+
+	w.Value = toDataAll(w.Value)
 	w.Arcs = arcs
 	w.isData = true
 	w.Conjuncts = make([]Conjunct, len(v.Conjuncts))
 	copy(w.Conjuncts, v.Conjuncts)
-	for i := range w.Conjuncts {
+	for i, c := range w.Conjuncts {
 		w.Conjuncts[i].CloseID = 0
+		if v, _ := c.x.(Value); v != nil {
+			w.Conjuncts[i].x = toDataAll(v)
+		}
 	}
 	w.Closed = nil
 	return &w
 }
 
+func toDataAll(v Value) Value {
+	switch x := v.(type) {
+	default:
+		return x
+
+	case *Vertex:
+		return x.ToDataAll()
+
+	// The following cases are always erroneous, but we handle them anyway
+	// to avoid issues with the closedness algorithm down the line.
+	case *Disjunction:
+		d := *x
+		d.Values = make([]*Vertex, len(x.Values))
+		for i, v := range x.Values {
+			d.Values[i] = v.ToDataAll()
+		}
+		return &d
+
+	case *Conjunction:
+		c := *x
+		c.Values = make([]Value, len(x.Values))
+		for i, v := range x.Values {
+			c.Values[i] = toDataAll(v)
+		}
+		return &c
+	}
+}
+
 // func (v *Vertex) IsEvaluating() bool {
 // 	return v.Value == cycle
 // }
@@ -314,9 +347,6 @@
 // func (v *Vertex) Evaluate()
 
 func (v *Vertex) Finalize(c *OpContext) {
-	if c == nil {
-		fmt.Println("WOT?")
-	}
 	c.Unify(c, v, Finalized)
 }