Skip to content

Commit

Permalink
internal/core/adt: use Vertex.Indirect in more places
Browse files Browse the repository at this point in the history
This prepares for several things, including the
implementation of disjunctions and structure sharing.

Issue #2851
Issue #2884
Issue #2854

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I831d5419e6b035667aae6f254f45456b34dfd93c
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1190799
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
mpvl committed Apr 10, 2024
1 parent 924178c commit cc2075b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,11 @@ func (v *Vertex) IsList() bool {
func (v *Vertex) Lookup(f Feature) *Vertex {
for _, a := range v.Arcs {
if a.Label == f {
// TODO(share): this indirection should ultimately be eliminated:
// the original node may have useful information (like original
// conjuncts) that are eliminated after indirection.
// We should leave it up to the user of Lookup at what point an
// indirection is necessary.
a = a.Indirect()
return a
}
Expand Down
6 changes: 6 additions & 0 deletions internal/core/adt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,9 @@ func (c *OpContext) evalState(v Expr, state combinedFlags) (result Value) {
if arc == nil {
return nil
}
// TODO: consider moving this after markCycle, depending on how we
// implement markCycle.
arc = arc.Indirect()

// Save the old CloseInfo and restore after evaluate to avoid detecting
// spurious cycles.
Expand Down Expand Up @@ -823,6 +826,9 @@ func (c *OpContext) unifyNode(v Expr, state combinedFlags) (result Value) {
if v == nil {
return nil
}
// TODO: consider moving this after markCycle, depending on how we
// implement markCycle.
v = v.Indirect()

if c.isDevVersion() {
if n := v.getState(c); n != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/core/adt/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func processResolver(ctx *OpContext, t *task, mode runMode) {
// TODO: yield instead?
return
}
// TODO: consider moving after markCycle or removing.
arc = arc.Indirect()

// A reference that points to itself indicates equality. In that case
// we are done computing and we can return the arc as is.
ci, skip := t.node.markCycle(arc, t.env, r, t.id)
Expand Down
13 changes: 13 additions & 0 deletions internal/core/adt/unify.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ func (v *Vertex) unify(c *OpContext, needs condition, mode runMode) bool {

nodeOnlyNeeds := needs &^ (subFieldsProcessed)
n.process(nodeOnlyNeeds, mode)
w := v.Indirect() // Dereference the disjunction result.
if w != v {
if w.Closed {
// Should resolve with dereference.
v.Closed = true
}
v.ArcType = w.ArcType
v.ChildErrors = CombineErrors(nil, v.ChildErrors, w.ChildErrors)
v.Arcs = nil
return w.state.meets(needs)
}
n.updateScalar()

// First process all but the subfields.
Expand Down Expand Up @@ -431,6 +442,8 @@ func (v *Vertex) lookup(c *OpContext, pos token.Pos, f Feature, flags combinedFl
needs := flags.conditions()
runMode := flags.runMode()

v = v.Indirect()

c.Logf(c.vertex, "LOOKUP %v", f)

state := v.getState(c)
Expand Down
1 change: 1 addition & 0 deletions internal/core/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func (w *printer) interpolation(x *adt.Interpolation) {
func (w *printer) node(n adt.Node) {
switch x := n.(type) {
case *adt.Vertex:
x = x.Indirect()
var kind adt.Kind
if x.BaseValue != nil {
kind = x.BaseValue.Kind()
Expand Down

0 comments on commit cc2075b

Please sign in to comment.