cue: exempt hidden fields also in Unify and UnifyAccept

These implemented their own logic for top-level fields
that was not entirely in sync with the main implementation.

Fixes #628

Change-Id: I0704e2667c87accb9752936843407925c11064fc
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8230
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
diff --git a/cue/types_test.go b/cue/types_test.go
index fc1e77b..925d3a9 100644
--- a/cue/types_test.go
+++ b/cue/types_test.go
@@ -1403,6 +1403,11 @@
 		pathA: a,
 		pathB: b,
 		want:  `["foo",4]`,
+	}, {
+		value: `a: {a: string, _hidden: int, _#hidden: int}, b: close({a: "foo"})`,
+		pathA: a,
+		pathB: b,
+		want:  `{"a":"foo"}`,
 	}}
 	for _, tc := range testCases {
 		t.Run(tc.value, func(t *testing.T) {
diff --git a/internal/core/adt/closed2.go b/internal/core/adt/closed2.go
index befde5e..60496ae 100644
--- a/internal/core/adt/closed2.go
+++ b/internal/core/adt/closed2.go
@@ -29,8 +29,10 @@
 	return v.IsClosed(ctx)
 }
 
+// TODO: cleanup code and error messages. Reduce duplication in some related
+// code.
 func verifyArc2(ctx *OpContext, f Feature, v *Vertex, isClosed bool) (found bool, err *Bottom) {
-
+	// TODO(perf): collect positions in error.
 	defer ctx.ReleasePositions(ctx.MarkPositions())
 
 	if ok, required := Accept(ctx, v.Parent, f); ok || (!required && !isClosed) {
@@ -38,7 +40,7 @@
 	}
 
 	if !f.IsString() && f != InvalidLabel {
-		// if f.IsHidden() && f != InvalidLabel {
+		// if f.IsHidden() && f != InvalidLabel { Also change Accept in composite.go
 		return false, nil
 	}
 
diff --git a/internal/core/adt/composite.go b/internal/core/adt/composite.go
index d197a88..49995be 100644
--- a/internal/core/adt/composite.go
+++ b/internal/core/adt/composite.go
@@ -520,9 +520,10 @@
 		return !v.IsClosed(ctx)
 	}
 
-	if !v.IsClosed(ctx) || v.Lookup(f) != nil {
+	if !f.IsString() || !v.IsClosed(ctx) || v.Lookup(f) != nil {
 		return true
 	}
+
 	// TODO(perf): collect positions in error.
 	defer ctx.ReleasePositions(ctx.MarkPositions())