cue/cmd/cue: fix regression for -H flag

Instance iterator accidentally would include instance twice
in some cases, which caused it to be unified into a new
anonoymous package for which hidden fields would not
be shown.

Also adds an optimization in Unify to return one of
the arguments if they are identical, instead of unifying.

The setting the variable instead of returning makes using
a debugger easier.

Change-Id: I311c0d2dcb50d277a47eaaae352e5fb467935eba
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9844
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go
index 31c2f64..9401182 100644
--- a/cmd/cue/cmd/common.go
+++ b/cmd/cue/cmd/common.go
@@ -173,9 +173,8 @@
 		}
 	default:
 		i = &instanceIterator{
-			inst: b.instance,
-			a:    []*cue.Instance{b.instance},
-			i:    -1,
+			a: []*cue.Instance{b.instance},
+			i: -1,
 		}
 		b.instance = nil
 	}
diff --git a/cmd/cue/cmd/testdata/script/eval_e_hidden.txt b/cmd/cue/cmd/testdata/script/eval_e_hidden.txt
index 0148499..3f0a3d8 100644
--- a/cmd/cue/cmd/testdata/script/eval_e_hidden.txt
+++ b/cmd/cue/cmd/testdata/script/eval_e_hidden.txt
@@ -7,6 +7,10 @@
 
 cue eval -e _a tst.cue
 stdout '34'
+
+cue eval -H
+stdout '_a: 34'
+
 -- dep.cue --
 package dep
 
diff --git a/cmd/cue/cmd/testdata/script/hidden.txt b/cmd/cue/cmd/testdata/script/hidden.txt
new file mode 100644
index 0000000..32ecf7a
--- /dev/null
+++ b/cmd/cue/cmd/testdata/script/hidden.txt
@@ -0,0 +1,46 @@
+cue eval pkg.cue -H
+cmp stdout expect-stdout
+
+cue eval -H
+cmp stdout expect-stdout
+
+cue eval file.cue -H
+cmp stdout expect-stdout
+
+-- pkg.cue --
+package pkg
+
+_top: 1
+
+a: _h0: int
+
+#foo: {
+    _h1: string
+}
+
+{
+    _h2: string
+}
+
+-- file.cue --
+_top: 1
+
+a: _h0: int
+
+#foo: {
+    _h1: string
+}
+
+{
+    _h2: string
+}
+
+-- expect-stdout --
+_top: 1
+a: {
+    _h0: int
+}
+_h2: string
+#foo: {
+    _h1: string
+}
diff --git a/cue/types.go b/cue/types.go
index 636fe52..3502e4a 100644
--- a/cue/types.go
+++ b/cue/types.go
@@ -1815,7 +1815,7 @@
 	if v.v == nil {
 		return w
 	}
-	if w.v == nil {
+	if w.v == nil || w.v == v.v {
 		return v
 	}
 
diff --git a/internal/core/adt/feature.go b/internal/core/adt/feature.go
index 2df3656..a099314 100644
--- a/internal/core/adt/feature.go
+++ b/internal/core/adt/feature.go
@@ -107,7 +107,7 @@
 	}
 	s := index.IndexToString(f.safeIndex())
 	if p := strings.IndexByte(s, '\x00'); p >= 0 {
-		return s[p+1:]
+		s = s[p+1:]
 	}
 	return s
 }