Skip to content

Commit

Permalink
internal/core: search harder for BinExprs in toposort
Browse files Browse the repository at this point in the history
Finding explicit unifications (`a & b`) is essential for correct
construction of the graph of nodes, which is central to toposort. By
searching through a vertex's structinfo's Decls, we can find more
BinExprs, and so build more correct graphs.

Signed-off-by: Matthew Sackman <matthew@cue.works>
Change-Id: Id19b772617b59853e38b4291560e9689b6e2e111
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1207585
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
cuematthew committed Jan 22, 2025
1 parent 6ada21c commit 9caec6f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
8 changes: 4 additions & 4 deletions internal/core/toposort/testdata/t.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ k2: {
t: 10
}
d6: {
z: 1
y: 2
x: 3
w: 4
z: 1
y: 2
za: 11
t: 10
}
Expand Down Expand Up @@ -414,10 +414,10 @@ diff old new
- w: 4
- z: 1
- y: 2
+ z: 1
+ y: 2
+ x: 3
+ w: 4
+ z: 1
+ y: 2
+ za: 11
t: 10
}
Expand Down
44 changes: 27 additions & 17 deletions internal/core/toposort/vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,29 +291,39 @@ func analyseStructs(v *adt.Vertex, builder *GraphBuilder) ([]*structMeta, map[ad
})
}

// Explore our own conjuncts to find explicit unifications and
// record as appropriate in the structMetas.
// Explore our own conjuncts, and the decls from our StructList, to
// find explicit unifications, and mark structMetas accordingly.
var worklist []adt.Expr
v.VisitLeafConjuncts(func(c adt.Conjunct) bool {
debug("self conjunct field %p :: %T, expr %p :: %T\n",
c.Field(), c.Field(), c.Expr(), c.Expr())
worklist := []adt.Expr{c.Expr()}
for len(worklist) != 0 {
expr := worklist[0]
worklist = worklist[1:]

binExpr, ok := expr.(*adt.BinaryExpr)
if !ok || binExpr.Op != adt.AndOp {
continue
worklist = append(worklist, c.Expr())
return true
})
for _, si := range structInfos {
for _, decl := range si.StructLit.Decls {
if expr, ok := decl.(adt.Expr); ok {
worklist = append(worklist, expr)
}
for _, expr := range []adt.Expr{binExpr.X, binExpr.Y} {
for _, sMeta := range nodeToStructMeta[expr] {
sMeta.isExplicit = true
}
}
}

for len(worklist) != 0 {
expr := worklist[0]
worklist = worklist[1:]

binExpr, ok := expr.(*adt.BinaryExpr)
if !ok || binExpr.Op != adt.AndOp {
continue
}
for _, expr := range []adt.Expr{binExpr.X, binExpr.Y} {
for _, sMeta := range nodeToStructMeta[expr] {
sMeta.isExplicit = true
debug(" now explicit: %v\n", sMeta)
}
worklist = append(worklist, binExpr.X, binExpr.Y)
}
return true
})
worklist = append(worklist, binExpr.X, binExpr.Y)
}

return roots, outgoing
}
Expand Down

0 comments on commit 9caec6f

Please sign in to comment.