Skip to content

Commit 185426f

Browse files
committed
internal/core/adt: fix attribute processing
The use of the Vertex.Structs fields has been almost completely eliminated in the new evaluator. It is, however, still used for some things, like keeping track of attributes. This change fixes the bookkeeping of StructLits. It changes the signature of Init to allow it to determine if a full init is necessary. This also gives us some compiler support to check if all call sites in the old evaluator have an equivalent in the new one. Note that one use of Init was missing in the new evaluator. The case used in list processing seems unnecessary in the new evaluator. Issue #3060 Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: I2eb3319f0f3ad26c3d64adcff819016a24ec6e7d Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194658 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent efd38e6 commit 185426f

File tree

9 files changed

+40
-58
lines changed

9 files changed

+40
-58
lines changed

cue/attribute_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ func TestAttributes(t *testing.T) {
9696
}}
9797
for _, tc := range testCases {
9898
cuetdtest.FullMatrix.Run(t, tc.path, func(t *cuetdtest.M) {
99-
t.TODO_V3()
100-
10199
v := getValue(t, config).LookupPath(ParsePath(tc.path))
102100
a := v.Attributes(tc.flags)
103101
got := fmt.Sprint(a)

internal/core/adt/closed_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestClosedness(t *testing.T) {
4141
t.Fatal(err)
4242
}
4343
st := expr.Elem().(*adt.StructLit)
44-
st.Init()
44+
st.Init(ctx)
4545

4646
return &adt.StructInfo{
4747
StructLit: st,

internal/core/adt/comprehension.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func (n *nodeContext) insertComprehension(
244244
case !ec.done:
245245
ec.structs = append(ec.structs, st)
246246
case len(ec.envs) > 0:
247-
st.Init()
247+
st.Init(n.ctx)
248248
}
249249
}
250250

@@ -453,7 +453,7 @@ func (n *nodeContext) processComprehensionInner(d *envYield, state vertexStatus)
453453

454454
if len(d.envs) > 0 {
455455
for _, s := range d.structs {
456-
s.Init()
456+
s.Init(n.ctx)
457457
}
458458
}
459459
d.structs = nil

internal/core/adt/conjunct.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,10 @@ func (n *nodeContext) scheduleStruct(env *Environment,
243243
hasEmbed := false
244244
hasEllipsis := false
245245

246+
// TODO: do we still need this?
246247
// shouldClose := ci.cc.isDef || ci.cc.isClosedOnce
247-
// s.Init()
248+
249+
s.Init(n.ctx)
248250

249251
// TODO: do we still need to AddStruct and do we still need to Disable?
250252
parent := n.node.AddStruct(s, childEnv, ci)

internal/core/adt/eval.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,7 +2050,7 @@ func (n *nodeContext) addStruct(
20502050
Vertex: n.node,
20512051
}
20522052

2053-
s.Init()
2053+
s.Init(n.ctx)
20542054

20552055
if s.HasEmbed && !s.IsFile() {
20562056
closeInfo = closeInfo.SpawnGroup(nil)
@@ -2441,7 +2441,7 @@ outer:
24412441
s := l.list.info
24422442
if s == nil {
24432443
s = &StructLit{Decls: []Decl{l.elipsis}}
2444-
s.Init()
2444+
s.Init(n.ctx)
24452445
l.list.info = s
24462446
}
24472447
info := n.node.AddStruct(s, l.env, l.id)

internal/core/adt/expr.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,16 @@ func (o *StructLit) MarkField(f Feature) {
9797
o.Fields = append(o.Fields, FieldInfo{Label: f})
9898
}
9999

100-
func (o *StructLit) Init() {
100+
func (o *StructLit) Init(ctx *OpContext) {
101101
if o.initialized {
102102
return
103103
}
104104
o.initialized = true
105+
106+
if ctx.isDevVersion() {
107+
return
108+
}
109+
105110
for _, d := range o.Decls {
106111
switch x := d.(type) {
107112
case *Field:

internal/core/adt/unify.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,20 @@ func (n *nodeContext) completeAllArcs(needs condition, mode runMode) bool {
562562
}
563563
n.node.Arcs = n.node.Arcs[:k]
564564

565+
// Strip struct literals that were not initialized and are not part
566+
// of the output.
567+
//
568+
// TODO(perf): we could keep track if any such structs exist and only
569+
// do this removal if there is a change of shrinking the list.
570+
k = 0
571+
for _, s := range n.node.Structs {
572+
if s.initialized {
573+
n.node.Structs[k] = s
574+
k++
575+
}
576+
}
577+
n.node.Structs = n.node.Structs[:k]
578+
565579
// TODO: This seems to be necessary, but enables structural cycles.
566580
// Evaluator whether we still need this.
567581
//

internal/core/export/testdata/main/attrs.txtar

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ dynamicSimple: {
369369
comprehensions: {
370370
@step(0)
371371
@step(2c)
372-
@step(4c)
373372
c1: {} @step(1)
374373
c2: {
375374
@step(2a)
@@ -435,7 +434,6 @@ dynamicSimple: {
435434
comprehensions: {
436435
@step(0)
437436
@step(2c)
438-
@step(4c)
439437
c1: {} @step(1)
440438
c2: {
441439
@step(2a)
@@ -456,7 +454,7 @@ dynamicSimple: {
456454
diff old new
457455
--- old
458456
+++ new
459-
@@ -150,19 +150,20 @@
457+
@@ -150,11 +150,11 @@
460458
@decl1()
461459
}
462460
b: {
@@ -465,32 +463,15 @@ diff old new
465463
- }
466464
- c: {
467465
- @decl1(), @decl2()
468-
- }
469-
- d: {
470-
- @decl2(), @decl1()
471-
- }
472-
- }
473-
- comprehensions: {
474-
- @step(0)
475-
- @step(2c)
476466
+ @decl2(), @decl1()
477467
+ x: 1
478468
+ }
479469
+ c: {
480470
+ @decl2(), @decl1()
481-
+ }
482-
+ d: {
483-
+ @decl2(), @decl1()
484-
+ }
485-
+ }
486-
+ comprehensions: {
487-
+ @step(0)
488-
+ @step(2c)
489-
+ @step(4c)
490-
c1: {} @step(1)
491-
c2: {
492-
@step(2a)
493-
@@ -215,19 +216,20 @@
471+
}
472+
d: {
473+
@decl2(), @decl1()
474+
@@ -215,11 +215,11 @@
494475
@decl1()
495476
}
496477
b: {
@@ -499,33 +480,14 @@ diff old new
499480
- }
500481
- c: {
501482
- @decl1(), @decl2()
502-
- }
503-
- d: {
504-
- @decl2(), @decl1()
505-
- }
506-
- }
507-
- comprehensions: {
508-
- @step(0)
509-
- @step(2c)
510483
+ @decl2(), @decl1()
511484
+ x: 1
512485
+ }
513486
+ c: {
514487
+ @decl2(), @decl1()
515-
+ }
516-
+ d: {
517-
+ @decl2(), @decl1()
518-
+ }
519-
+ }
520-
+ comprehensions: {
521-
+ @step(0)
522-
+ @step(2c)
523-
+ @step(4c)
524-
c1: {} @step(1)
525-
c2: {
526-
@step(2a)
527-
-- diff/value/todo/p1 --
528-
comprehension: @step(4c) added in comprehensions
488+
}
489+
d: {
490+
@decl2(), @decl1()
529491
-- diff/value/todo/p3 --
530492
Reordering of attributes.
531493
-- out/value --

internal/core/export/value_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ var exclude = map[string]string{
3939
func TestValue(t *testing.T) {
4040
const debugValue = `
4141
-- in.cue --
42-
// Issue #2305
43-
g: #d
44-
#d: 1
42+
if false {
43+
c4: { @step(4a) } @step(4b)
44+
@step(4c)
45+
}
4546
`
4647

4748
test := cuetxtar.TxTarTest{

0 commit comments

Comments
 (0)