cue: merge attributes for top-level fields

Change-Id: Iaf136102819c247897daad84aae7cbee73a3c8ef
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5746
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/export_test.go b/cue/export_test.go
index 88c5b62..3c1a98c 100644
--- a/cue/export_test.go
+++ b/cue/export_test.go
@@ -1089,6 +1089,21 @@
 			_foo: _
 			`,
 		out: `{}`,
+	}, {
+		eval: true,
+		in: `
+		A :: {
+			foo: int @tag2(2)
+		} @tag2(1)
+
+		A :: {
+			foo: 2 @tag(2)
+		} @tag(1)
+
+		`,
+		out: `A :: {
+	foo: 2 @tag(2) @tag2(2)
+} @tag(1) @tag2(1)`,
 	}}
 	for _, tc := range testCases {
 		t.Run("", func(t *testing.T) {
diff --git a/cue/value.go b/cue/value.go
index 026080b..8092014 100644
--- a/cue/value.go
+++ b/cue/value.go
@@ -1351,6 +1351,11 @@
 			isDef = false
 		}
 		x.arcs[i].definition = isDef
+		attrs, err := unifyAttrs(ctx, x, x.arcs[i].attrs, a)
+		if err != nil {
+			x.arcs[i].v = err
+		}
+		x.arcs[i].attrs = attrs
 		// TODO: should we warn if there is a mixed mode of optional and non
 		// optional fields at this point?
 		return