internal/core/convert: fix faulty label update

The code assumed that the arc for which the
label was updated was generated by convert.go
itself. This is not always the case, however.

This fix is to always use a new Vertex.

Fixes #616

Change-Id: Icddda11d9a5f178e7ae402d80a8d57d315be04d7
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8047
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/internal/core/convert/go.go b/internal/core/convert/go.go
index e585ad9..5c74ad4 100644
--- a/internal/core/convert/go.go
+++ b/internal/core/convert/go.go
@@ -451,6 +451,8 @@
 				obj.Decls = append(obj.Decls, &adt.Field{Label: f, Value: sub})
 				arc, ok := sub.(*adt.Vertex)
 				if ok {
+					a := *arc
+					arc = &a
 					arc.Label = f
 				} else {
 					arc = &adt.Vertex{Label: f, BaseValue: sub}
@@ -497,6 +499,8 @@
 					f := ctx.StringLabel(s)
 					arc, ok := sub.(*adt.Vertex)
 					if ok {
+						a := *arc
+						arc = &a
 						arc.Label = f
 					} else {
 						arc = &adt.Vertex{Label: f, BaseValue: sub}
diff --git a/pkg/list/testdata/sort.txtar b/pkg/list/testdata/sort.txtar
new file mode 100644
index 0000000..6ab7570
--- /dev/null
+++ b/pkg/list/testdata/sort.txtar
@@ -0,0 +1,54 @@
+// Issue #616
+-- in.cue --
+import "list"
+
+t1: {
+    l: ["c","b","a"]
+    ls: list.Sort(l, list.Ascending)
+    il: list.IsSorted(l, list.Ascending)
+    ils: list.IsSorted(ls, list.Ascending)
+}
+
+t2: {
+    l: ["c","b","a"]
+    il: list.IsSorted(l, list.Ascending)
+
+    ls: list.Sort(l, list.Ascending)
+    ils: list.IsSorted(ls, list.Ascending)
+}
+
+t3: {
+    L: ["c","b","a", "e", "d"]
+    l: list.Take(L, 4)
+    il: list.IsSorted(l, list.Ascending)
+
+    ls: list.Sort(l, list.Ascending)
+    ils: list.IsSorted(ls, list.Ascending)
+
+    l2: l
+    l3: ls
+}
+
+-- out/list --
+t1: {
+	l: ["c", "b", "a"]
+	ls: ["a", "b", "c"]
+	il:  false
+	ils: true
+}
+t2: {
+	l: ["c", "b", "a"]
+	il: false
+	ls: ["a", "b", "c"]
+	ils: true
+}
+t3: {
+	L: ["c", "b", "a", "e", "d"]
+	l: ["c", "b", "a", "e"]
+	il: false
+	ls: ["a", "b", "c", "e"]
+	ils: true
+	l2: ["c", "b", "a", "e"]
+	l3: ["a", "b", "c", "e"]
+}
+