Skip to content

Commit

Permalink
internal/core/adt: avoid duplicate insertion of conjuncts
Browse files Browse the repository at this point in the history
The current code could result in adding conjuncts both
dynamically and through insertConjuncts.

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: I86b1bf37a2dc677f71e1a2f3b54d057d196b689f
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/546855
Unity-Result: CUEcueckoo <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
mpvl committed Nov 30, 2022
1 parent a68c6ce commit d341814
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cue/testdata/comprehensions/pushdown.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ Allocs: 23
Retain: 70

Unifications: 389
Conjuncts: 644
Conjuncts: 632
Disjuncts: 433
-- out/eval --
Errors:
Expand Down
10 changes: 5 additions & 5 deletions cue/testdata/cycle/chain.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ issue2052: full: {
}
-- out/eval/stats --
Leaks: 17
Freed: 39364
Reused: 39317
Freed: 20940
Reused: 20893
Allocs: 64
Retain: 143

Unifications: 12118
Conjuncts: 67085
Disjuncts: 39452
Unifications: 7522
Conjuncts: 34391
Disjuncts: 21028
-- out/eval --
(struct){
chain: (struct){
Expand Down
2 changes: 1 addition & 1 deletion cue/testdata/cycle/comprehension.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ Allocs: 60
Retain: 127

Unifications: 828
Conjuncts: 2532
Conjuncts: 2523
Disjuncts: 1377
-- out/eval --
Errors:
Expand Down
20 changes: 1 addition & 19 deletions cue/testdata/cycle/self.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ Allocs: 23
Retain: 33

Unifications: 236
Conjuncts: 700
Conjuncts: 687
Disjuncts: 435
-- out/eval --
Errors:
Expand All @@ -291,15 +291,6 @@ list.error2.a: conflicting values "3" and "2":
./in.cue:15:6
./in.cue:16:11
list.error2.a: element at index 2 not allowed by earlier comprehension or reference cycle
list.error2.a.0: conflicting values "3" and "1":
./in.cue:15:6
./in.cue:16:6
list.error2.a.1: conflicting values "1" and "2":
./in.cue:16:6
./in.cue:16:11
list.error2.a.1: conflicting values "3" and "2":
./in.cue:15:6
./in.cue:16:11
list.error2.a.2: conflicting values "1" and "2":
./in.cue:16:6
./in.cue:16:11
Expand Down Expand Up @@ -348,9 +339,6 @@ Result:
// [eval] list.error2.a: conflicting values "3" and "1":
// ./in.cue:15:6
// ./in.cue:16:6
// list.error2.a.0: conflicting values "3" and "1":
// ./in.cue:15:6
// ./in.cue:16:6
}
1: (_|_){
// [eval] list.error2.a: conflicting values "1" and "2":
Expand All @@ -359,12 +347,6 @@ Result:
// list.error2.a: conflicting values "3" and "2":
// ./in.cue:15:6
// ./in.cue:16:11
// list.error2.a.1: conflicting values "1" and "2":
// ./in.cue:16:6
// ./in.cue:16:11
// list.error2.a.1: conflicting values "3" and "2":
// ./in.cue:15:6
// ./in.cue:16:11
}
2: (_|_){
// [eval] list.error2.a.2: conflicting values "1" and "2":
Expand Down
10 changes: 6 additions & 4 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,11 +757,13 @@ func (v *Vertex) AddConjunct(c Conjunct) *Bottom {
// This is likely a bug in the evaluator and should not happen.
return &Bottom{Err: errors.Newf(token.NoPos, "cannot add conjunct")}
}
v.addConjunct(c)
if !v.hasConjunct(c) {
v.addConjunctUnchecked(c)
}
return nil
}

func (v *Vertex) addConjunct(c Conjunct) {
func (v *Vertex) hasConjunct(c Conjunct) (added bool) {
switch c.x.(type) {
case *OptionalField, *BulkOptionalField, *Ellipsis:
default:
Expand All @@ -772,10 +774,10 @@ func (v *Vertex) addConjunct(c Conjunct) {
if x.CloseInfo.closeInfo == c.CloseInfo.closeInfo &&
x.x == c.x &&
x.Env.Up == c.Env.Up && x.Env.Vertex == c.Env.Vertex {
return
return true
}
}
v.addConjunctUnchecked(c)
return false
}

func (v *Vertex) addConjunctUnchecked(c Conjunct) {
Expand Down
7 changes: 6 additions & 1 deletion internal/core/adt/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1964,13 +1964,18 @@ func (n *nodeContext) insertField(f Feature, x Conjunct) *Vertex {
arc.MultiLet = true
return arc
}
arc.addConjunct(x)
if arc.hasConjunct(x) {
return arc
}

switch {
case arc.state != nil:
arc.Conjuncts = append(arc.Conjuncts, x)
arc.state.addExprConjunct(x)

case arc.Status() == 0:
arc.addConjunctUnchecked(x)

default:
n.addBottom(&Bottom{
Code: IncompleteError,
Expand Down

0 comments on commit d341814

Please sign in to comment.