cue: more path cleanup

Change-Id: I4bb273b019e40b0c7b6dd838b7cfb7def6ad2627
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9573
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
diff --git a/cue/errors.go b/cue/errors.go
index f1a8030..8ac9891 100644
--- a/cue/errors.go
+++ b/cue/errors.go
@@ -87,7 +87,7 @@
 			return a
 		}
 	}
-	return e.v.appendPath(nil)
+	return pathToStrings(e.v.Path())
 }
 
 var errNotExists = &adt.Bottom{
diff --git a/cue/path.go b/cue/path.go
index b5574c0..465e360 100644
--- a/cue/path.go
+++ b/cue/path.go
@@ -106,6 +106,14 @@
 	return Path{path: selectors}
 }
 
+// pathToString is a utility function for creating debugging info.
+func pathToStrings(p Path) (a []string) {
+	for _, sel := range p.Selectors() {
+		a = append(a, sel.String())
+	}
+	return a
+}
+
 // ParsePath parses a CUE expression into a Path. Any error resulting from
 // this conversion can be obtained by calling Err on the result.
 //
diff --git a/cue/types.go b/cue/types.go
index 75b70b2..71abe0a 100644
--- a/cue/types.go
+++ b/cue/types.go
@@ -21,7 +21,6 @@
 	"io"
 	"math"
 	"math/big"
-	"strconv"
 	"strings"
 
 	"github.com/cockroachdb/apd/v2"
@@ -29,7 +28,6 @@
 	"cuelang.org/go/cue/ast"
 	"cuelang.org/go/cue/ast/astutil"
 	"cuelang.org/go/cue/errors"
-	"cuelang.org/go/cue/literal"
 	"cuelang.org/go/cue/token"
 	"cuelang.org/go/internal"
 	"cuelang.org/go/internal/core/adt"
@@ -551,27 +549,6 @@
 	return f, nil
 }
 
-func (v Value) appendPath(a []string) []string {
-	for _, f := range v.v.Path() {
-		switch f.Typ() {
-		case adt.IntLabel:
-			a = append(a, strconv.FormatInt(int64(f.Index()), 10))
-
-		case adt.StringLabel:
-			label := v.idx.LabelStr(f)
-			if !f.IsDef() && !f.IsHidden() {
-				if !ast.IsValidIdent(label) {
-					label = literal.String.Quote(label)
-				}
-			}
-			a = append(a, label)
-		default:
-			a = append(a, f.SelectorString(v.idx))
-		}
-	}
-	return a
-}
-
 // Value holds any value, which may be a Boolean, Error, List, Null, Number,
 // Struct, or String.
 type Value struct {
diff --git a/cue/types_test.go b/cue/types_test.go
index e949336..88535a5 100644
--- a/cue/types_test.go
+++ b/cue/types_test.go
@@ -2248,7 +2248,7 @@
 					v = v.Lookup(e)
 				}
 			}
-			got := v.appendPath(nil)
+			got := pathToStrings(v.Path())
 			if !reflect.DeepEqual(got, tc) {
 				t.Errorf("got %v; want %v", got, tc)
 			}
@@ -3124,6 +3124,16 @@
 			if gotPath != tc.want {
 				t.Errorf("got path %s; want %s", gotPath, tc.want)
 			}
+
+			x, p := v.ReferencePath()
+			if x.Value() != inst.Value() {
+				t.Error("reference not in original instance")
+			}
+			gotPath = p.String()
+			if gotPath != tc.want {
+				t.Errorf("got path %s; want %s", gotPath, tc.want)
+			}
+
 		})
 	}
 }