Skip to content
This repository was archived by the owner on Nov 18, 2021. It is now read-only.

Commit f1d3d81

Browse files
committed
internal/core/export: fix decl export printing and extraction
Fixes #894 A few related fixes: - Extract from all evaluated structs, not just unevaluated conjuncts, which results in dropping merged values. (Note that unlike with fields attributes, the the declaration attributes are value-bound, and thus need to be included.) - Decl attrs were not always printed in export. This has now been fixed. - Changed the (internal) API to accept a Vertex, instead of a list of conjuncts, which is a better abstraction. Change-Id: Id598fefa88aff88aefd126fb80f1637b3f50c7fb Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9382 Reviewed-by: CUE cueckoo <cueckoo@gmail.com> Reviewed-by: Paul Jolly <paul@myitcv.org.uk> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
1 parent c8b7fe0 commit f1d3d81

7 files changed

Lines changed: 277 additions & 28 deletions

File tree

cue/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,7 +2111,7 @@ func (v Value) Attribute(key string) Attribute {
21112111
return nonExistAttr(key)
21122112
}
21132113
// look up the attributes
2114-
for _, a := range export.ExtractFieldAttrs(v.v.Conjuncts) {
2114+
for _, a := range export.ExtractFieldAttrs(v.v) {
21152115
k, _ := a.Split()
21162116
if key != k {
21172117
continue
@@ -2149,13 +2149,13 @@ func (v Value) Attributes(mask AttrKind) []Attribute {
21492149
attrs := []Attribute{}
21502150

21512151
if mask&FieldAttr != 0 {
2152-
for _, a := range export.ExtractFieldAttrs(v.v.Conjuncts) {
2152+
for _, a := range export.ExtractFieldAttrs(v.v) {
21532153
attrs = append(attrs, newAttr(internal.FieldAttr, a))
21542154
}
21552155
}
21562156

21572157
if mask&DeclAttr != 0 {
2158-
for _, a := range export.ExtractDeclAttrs(v.v.Conjuncts) {
2158+
for _, a := range export.ExtractDeclAttrs(v.v) {
21592159
attrs = append(attrs, newAttr(internal.DeclAttr, a))
21602160
}
21612161
}

internal/core/export/expr.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,31 @@ func (x *exporter) mergeValues(label adt.Feature, src *adt.Vertex, a []conjunct,
137137
if len(e.fields) == 0 && !e.hasEllipsis {
138138
switch len(e.embed) + len(e.conjuncts) {
139139
case 0:
140+
if len(e.attrs) > 0 {
141+
break
142+
}
140143
if len(e.structs) > 0 {
141144
return s
142145
}
143146
return ast.NewIdent("_")
144147
case 1:
148+
var x ast.Expr
145149
if len(e.conjuncts) == 1 {
146-
return e.conjuncts[0]
150+
x = e.conjuncts[0]
151+
} else {
152+
x = e.embed[0]
153+
}
154+
if len(e.attrs) == 0 {
155+
return x
156+
}
157+
if st, ok := x.(*ast.StructLit); ok {
158+
s.Elts = append(s.Elts, st.Elts...)
159+
return s
147160
}
148-
return e.embed[0]
149161
case 2:
162+
if len(e.attrs) > 0 {
163+
break
164+
}
150165
// Simplify.
151166
e.conjuncts = append(e.conjuncts, e.embed...)
152167
return ast.NewBinExpr(token.AND, e.conjuncts...)
@@ -195,7 +210,9 @@ func (x *exporter) mergeValues(label adt.Feature, src *adt.Vertex, a []conjunct,
195210
ast.SetComments(d, docs)
196211
}
197212
if x.cfg.ShowAttributes {
198-
d.Attrs = ExtractFieldAttrs(a)
213+
for _, c := range a {
214+
d.Attrs = extractFieldAttrs(d.Attrs, c)
215+
}
199216
}
200217
s.Elts = append(s.Elts, d)
201218
}

internal/core/export/extract.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,15 @@ func containsDoc(a []*ast.CommentGroup, cg *ast.CommentGroup) bool {
137137
return false
138138
}
139139

140-
func ExtractFieldAttrs(a []adt.Conjunct) (attrs []*ast.Attribute) {
141-
for _, x := range a {
142-
f, ok := x.Source().(*ast.Field)
143-
if !ok {
144-
continue
145-
}
140+
func ExtractFieldAttrs(v *adt.Vertex) (attrs []*ast.Attribute) {
141+
for _, x := range v.Conjuncts {
142+
attrs = extractFieldAttrs(attrs, x)
143+
}
144+
return attrs
145+
}
146+
147+
func extractFieldAttrs(attrs []*ast.Attribute, c adt.Conjunct) []*ast.Attribute {
148+
if f, ok := c.Source().(*ast.Field); ok {
146149
for _, a := range f.Attrs {
147150
if !containsAttr(attrs, a) {
148151
attrs = append(attrs, a)
@@ -152,9 +155,11 @@ func ExtractFieldAttrs(a []adt.Conjunct) (attrs []*ast.Attribute) {
152155
return attrs
153156
}
154157

155-
func ExtractDeclAttrs(a []adt.Conjunct) (attrs []*ast.Attribute) {
156-
for _, c := range a {
157-
attrs = extractDeclAttrs(attrs, c.Expr().Source())
158+
func ExtractDeclAttrs(v *adt.Vertex) (attrs []*ast.Attribute) {
159+
for _, st := range v.Structs {
160+
if src := st.StructLit; src != nil {
161+
attrs = extractDeclAttrs(attrs, src.Src)
162+
}
158163
}
159164
return attrs
160165
}

internal/core/export/testdata/adt.txtar

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ errorListDef: {
120120
-- out/definition --
121121
import mystrings "strings"
122122

123+
@foo(bar)
123124
p1: {
124125
[X=string]: {
125126
name: X

internal/core/export/testdata/attrs.txtar

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@ a: {
1818

1919
a: {} @field(1) @field(3)
2020

21+
doNotPropagate: {
22+
#A: { } @attr1()
23+
a: #A
24+
25+
// Do not accumulated field attributes in embedding.
26+
#B: { } @attr1()
27+
b: { #B }
28+
}
29+
30+
embedScalarField: {
31+
a: { 2 } @attr1()
32+
a: { _ } @attr2()
33+
}
34+
35+
embedScalarDecl: {
36+
b0: { 2, @attr1() }
37+
b1: b0
38+
b2: { 2, b0, @attr2() }
39+
}
40+
41+
dontMergeForDef: {
42+
a: { @decl1() }
43+
b: a & { x: 1, @decl2() }
44+
c: a & { @decl2() }
45+
d: { a, @decl2() }
46+
}
47+
2148
-- b.cue --
2249
@package("b")
2350

@@ -42,21 +69,143 @@ a: {
4269
@decl(3)
4370
@decl(5)
4471
} @field(2) @field(1) @field(4) @field(3) @field(5)
72+
doNotPropagate: {
73+
#A: {} @attr1()
74+
a: #A
75+
76+
// Do not accumulated field attributes in embedding.
77+
#B: {} @attr1()
78+
b: #B
79+
}
80+
embedScalarField: {
81+
a: 2 @attr1() @attr2()
82+
}
83+
embedScalarDecl: {
84+
b0: {
85+
@attr1()
86+
2
87+
}
88+
b1: b0
89+
b2: {
90+
@attr2()
91+
b0
92+
2
93+
}
94+
}
95+
dontMergeForDef: {
96+
a: {
97+
@decl1()
98+
}
99+
b: a & {
100+
@decl2()
101+
x: 1
102+
}
103+
c: a & {
104+
@decl2()
105+
}
106+
d: {
107+
@decl2()
108+
a
109+
}
110+
}
45111
-- out/doc --
46112
[]
47113
[a]
114+
[doNotPropagate]
115+
[doNotPropagate #A]
116+
[doNotPropagate a]
117+
[doNotPropagate #B]
118+
- Do not accumulated field attributes in embedding.
119+
120+
[doNotPropagate b]
121+
[embedScalarField]
122+
[embedScalarField a]
123+
[embedScalarDecl]
124+
[embedScalarDecl b0]
125+
[embedScalarDecl b1]
126+
[embedScalarDecl b2]
127+
[dontMergeForDef]
128+
[dontMergeForDef a]
129+
[dontMergeForDef b]
130+
[dontMergeForDef b x]
131+
[dontMergeForDef c]
132+
[dontMergeForDef d]
48133
-- out/value --
49134
== Simplified
50135
{
51136
a: {}
137+
doNotPropagate: {
138+
a: {}
139+
b: {}
140+
}
141+
embedScalarField: {
142+
a: 2
143+
}
144+
embedScalarDecl: {
145+
b0: 2
146+
b1: 2
147+
b2: 2
148+
}
149+
dontMergeForDef: {
150+
a: {}
151+
b: {
152+
x: 1
153+
}
154+
c: {}
155+
d: {}
156+
}
52157
}
53158
== Raw
54159
{
55160
a: {}
161+
doNotPropagate: {
162+
#A: {}
163+
a: {}
164+
165+
// Do not accumulated field attributes in embedding.
166+
#B: {}
167+
b: {}
168+
}
169+
embedScalarField: {
170+
a: 2
171+
}
172+
embedScalarDecl: {
173+
b0: 2
174+
b1: 2
175+
b2: 2
176+
}
177+
dontMergeForDef: {
178+
a: {}
179+
b: {
180+
x: 1
181+
}
182+
c: {}
183+
d: {}
184+
}
56185
}
57186
== Final
58187
{
59188
a: {}
189+
doNotPropagate: {
190+
a: {}
191+
b: {}
192+
}
193+
embedScalarField: {
194+
a: 2
195+
}
196+
embedScalarDecl: {
197+
b0: 2
198+
b1: 2
199+
b2: 2
200+
}
201+
dontMergeForDef: {
202+
a: {}
203+
b: {
204+
x: 1
205+
}
206+
c: {}
207+
d: {}
208+
}
60209
}
61210
== All
62211
{
@@ -69,6 +218,43 @@ a: {
69218
@decl(3)
70219
@decl(5)
71220
} @field(2) @field(1) @field(4) @field(3) @field(5)
221+
doNotPropagate: {
222+
#A: {} @attr1()
223+
a: {}
224+
225+
// Do not accumulated field attributes in embedding.
226+
#B: {} @attr1()
227+
b: {}
228+
}
229+
embedScalarField: {
230+
a: 2 @attr1() @attr2()
231+
}
232+
embedScalarDecl: {
233+
b0: {
234+
2, @attr1()
235+
}
236+
b1: {
237+
2, @attr1()
238+
}
239+
b2: {
240+
2, @attr2(), @attr1()
241+
}
242+
}
243+
dontMergeForDef: {
244+
a: {
245+
@decl1()
246+
}
247+
b: {
248+
@decl1(), @decl2()
249+
x: 1
250+
}
251+
c: {
252+
@decl1(), @decl2()
253+
}
254+
d: {
255+
@decl2(), @decl1()
256+
}
257+
}
72258
}
73259
== Eval
74260
{
@@ -81,4 +267,39 @@ a: {
81267
@decl(3)
82268
@decl(5)
83269
} @field(2) @field(1) @field(4) @field(3) @field(5)
270+
doNotPropagate: {
271+
#A: {} @attr1()
272+
a: {}
273+
#B: {} @attr1()
274+
b: {}
275+
}
276+
embedScalarField: {
277+
a: 2 @attr1() @attr2()
278+
}
279+
embedScalarDecl: {
280+
b0: {
281+
2, @attr1()
282+
}
283+
b1: {
284+
2, @attr1()
285+
}
286+
b2: {
287+
2, @attr2(), @attr1()
288+
}
289+
}
290+
dontMergeForDef: {
291+
a: {
292+
@decl1()
293+
}
294+
b: {
295+
@decl1(), @decl2()
296+
x: 1
297+
}
298+
c: {
299+
@decl1(), @decl2()
300+
}
301+
d: {
302+
@decl2(), @decl1()
303+
}
304+
}
84305
}

0 commit comments

Comments
 (0)