Revert "cue: fix lookup for cue API"

This reverts commit 8bd24267d581890c46dc1c584dd1d08f88dd294b.

Reason for revert: results in breakage. Need to investigate.

Change-Id: Ice24e302f7d3f6a162ce6d00f61a39df14062a34
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4221
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/instance.go b/cue/instance.go
index 60ceffb..9b7955e 100644
--- a/cue/instance.go
+++ b/cue/instance.go
@@ -257,7 +257,7 @@
 		i.setError(val.toErr(err))
 		return i
 	}
-	i.scope = v.obj
+	i.scope = v.n
 
 	if err := resolveFiles(idx, p); err != nil {
 		i.setError(err)
diff --git a/cue/types.go b/cue/types.go
index 8f7c30b..e62d628 100644
--- a/cue/types.go
+++ b/cue/types.go
@@ -135,21 +135,20 @@
 type structValue struct {
 	ctx  *context
 	path *valueData
-	obj  *structLit
-	arcs arcs
+	n    *structLit
 }
 
 // Len reports the number of fields in this struct.
 func (o *structValue) Len() int {
-	if o.obj == nil {
+	if o.n == nil {
 		return 0
 	}
-	return len(o.arcs)
+	return len(o.n.arcs)
 }
 
 // At reports the key and value of the ith field, i < o.Len().
 func (o *structValue) At(i int) (key string, v Value) {
-	a := o.arcs[i]
+	a := o.n.arcs[i]
 	v = newChildValue(o, i)
 	return o.ctx.labelStr(a.feature), v
 }
@@ -161,17 +160,19 @@
 	i := 0
 	len := o.Len()
 	for ; i < len; i++ {
-		if o.arcs[i].feature == f {
+		if o.n.arcs[i].feature == f {
 			break
 		}
 	}
 	if i == len {
 		// TODO: better message.
 		ctx := o.ctx
-		x := ctx.mkErr(o.obj, codeNotExist, "value %q not found", key)
+		x := ctx.mkErr(o.n, codeNotExist, "value %q not found", key)
 		v := x.evalPartial(ctx)
 		return Value{ctx.index, &valueData{o.path, 0, arc{cache: v, v: x}}}
 	}
+	// v, _ := o.n.lookup(o.ctx, f)
+	// v = o.ctx.manifest(v)
 	return newChildValue(o, i)
 }
 
@@ -605,26 +606,8 @@
 }
 
 func newChildValue(obj *structValue, i int) Value {
-	a := obj.arcs[i]
-	for j, b := range obj.obj.arcs {
-		if b.feature == a.feature {
-			a = obj.obj.iterAt(obj.ctx, j)
-			// TODO: adding more technical debt here. The evaluator should be
-			// rewritten.
-			x := obj.obj
-			ctx := obj.ctx
-			if x.optionals != nil {
-				name := ctx.labelStr(x.arcs[i].feature)
-				arg := &stringLit{x.baseValue, name, nil}
-
-				val, _ := x.optionals.constraint(ctx, arg)
-				if val != nil {
-					a.v = mkBin(ctx, x.Pos(), opUnify, a.v, val)
-				}
-			}
-			break
-		}
-	}
+	a := obj.n.arcs[i]
+	a.cache = obj.n.at(obj.ctx, i)
 
 	return Value{obj.ctx.index, &valueData{obj.path, uint32(i), a}}
 }
@@ -657,8 +640,7 @@
 	if v.path == nil {
 		return v
 	}
-	fmt.Println(v)
-	return remakeValue(v, v.path.v)
+	return remakeValue(v, v.path.v.evalPartial(v.ctx()))
 }
 
 // Default reports the default value and whether it existed. It returns the
@@ -1166,9 +1148,17 @@
 			k++
 		}
 		arcs = arcs[:k]
-		return structValue{ctx, v.path, obj, arcs}, nil
+		obj = &structLit{
+			obj.baseValue,   // baseValue
+			obj.emit,        // emit
+			obj.optionals,   // template
+			obj.closeStatus, // closeStatus
+			nil,             // comprehensions
+			arcs,            // arcs
+			nil,             // attributes
+		}
 	}
-	return structValue{ctx, v.path, obj, obj.arcs}, nil
+	return structValue{ctx, v.path, obj}, nil
 }
 
 // Struct returns the underlying struct of a value or an error if the value
@@ -1216,19 +1206,6 @@
 	a := s.s.arcs[i]
 	a.cache = s.s.at(ctx, i)
 
-	// TODO: adding more technical debt here. The evaluator should be
-	// rewritten.
-	x := s.s
-	if x.optionals != nil {
-		name := ctx.labelStr(x.arcs[i].feature)
-		arg := &stringLit{x.baseValue, name, nil}
-
-		val, _ := x.optionals.constraint(ctx, arg)
-		if val != nil {
-			a.v = mkBin(ctx, x.Pos(), opUnify, a.v, val)
-		}
-	}
-
 	v := Value{ctx.index, &valueData{s.v.path, uint32(i), a}}
 	str := ctx.labelStr(a.feature)
 	return FieldInfo{str, i, v, a.definition, a.optional, a.feature&hidden != 0}
@@ -1260,16 +1237,7 @@
 	if err != nil {
 		return Iterator{ctx: ctx}, v.toErr(err)
 	}
-	n := &structLit{
-		obj.obj.baseValue,   // baseValue
-		obj.obj.emit,        // emit
-		obj.obj.optionals,   // template
-		obj.obj.closeStatus, // closeStatus
-		nil,                 // comprehensions
-		obj.arcs,            // arcs
-		nil,                 // attributes
-	}
-	return Iterator{ctx: ctx, val: v, iter: n, len: len(n.arcs)}, nil
+	return Iterator{ctx: ctx, val: v, iter: obj.n, len: len(obj.n.arcs)}, nil
 }
 
 // Lookup reports the value at a path starting from v.
@@ -1697,7 +1665,7 @@
 		for i := 0; i < obj.Len(); i++ {
 			_, v := obj.At(i)
 			opts := opts
-			if obj.arcs[i].definition {
+			if obj.n.arcs[i].definition {
 				opts.concrete = false
 			}
 			x.walk(v, opts)
diff --git a/cue/types_test.go b/cue/types_test.go
index bbebffd..010808a 100644
--- a/cue/types_test.go
+++ b/cue/types_test.go
@@ -17,7 +17,6 @@
 import (
 	"bytes"
 	"fmt"
-	"go/parser"
 	"io/ioutil"
 	"math"
 	"math/big"
@@ -688,69 +687,6 @@
 	}
 }
 
-func TestLookup(t *testing.T) {
-	_ = parser.ParseFile
-	var runtime = new(Runtime)
-	inst, err := runtime.Compile("x.cue", `
-V :: {
-	x: int
-}
-X :: {
-	<_>: int64
-} & V
-v: X
-`)
-	if err != nil {
-		t.Fatalf("compile: %v", err)
-	}
-	// expr, err := parser.ParseExpr("lookup.cue", `v`, parser.DeclarationErrors, parser.AllErrors)
-	// if err != nil {
-	// 	log.Fatalf("parseExpr: %v", err)
-	// }
-	// v := inst.Eval(expr)
-	testCases := []struct {
-		ref  []string
-		raw  string
-		eval string
-	}{{
-		ref:  []string{"v", "x"},
-		raw:  "(int & <=9223372036854775807 & int & >=-9223372036854775808)",
-		eval: "int64",
-	}}
-	for _, tc := range testCases {
-		v := inst.Lookup(tc.ref...)
-
-		if got := fmt.Sprint(v); got != tc.raw {
-			t.Errorf("got %v; want %v", got, tc.raw)
-		}
-
-		if got := fmt.Sprint(v.Eval().Syntax()); got != tc.eval {
-			t.Errorf("got %v; want %v", got, tc.eval)
-		}
-
-		v = inst.Lookup()
-		for _, ref := range tc.ref {
-			s, err := v.Struct()
-			if err != nil {
-				t.Fatal(err)
-			}
-			fi, err := s.FieldByName(ref)
-			if err != nil {
-				t.Fatal(err)
-			}
-			v = fi.Value
-		}
-
-		if got := fmt.Sprint(v); got != tc.raw {
-			t.Errorf("got %v; want %v", got, tc.raw)
-		}
-
-		if got := fmt.Sprint(v.Eval().Syntax()); got != tc.eval {
-			t.Errorf("got %v; want %v", got, tc.eval)
-		}
-	}
-}
-
 func TestDefaults(t *testing.T) {
 	testCases := []struct {
 		value string
@@ -1809,7 +1745,7 @@
 	return doc
 }
 
-func TestMarshalJSON(t *testing.T) {
+func TestMashalJSON(t *testing.T) {
 	testCases := []struct {
 		value string
 		json  string