Skip to content

Commit

Permalink
internal/core/adt: optional field rewrite
Browse files Browse the repository at this point in the history
This rewrite includes optional fields as Arcs in a Vertex,
instead of keeping track of them in the StructInfo.
This has the effect of them being evaluated and processed
like any other arc. This, in turn, allows optional fields
to be prepared for comparison, which allows disjunctions
to be evaluated early.

It is also a necessary step for the implementation of
required fields.

Issue #2003

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: Id28c39d6d195b97a9d026fe9804796023306e9bc
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/551104
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
mpvl committed Mar 17, 2023
1 parent 4fed730 commit 19bfdb9
Show file tree
Hide file tree
Showing 43 changed files with 332 additions and 247 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/vet_yaml.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
cmp stderr expect-stderr

-- expect-stderr --
phrases: invalid value "phrases:\n # A quote from Mark Twain.\n quote1:\n lang: en\n attribution: Mark Twain\n\n # A Norwegian proverb.\n proverb:\n lang: no\n text: Stemmen som sier at du ikke klarer det, lyver." (does not satisfy encoding/yaml.Validate({phrases:{},#Phrase:{lang:=~"^[a-zA-Z0-9-_]{2,}$" | false,text:!=""},#LanguageTag:=~"^[a-zA-Z0-9-_]{2,}$" | false})): error in call to encoding/yaml.Validate: incomplete value !="":
phrases: invalid value "phrases:\n # A quote from Mark Twain.\n quote1:\n lang: en\n attribution: Mark Twain\n\n # A Norwegian proverb.\n proverb:\n lang: no\n text: Stemmen som sier at du ikke klarer det, lyver." (does not satisfy encoding/yaml.Validate({phrases:{},#Phrase:{lang:=~"^[a-zA-Z0-9-_]{2,}$" | false,text:!="",},#LanguageTag:=~"^[a-zA-Z0-9-_]{2,}$" | false})): error in call to encoding/yaml.Validate: incomplete value !="":
./yaml.cue:19:10
./yaml.cue:11:17
./yaml.cue:21:10
Expand Down
2 changes: 1 addition & 1 deletion codereview.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Code generated internal/ci/ci_tool.cue; DO NOT EDIT.

github: https://github.com/cue-lang/cue
gerrit: https://review.gerrithub.io/a/cue-lang/cue
github: https://github.com/cue-lang/cue
cue-unity: https://github.com/cue-unity/unity
4 changes: 4 additions & 0 deletions cue/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ outer:
f := sel.sel.feature(v.idx)
for _, a := range n.Arcs {
if a.Label == f {
if a.IsConstraint() && !sel.sel.optional() {
break
}
parent = linkParent(parent, n, a)
n = a
continue outer
}
}
if sel.sel.optional() {
// pattern or additional constraints.
x := &adt.Vertex{
Parent: n,
Label: sel.sel.feature(ctx),
Expand Down
12 changes: 6 additions & 6 deletions cue/testdata/cycle/compbottom2.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ nestedChain: {
}
-- out/eval/stats --
Leaks: 1
Freed: 143
Reused: 134
Freed: 150
Reused: 141
Allocs: 10
Retain: 72
Retain: 76

Unifications: 144
Conjuncts: 159
Disjuncts: 194
Unifications: 151
Conjuncts: 166
Disjuncts: 205
-- out/eval --
(struct){
self: (struct){
Expand Down
10 changes: 5 additions & 5 deletions cue/testdata/cycle/structural.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,14 @@ n3: n1 & {n1}
n4: n1 & {x: n1 & {y: n1 & {z: int}}}
-- out/eval/stats --
Leaks: 16
Freed: 792
Reused: 780
Freed: 794
Reused: 782
Allocs: 28
Retain: 65

Unifications: 622
Conjuncts: 1219
Disjuncts: 841
Unifications: 624
Conjuncts: 1221
Disjuncts: 843
-- out/eval --
Errors:
a1.f.0: structural cycle
Expand Down
10 changes: 5 additions & 5 deletions cue/testdata/definitions/036_closing_with_failed_optional.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ v1:
}
-- out/eval/stats --
Leaks: 0
Freed: 20
Reused: 15
Freed: 28
Reused: 23
Allocs: 5
Retain: 1

Unifications: 16
Conjuncts: 31
Disjuncts: 21
Unifications: 24
Conjuncts: 43
Disjuncts: 29
-- out/eval --
(struct){
#k1: (#struct){
Expand Down
12 changes: 6 additions & 6 deletions cue/testdata/definitions/issue483.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ instance: #Type & {
#Root: {...}
-- out/eval/stats --
Leaks: 0
Freed: 11
Reused: 5
Allocs: 6
Freed: 14
Reused: 7
Allocs: 7
Retain: 3

Unifications: 11
Conjuncts: 31
Disjuncts: 14
Unifications: 14
Conjuncts: 35
Disjuncts: 17
-- out/eval --
(struct){
out: (#struct){
Expand Down
10 changes: 5 additions & 5 deletions cue/testdata/disjunctions/elimination.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,14 @@ issue2263: full: {

-- out/eval/stats --
Leaks: 4
Freed: 2151
Reused: 2135
Freed: 2339
Reused: 2323
Allocs: 20
Retain: 119

Unifications: 1196
Conjuncts: 3205
Disjuncts: 2270
Unifications: 1260
Conjuncts: 3417
Disjuncts: 2458
-- out/eval --
Errors:
issue2209.full.Bar.resource.spec: 6 errors in empty disjunction:
Expand Down
10 changes: 5 additions & 5 deletions cue/testdata/disjunctions/errors.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ explicitDefaultError: {
}
-- out/eval/stats --
Leaks: 0
Freed: 39
Reused: 31
Freed: 40
Reused: 32
Allocs: 8
Retain: 0

Unifications: 27
Conjuncts: 55
Disjuncts: 39
Unifications: 28
Conjuncts: 56
Disjuncts: 40
-- out/eval --
Errors:
issue516.x: 2 errors in empty disjunction:
Expand Down
12 changes: 6 additions & 6 deletions cue/testdata/eval/closed_disjunction.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ b: #A & {
}
-- out/eval/stats --
Leaks: 0
Freed: 32
Reused: 26
Allocs: 6
Freed: 46
Reused: 39
Allocs: 7
Retain: 0

Unifications: 20
Conjuncts: 46
Disjuncts: 32
Unifications: 34
Conjuncts: 60
Disjuncts: 46
-- out/eval --
Errors:
b: 2 errors in empty disjunction:
Expand Down
30 changes: 15 additions & 15 deletions cue/testdata/eval/issue2146.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,19 @@ p2: {

-- out/eval/stats --
Leaks: 37
Freed: 143
Reused: 134
Allocs: 46
Retain: 154
Freed: 152
Reused: 145
Allocs: 44
Retain: 77

Unifications: 164
Conjuncts: 544
Disjuncts: 197
Unifications: 173
Conjuncts: 557
Disjuncts: 206
-- out/eval --
(struct){
p1: (struct){
#A: (#struct){ |(*(#struct){
y: (int){ 1 }
let list#1 = (#list){
0: (_|_){
// [incomplete] p1.#A.list.0: cannot reference optional field: x:
Expand All @@ -63,7 +64,6 @@ Disjuncts: 197
all: (#list){
0: (int){ 1 }
}
y: (int){ 1 }
}, (#struct){
let list#1 = (#list){
0: (_|_){
Expand All @@ -79,6 +79,7 @@ Disjuncts: 197
}
}) }
a: (#struct){
x: (int){ 3 }
let list#1 = (#list){
0: (int){ 3 }
1: (_|_){
Expand All @@ -89,21 +90,21 @@ Disjuncts: 197
all: (#list){
0: (int){ 3 }
}
x: (int){ 3 }
}
b: (#struct){
x: (int){ 3 }
let list#1multi = [
〈1;x〉,
〈1;y〉,
]
all: (#list){
0: (int){ 3 }
}
x: (int){ 3 }
}
}
p2: (struct){
#A: (#struct){ |(*(#struct){
y: (int){ 1 }
let list#2 = (#list){
0: (_|_){
// [incomplete] p2.#A.list.0: cannot reference optional field: x:
Expand All @@ -114,7 +115,6 @@ Disjuncts: 197
all: (#list){
0: (int){ 1 }
}
y: (int){ 1 }
}, (#struct){
let list#2 = (#list){
0: (_|_){
Expand All @@ -130,6 +130,8 @@ Disjuncts: 197
}
}) }
a: (#struct){
x: (int){ 3 }
y: (int){ 2 }
let list#2 = (#list){
0: (int){ 3 }
1: (int){ 2 }
Expand All @@ -138,10 +140,10 @@ Disjuncts: 197
0: (int){ 3 }
1: (int){ 2 }
}
x: (int){ 3 }
y: (int){ 2 }
}
b: (#struct){
x: (int){ 3 }
y: (int){ 2 }
let list#2multi = [
〈1;x〉,
〈1;y〉,
Expand All @@ -150,8 +152,6 @@ Disjuncts: 197
0: (int){ 3 }
1: (int){ 2 }
}
x: (int){ 3 }
y: (int){ 2 }
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions cue/testdata/eval/issue353.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ e: a: "hello"
}
-- out/eval/stats --
Leaks: 0
Freed: 11
Reused: 5
Freed: 15
Reused: 9
Allocs: 6
Retain: 0

Unifications: 7
Conjuncts: 17
Disjuncts: 11
Unifications: 11
Conjuncts: 21
Disjuncts: 15
-- out/eval --
(struct){
e: (#struct){ |((#struct){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ c: d & {
}
-- out/eval/stats --
Leaks: 0
Freed: 11
Reused: 6
Freed: 14
Reused: 9
Allocs: 5
Retain: 0

Unifications: 7
Conjuncts: 13
Disjuncts: 11
Unifications: 10
Conjuncts: 17
Disjuncts: 14
-- out/eval --
(struct){
d: (struct){ |((struct){
Expand Down
10 changes: 5 additions & 5 deletions cue/testdata/fulleval/029_Issue_#94.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ index: {
}
-- out/eval/stats --
Leaks: 0
Freed: 21
Reused: 18
Freed: 22
Reused: 19
Allocs: 3
Retain: 0

Unifications: 21
Conjuncts: 31
Disjuncts: 21
Unifications: 22
Conjuncts: 32
Disjuncts: 22
-- out/eval --
(struct){
foo: (struct){
Expand Down
10 changes: 5 additions & 5 deletions cue/testdata/references/optional.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ a: {
}
-- out/eval/stats --
Leaks: 0
Freed: 3
Reused: 0
Freed: 4
Reused: 1
Allocs: 3
Retain: 0

Unifications: 3
Conjuncts: 3
Disjuncts: 3
Unifications: 4
Conjuncts: 4
Disjuncts: 4
-- out/eval --
(struct){
a: (struct){
Expand Down
10 changes: 5 additions & 5 deletions cue/testdata/resolve/009_optional_field_unification.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ g1: 1
}
-- out/eval/stats --
Leaks: 0
Freed: 9
Reused: 6
Freed: 11
Reused: 8
Allocs: 3
Retain: 0

Unifications: 9
Conjuncts: 16
Disjuncts: 9
Unifications: 11
Conjuncts: 19
Disjuncts: 11
-- out/eval --
(struct){
a: (struct){
Expand Down
Loading

0 comments on commit 19bfdb9

Please sign in to comment.