Skip to content

Commit

Permalink
internal/core/adt: use setBaseValue to set Vertex.BaseValue
Browse files Browse the repository at this point in the history
Having a single point of entry through which BaseValue
is set helps debugging.

evalv2 also needed to update the status while setting
this value. This is no longer necessary for evalv3. To
keep things simple, we only ensure that for evalv3 code
uses this entry point.

Note that the method is defined on nodeContext. This
additionally enforces that a modification can only be
made while a Vertex is actively being evaluated.

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: I3f10a1e47309a2808f873806c7625dc7c7012247
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200791
Reviewed-by: Matthew Sackman <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
mpvl committed Sep 6, 2024
1 parent 3988f4f commit abb6364
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 18 deletions.
12 changes: 12 additions & 0 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,18 @@ func (v *Vertex) setValue(ctx *OpContext, state vertexStatus, value BaseValue) *
return nil
}

func (n *nodeContext) setBaseValue(value BaseValue) {
n.node.BaseValue = value
}

// swapBaseValue swaps the BaseValue of a node with the given value and returns
// the previous value.
func (n *nodeContext) swapBaseValue(value BaseValue) (saved BaseValue) {
saved = n.node.BaseValue
n.setBaseValue(value)
return saved
}

// ToVertex wraps v in a new Vertex, if necessary.
func ToVertex(v Value) *Vertex {
switch x := v.(type) {
Expand Down
2 changes: 1 addition & 1 deletion internal/core/adt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ func (c *OpContext) evalState(v Expr, state combinedFlags) (result Value) {
}
err := c.Newf("cycle with field %v", x)
b := &Bottom{Code: CycleError, Err: err}
v.setValue(c, v.status, b)
s.setBaseValue(b)
return b
// TODO: use this instead, as is usual for incomplete errors,
// and also move this block one scope up to also apply to
Expand Down
4 changes: 2 additions & 2 deletions internal/core/adt/cycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,14 +611,14 @@ func assertStructuralCycle(n *nodeContext) bool {
}

func (n *nodeContext) reportCycleError() {
n.node.BaseValue = CombineErrors(nil,
n.setBaseValue(CombineErrors(nil,
n.node.Value(),
&Bottom{
Code: StructuralCycleError,
Err: n.ctx.Newf("structural cycle"),
Value: n.node.Value(),
// TODO: probably, this should have the referenced arc.
})
}))
n.node.Arcs = nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/core/adt/disjunct.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (n *nodeContext) expandDisjuncts(
n.disjuncts = append(n.disjuncts, n)
}
if n.node.BaseValue == nil {
n.node.BaseValue = n.getValidators(state)
n.setBaseValue(n.getValidators(state))
}

n.usedDefault = append(n.usedDefault, defaultInfo{
Expand Down
8 changes: 3 additions & 5 deletions internal/core/adt/disjunct2.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (n *nodeContext) processDisjunctions() *Bottom {

case 1:
d := cross[0].node
n.node.BaseValue = d
n.setBaseValue(d)
n.defaultMode = cross[0].defaultMode

default:
Expand Down Expand Up @@ -419,9 +419,7 @@ func (n *nodeContext) doDisjunct(c Conjunct, m defaultMode, mode runMode) (*node

v := d.node

saved := n.node.BaseValue
n.node.BaseValue = v
defer func() { n.node.BaseValue = saved }()
defer n.setBaseValue(n.swapBaseValue(v))

// Clear relevant scheduler states.
// TODO: do something more principled: just ensure that a node that has
Expand Down Expand Up @@ -487,7 +485,7 @@ func (n *nodeContext) finalizeDisjunctions() {
}

v := n.node
v.BaseValue = d
n.setBaseValue(d)

// The conjuncts will have too much information. Better have no
// information than incorrect information.
Expand Down
9 changes: 5 additions & 4 deletions internal/core/adt/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,22 @@ func isIncomplete(v *Vertex) bool {
//
// If x is not already an error, the value is recorded in the error for
// reference.
func (v *Vertex) AddChildError(recursive *Bottom) {
func (n *nodeContext) AddChildError(recursive *Bottom) {
v := n.node
v.ChildErrors = CombineErrors(nil, v.ChildErrors, recursive)
if recursive.IsIncomplete() {
return
}
x := v.BaseValue
err, _ := x.(*Bottom)
if err == nil {
v.BaseValue = &Bottom{
n.setBaseValue(&Bottom{
Code: recursive.Code,
Value: v,
HasRecursive: true,
ChildError: true,
Err: recursive.Err,
}
})
return
}

Expand All @@ -154,7 +155,7 @@ func (v *Vertex) AddChildError(recursive *Bottom) {
err.Code = recursive.Code
}

v.BaseValue = err
n.setBaseValue(err)
}

// CombineErrors combines two errors that originate at the same Vertex.
Expand Down
2 changes: 1 addition & 1 deletion internal/core/adt/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ func (n *nodeContext) completeArcs(state vertexStatus) {
}

if err := a.Bottom(); err != nil {
n.node.AddChildError(err)
n.AddChildError(err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/core/adt/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (n *nodeContext) updateListType(list Expr, id CloseInfo, isClosed bool, ell
m = &ListMarker{
IsOpen: true,
}
n.node.setValue(n.ctx, conjuncts, m)
n.setBaseValue(m)
}
m.IsOpen = m.IsOpen && !isClosed

Expand Down
6 changes: 3 additions & 3 deletions internal/core/adt/unify.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (v *Vertex) unify(c *OpContext, needs condition, mode runMode) bool {
if b := n.node.Bottom(); b != nil {
err = CombineErrors(nil, b, err)
}
n.node.BaseValue = err
n.setBaseValue(err)
}

if mode == attemptOnly {
Expand Down Expand Up @@ -434,7 +434,7 @@ func (n *nodeContext) updateScalar() {
// Set BaseValue to scalar, but only if it was not set before. Most notably,
// errors should not be discarded.
if n.scalar != nil && (!n.node.IsErr() || isCyclePlaceholder(n.node.BaseValue)) {
n.node.BaseValue = n.scalar
n.setBaseValue(n.scalar)
n.signal(scalarKnown)
}
}
Expand Down Expand Up @@ -521,7 +521,7 @@ func (n *nodeContext) completeAllArcs(needs condition, mode runMode) bool {
if !a.Label.IsLet() && a.ArcType <= ArcRequired {
a := a.DerefValue()
if err := a.Bottom(); err != nil {
n.node.AddChildError(err)
n.AddChildError(err)
}
success = true // other arcs are irrelevant
}
Expand Down

0 comments on commit abb6364

Please sign in to comment.