Skip to content

Commit 29236f2

Browse files
committed
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 <[email protected]>
1 parent d6ec5cf commit 29236f2

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

cue/types_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,47 +1590,71 @@ func TestValueDoc(t *testing.T) {
15901590
field2: int
15911591
}
15921592
`
1593+
config2 := `
1594+
// Another Foo.
1595+
Foo: {}
1596+
`
1597+
inst := getInstance(t, config)
1598+
v1 := inst.Value()
1599+
v2 := getInstance(t, config2).Value()
1600+
both := v1.Unify(v2)
1601+
15931602
testCases := []struct {
1603+
val Value
15941604
path string
15951605
doc string
15961606
}{{
1607+
val: v1,
15971608
path: "foos",
15981609
doc: "foos are instances of Foo.\n",
15991610
}, {
1611+
val: v1,
16001612
path: "foos MyFoo",
16011613
doc: "My first little foo.\n",
16021614
}, {
1615+
val: v1,
16031616
path: "foos MyFoo field1",
16041617
doc: `field1 is an int.
16051618
16061619
local field comment.
16071620
`,
16081621
}, {
1622+
val: v1,
16091623
path: "foos MyFoo field2",
16101624
doc: "other field comment.\n",
16111625
}, {
1626+
val: v1,
16121627
path: "foos MyFoo dup3",
16131628
doc: `duplicate field comment
16141629
16151630
duplicate field comment
16161631
`,
16171632
}, {
1633+
val: v1,
16181634
path: "bar field1",
16191635
doc: "comment from bar on field 1\n",
16201636
}, {
1637+
val: v1,
16211638
path: "baz field1",
16221639
doc: `comment from baz on field 1
16231640
16241641
comment from bar on field 1
16251642
`,
16261643
}, {
1644+
val: v1,
16271645
path: "baz field2",
16281646
doc: "comment from bar on field 2\n",
1647+
}, {
1648+
val: both,
1649+
path: "Foo",
1650+
doc: `Another Foo.
1651+
1652+
A Foo fooses stuff.
1653+
`,
16291654
}}
1630-
inst := getInstance(t, config)
16311655
for _, tc := range testCases {
16321656
t.Run("field:"+tc.path, func(t *testing.T) {
1633-
v := inst.Value().Lookup(strings.Split(tc.path, " ")...)
1657+
v := tc.val.Lookup(strings.Split(tc.path, " ")...)
16341658
doc := docStr(v.Doc())
16351659
if doc != tc.doc {
16361660
t.Errorf("doc: got:\n%vwant:\n%v", doc, tc.doc)

cue/value.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ func mergeDocs(a, b *docNode) *docNode {
885885
return b
886886
}
887887
if b == nil {
888-
return b
888+
return a
889889
}
890890
// TODO: filter out duplicates?
891891
return &docNode{nil, a, b}
@@ -910,6 +910,7 @@ func (x *structLit) insertValue(ctx *context, f label, optional bool, value valu
910910
// TODO: should we warn if there is a mixed mode of optional and non
911911
// optional fields at this point?
912912
x.arcs[i].optional = x.arcs[i].optional && optional
913+
x.arcs[i].docs = mergeDocs(x.arcs[i].docs, docs)
913914
return
914915
}
915916
x.arcs = append(x.arcs, arc{f, optional, value, nil, a, docs})

0 commit comments

Comments
 (0)