cue: fix possible drop of doc comment in unification

Change-Id: I90b536299f1e02d2245eb13e70e5f1773edce2c5
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2714
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/types_test.go b/cue/types_test.go
index 55e8034..df59170 100644
--- a/cue/types_test.go
+++ b/cue/types_test.go
@@ -1590,47 +1590,71 @@
 		field2: int
 	}
 	`
+	config2 := `
+	// Another Foo.
+	Foo: {}
+	`
+	inst := getInstance(t, config)
+	v1 := inst.Value()
+	v2 := getInstance(t, config2).Value()
+	both := v1.Unify(v2)
+
 	testCases := []struct {
+		val  Value
 		path string
 		doc  string
 	}{{
+		val:  v1,
 		path: "foos",
 		doc:  "foos are instances of Foo.\n",
 	}, {
+		val:  v1,
 		path: "foos MyFoo",
 		doc:  "My first little foo.\n",
 	}, {
+		val:  v1,
 		path: "foos MyFoo field1",
 		doc: `field1 is an int.
 
 local field comment.
 `,
 	}, {
+		val:  v1,
 		path: "foos MyFoo field2",
 		doc:  "other field comment.\n",
 	}, {
+		val:  v1,
 		path: "foos MyFoo dup3",
 		doc: `duplicate field comment
 
 duplicate field comment
 `,
 	}, {
+		val:  v1,
 		path: "bar field1",
 		doc:  "comment from bar on field 1\n",
 	}, {
+		val:  v1,
 		path: "baz field1",
 		doc: `comment from baz on field 1
 
 comment from bar on field 1
 `,
 	}, {
+		val:  v1,
 		path: "baz field2",
 		doc:  "comment from bar on field 2\n",
+	}, {
+		val:  both,
+		path: "Foo",
+		doc: `Another Foo.
+
+A Foo fooses stuff.
+`,
 	}}
-	inst := getInstance(t, config)
 	for _, tc := range testCases {
 		t.Run("field:"+tc.path, func(t *testing.T) {
-			v := inst.Value().Lookup(strings.Split(tc.path, " ")...)
+			v := tc.val.Lookup(strings.Split(tc.path, " ")...)
 			doc := docStr(v.Doc())
 			if doc != tc.doc {
 				t.Errorf("doc: got:\n%vwant:\n%v", doc, tc.doc)
diff --git a/cue/value.go b/cue/value.go
index c8f0997..a459740 100644
--- a/cue/value.go
+++ b/cue/value.go
@@ -885,7 +885,7 @@
 		return b
 	}
 	if b == nil {
-		return b
+		return a
 	}
 	// TODO: filter out duplicates?
 	return &docNode{nil, a, b}
@@ -910,6 +910,7 @@
 		// TODO: should we warn if there is a mixed mode of optional and non
 		// optional fields at this point?
 		x.arcs[i].optional = x.arcs[i].optional && optional
+		x.arcs[i].docs = mergeDocs(x.arcs[i].docs, docs)
 		return
 	}
 	x.arcs = append(x.arcs, arc{f, optional, value, nil, a, docs})