Skip to content

Commit

Permalink
tools/trim: implement trim for evalv3
Browse files Browse the repository at this point in the history
Trim now works on the result of evaluation for evalv3.

This new implementation only works for evalv3, and does not alter any
code paths for evalv2 and the existing trim.

The new implementation addresses all known bugs with trim.

Signed-off-by: Matthew Sackman <[email protected]>
Change-Id: If814b5f66ce2ea3ce37ddcbec71eb38f1bc97216
Dispatch-Trailer: {"type":"trybot","CL":1208658,"patchset":5,"ref":"refs/changes/58/1208658/5","targetBranch":"master"}
  • Loading branch information
cuematthew authored and cueckoo committed Feb 14, 2025
1 parent 64abb36 commit 9938e1c
Show file tree
Hide file tree
Showing 78 changed files with 3,372 additions and 31 deletions.
63 changes: 63 additions & 0 deletions cmd/cue/cmd/testdata/script/trim_package.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
exec cue mod init

# -- evalv3 --
env CUE_EXPERIMENT=evalv3=1
exec cue trim -o - ./...
cmp stdout expect-stdout-v3

# -- evalv2 --
env CUE_EXPERIMENT=evalv3=0
exec cue trim -o - ./...
cmp stdout expect-stdout-v2

-- a.cue --
package p

x: y: int
x: z: int
-- b/b.cue --
package p

x: y: 7
-- c/c.cue --
package p

x: z: 6
-- expect-stdout-v3 --
package p

x: y: int
x: z: int
package p

x: y: int
x: z: int
package p

x: y: 7
package p

x: y: int
x: z: int
package p

x: z: 6
-- expect-stdout-v2 --
package p

x: y: int
x: z: int
package p

x: y: int
x: z: int
package p

x: y: 7
package p

x: y: int
x: z: int
package p

x: z: 6
3 changes: 3 additions & 0 deletions internal/core/subsume/vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ func (s *subsumer) verticesDev(x, y *adt.Vertex) bool {
return true
}

case nil:
return false

default:
panic(fmt.Sprintf("unexpected type %T", v))
}
Expand Down
10 changes: 10 additions & 0 deletions tools/trim/testdata/1.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TODO. This should get simplified in the same way as 6, but
doesn't. Possible bug in evaluator.

-- a.cue --
<10
7
-- out/trim --
== a.cue
<10
7
38 changes: 38 additions & 0 deletions tools/trim/testdata/10.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Similar to 9, but with list instead of struct. Note the simplification
of the pattern too.

See also 60.

-- a.cue --
d: [
{name: "jack", age: 5},
{name: "gill", age: >4},
{name: "hilbert", age: int},
]
d: [...{name: string, age: 5, age: number}]
-- out/trim-v3 --
== a.cue
d: [
{name: "jack"},
{name: "gill"},
{name: "hilbert"},
]
d: [...{name: string, age: 5}]
-- diff/-out/trim-v3<==>+out/trim --
diff old new
--- old
+++ new
@@ -4,4 +4,4 @@
{name: "gill"},
{name: "hilbert"},
]
-d: [...{name: string, age: 5, age: number}]
+d: [...{name: string, age: 5}]
-- out/trim --
== a.cue
d: [
{name: "jack"},
{name: "gill"},
{name: "hilbert"},
]
d: [...{name: string, age: 5, age: number}]
19 changes: 19 additions & 0 deletions tools/trim/testdata/11.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Explicit unification of structs can lead to simplification.
Contrast with 4.

-- a.cue --
x: {a: bool, b: "hi"} & {a: true, b: string}
-- out/trim-v3 --
== a.cue
x: {b: "hi"} & {a: true}
-- diff/-out/trim-v3<==>+out/trim --
diff old new
--- old
+++ new
@@ -1,2 +1,2 @@
== a.cue
-x: {a: bool, b: "hi"} & {a: true, b: string}
+x: {b: "hi"} & {a: true}
-- out/trim --
== a.cue
x: {a: bool, b: "hi"} & {a: true, b: string}
19 changes: 19 additions & 0 deletions tools/trim/testdata/12.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
A variant of 11, with fields slightly rearranged. We do not currently
simplify explicit unification where one side is top.

-- a.cue --
x: {a: bool, b: string} & {a: true, b: "hi"}
-- out/trim-v3 --
== a.cue
x: _ & {a: true, b: "hi"}
-- diff/-out/trim-v3<==>+out/trim --
diff old new
--- old
+++ new
@@ -1,2 +1,2 @@
== a.cue
-x: {a: bool, b: string} & {a: true, b: "hi"}
+x: _ & {a: true, b: "hi"}
-- out/trim --
== a.cue
x: {a: bool, b: string} & {a: true, b: "hi"}
19 changes: 19 additions & 0 deletions tools/trim/testdata/13.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
A variant of 12, to ensure the ellipsis does not prevent
simplification of the struct to top.

-- a.cue --
x: {a: bool, b: string, ...} & {a: true, b: "hi"}
-- out/trim-v3 --
== a.cue
x: _ & {a: true, b: "hi"}
-- diff/-out/trim-v3<==>+out/trim --
diff old new
--- old
+++ new
@@ -1,2 +1,2 @@
== a.cue
-x: {a: bool, b: string, ...} & {a: true, b: "hi"}
+x: _ & {a: true, b: "hi"}
-- out/trim --
== a.cue
x: {a: bool, b: string, ...} & {a: true, b: "hi"}
19 changes: 19 additions & 0 deletions tools/trim/testdata/14.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Further variation on 11: make sure patterns are kept, even if they're
not used.

-- a.cue --
x: {a: bool, b: string, [>"b"]: int} & {a: true, b: "hi"}
-- out/trim-v3 --
== a.cue
x: {[>"b"]: int} & {a: true, b: "hi"}
-- diff/-out/trim-v3<==>+out/trim --
diff old new
--- old
+++ new
@@ -1,2 +1,2 @@
== a.cue
-x: {a: bool, b: string, [>"b"]: int} & {a: true, b: "hi"}
+x: {[>"b"]: int} & {a: true, b: "hi"}
-- out/trim --
== a.cue
x: {a: bool, b: string, [>"b"]: int} & {a: true, b: "hi"}
19 changes: 19 additions & 0 deletions tools/trim/testdata/15.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Similar to 11, but with lists: explicit unification of lists can lead
to simplification.

-- a.cue --
x: [int, int, int] & [9, 8, 7]
-- out/trim-v3 --
== a.cue
x: [_, _, _] & [9, 8, 7]
-- diff/-out/trim-v3<==>+out/trim --
diff old new
--- old
+++ new
@@ -1,2 +1,2 @@
== a.cue
-x: [int, int, int] & [9, 8, 7]
+x: [_, _, _] & [9, 8, 7]
-- out/trim --
== a.cue
x: [int, int, int] & [9, 8, 7]
25 changes: 25 additions & 0 deletions tools/trim/testdata/16.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Like 11, but using a let comprehension. For trim, the let
comprehension is treated the same as a field. I.e. in this case, it's
no different to s: {a: bool, b: "hi"}, and so no simplification can
occur.

-- a.cue --
let s = {a: bool, b: "hi"}
x: s & {a: true, b: string}
-- out/trim-v3 --
== a.cue
let s = {a: bool, b: "hi"}
x: s & {a: true}
-- diff/-out/trim-v3<==>+out/trim --
diff old new
--- old
+++ new
@@ -1,3 +1,3 @@
== a.cue
let s = {a: bool, b: "hi"}
-x: s & {a: true, b: string}
+x: s & {a: true}
-- out/trim --
== a.cue
let s = {a: bool, b: "hi"}
x: s & {a: true, b: string}
42 changes: 42 additions & 0 deletions tools/trim/testdata/17.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Variant of 9, but with more complex field expressions. As before, we
must ignore the conjuncts from the pattern when simplifying the
fields, so that we don't accidentally remove the entire field.

-- a.cue --
d: {
jack: {age: 5, age: 5}
gill: {age: 5}
hilbert: {age: 5} & {age: 5}
[string]: {age: 5}
}
-- out/trim-v3 --
== a.cue
d: {
jack: _
gill: _
hilbert: _ & _
[string]: {age: 5}
}
-- diff/-out/trim-v3<==>+out/trim --
diff old new
--- old
+++ new
@@ -1,7 +1,7 @@
== a.cue
d: {
- jack: {}
- gill: {}
- hilbert: {} & {}
+ jack: _
+ gill: _
+ hilbert: _ & _
[string]: {age: 5}
}
-- out/trim --
== a.cue
d: {
jack: {}
gill: {}
hilbert: {} & {}
[string]: {age: 5}
}
6 changes: 6 additions & 0 deletions tools/trim/testdata/18.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Make sure we keep patterns for lists too.
-- a.cue --
x: [...string]
-- out/trim --
== a.cue
x: [...string]
6 changes: 6 additions & 0 deletions tools/trim/testdata/19.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Make sure we keep *all* of a pattern, not just its root.
-- a.cue --
x: [string]: a: b: c: d: _
-- out/trim --
== a.cue
x: [string]: a: b: c: d: _
10 changes: 10 additions & 0 deletions tools/trim/testdata/2.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TODO: This should get simplified in the same way as 3, but
doesn't. Possible bug in evaluator.

-- a.cue --
string
"hi"
-- out/trim --
== a.cue
string
"hi"
29 changes: 29 additions & 0 deletions tools/trim/testdata/20.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Simplifications can occur within definitions, but we must be sure to
keep the ellipsis.

-- a.cue --
let #x = {a: int, b: {c: 5, c: >4, ...}}
y: #x & {a: 4, b: {c: int}}
z: #x & {a: 4, b: {c: int, d: "hi"}}
-- out/trim-v3 --
== a.cue
let #x = {a: int, b: {c: 5, ...}}
y: #x & {a: 4}
z: #x & {a: 4, b: {d: "hi"}}
-- diff/-out/trim-v3<==>+out/trim --
diff old new
--- old
+++ new
@@ -1,4 +1,4 @@
== a.cue
-let #x = {a: int, b: {c: 5, c: >4, ...}}
-y: #x & {a: 4, b: {c: int}}
-z: #x & {a: 4, b: {c: int, d: "hi"}}
+let #x = {a: int, b: {c: 5, ...}}
+y: #x & {a: 4}
+z: #x & {a: 4, b: {d: "hi"}}
-- out/trim --
== a.cue
let #x = {a: int, b: {c: 5, c: >4, ...}}
y: #x & {a: 4, b: {c: int}}
z: #x & {a: 4, b: {c: int, d: "hi"}}
10 changes: 10 additions & 0 deletions tools/trim/testdata/21.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
We do no simplification of arguments to function calls. This includes
close, which appears in the AST as a normal CallExpr.

-- a.cue --
y: close({a: int, b: {c: string}}) & {a: 4, b: {c: "hi"}}
z: close({a: int, b: {c: string}} & {a: 4, b: {c: "hi"}})
-- out/trim --
== a.cue
y: close({a: int, b: {c: string}}) & {a: 4, b: {c: "hi"}}
z: close({a: int, b: {c: string}} & {a: 4, b: {c: "hi"}})
27 changes: 27 additions & 0 deletions tools/trim/testdata/22.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
As with 21, make sure we don't do simplification of arguments to
function calls - we've not just special-cased "close".

-- a.cue --
let x = close
y: x({a: 5})
z: y & {a: int}
-- out/trim-v3 --
== a.cue
let x = close
y: x({a: 5})
z: y & _
-- diff/-out/trim-v3<==>+out/trim --
diff old new
--- old
+++ new
@@ -1,4 +1,4 @@
== a.cue
let x = close
y: x({a: 5})
-z: y & {a: int}
+z: y & _
-- out/trim --
== a.cue
let x = close
y: x({a: 5})
z: y & {a: int}
5 changes: 5 additions & 0 deletions tools/trim/testdata/23.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- a.cue --
x: (2 + 3) & (1 + 4)
-- out/trim --
== a.cue
x: (2 + 3) & (1 + 4)
Loading

0 comments on commit 9938e1c

Please sign in to comment.