Skip to content

Commit

Permalink
internal/core/adt: remove state arg from Err, SetValue, and UpdateValue
Browse files Browse the repository at this point in the history
The goal is to unexport all states from the internal API as we
are moving away from a model that uses states at all.

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I29350d469fbe112e21fde09c5d5ba5bc10abcbb9
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/551409
Reviewed-by: Aram Hăvărneanu <aram@cue.works>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
mpvl committed Mar 24, 2023
1 parent 7851142 commit 97904e3
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cue/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (c *Context) BuildInstance(i *build.Instance, options ...BuildOption) Value
func (c *Context) makeError(err errors.Error) Value {
b := &adt.Bottom{Err: err}
node := &adt.Vertex{BaseValue: b}
node.UpdateStatus(adt.Finalized)
node.ForceDone()
node.AddConjunct(adt.MakeRootConjunct(nil, b))
return c.make(node)
}
Expand Down
10 changes: 5 additions & 5 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ func newErrValue(v Value, b *adt.Bottom) Value {
node.Label = v.v.Label
node.Parent = v.v.Parent
}
node.UpdateStatus(adt.Finalized)
node.ForceDone()
node.AddConjunct(adt.MakeRootConjunct(nil, b))
return makeChildValue(v.parent(), node)
}
Expand All @@ -612,7 +612,7 @@ func newVertexRoot(idx *runtime.Runtime, ctx *adt.OpContext, x *adt.Vertex) Valu
// with an error value.
x.Finalize(ctx)
} else {
x.UpdateStatus(adt.Finalized)
x.ForceDone()
}
return makeValue(idx, x, nil)
}
Expand Down Expand Up @@ -699,7 +699,7 @@ func remakeValue(base Value, env *adt.Environment, v adt.Expr) Value {

func remakeFinal(base Value, env *adt.Environment, v adt.Value) Value {
n := &adt.Vertex{Parent: base.v.Parent, Label: base.v.Label, BaseValue: v}
n.UpdateStatus(adt.Finalized)
n.ForceDone()
return makeChildValue(base.parent(), n)
}

Expand Down Expand Up @@ -1864,7 +1864,7 @@ func (v Value) Unify(w Value) Value {
n.Label = v.v.Label
n.Closed = v.v.Closed || w.v.Closed

if err := n.Err(ctx, adt.Finalized); err != nil {
if err := n.Err(ctx); err != nil {
return makeValue(v.idx, n, v.parent_)
}
if err := allowed(ctx, v.v, n); err != nil {
Expand Down Expand Up @@ -1900,7 +1900,7 @@ func (v Value) UnifyAccept(w Value, accept Value) Value {
n.Parent = v.v.Parent
n.Label = v.v.Label

if err := n.Err(ctx, adt.Finalized); err != nil {
if err := n.Err(ctx); err != nil {
return makeValue(v.idx, n, v.parent_)
}
if err := allowed(ctx, accept.v, n); err != nil {
Expand Down
22 changes: 16 additions & 6 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,12 @@ func (v *Vertex) Status() VertexStatus {
return v.status
}

func (v *Vertex) UpdateStatus(s VertexStatus) {
// ForceDone prevents v from being evaluated.
func (v *Vertex) ForceDone() {
v.updateStatus(Finalized)
}

func (v *Vertex) updateStatus(s VertexStatus) {
Assertf(v.status <= s+1, "attempt to regress status from %d to %d", v.Status(), s)

if s == Finalized && v.BaseValue == nil {
Expand Down Expand Up @@ -565,8 +570,8 @@ func (v *Vertex) IsErr() bool {
return false
}

func (v *Vertex) Err(c *OpContext, state VertexStatus) *Bottom {
c.unify(v, state)
func (v *Vertex) Err(c *OpContext) *Bottom {
v.Finalize(c)
if b, ok := v.BaseValue.(*Bottom); ok {
return b
}
Expand All @@ -590,12 +595,17 @@ func (v *Vertex) CompleteArcs(c *OpContext) {
}

func (v *Vertex) AddErr(ctx *OpContext, b *Bottom) {
v.SetValue(ctx, Finalized, CombineErrors(nil, v.Value(), b))
v.SetValue(ctx, CombineErrors(nil, v.Value(), b))
}

// SetValue sets the value of a node.
func (v *Vertex) SetValue(ctx *OpContext, value BaseValue) *Bottom {
return v.setValue(ctx, Finalized, value)
}

func (v *Vertex) SetValue(ctx *OpContext, state VertexStatus, value BaseValue) *Bottom {
func (v *Vertex) setValue(ctx *OpContext, state VertexStatus, value BaseValue) *Bottom {
v.BaseValue = value
v.UpdateStatus(state)
v.updateStatus(state)
return 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 @@ -450,7 +450,7 @@ func (n *nodeContext) makeError() {
Code: code,
Err: n.disjunctError(),
}
n.node.SetValue(n.ctx, Finalized, b)
n.node.SetValue(n.ctx, b)
}

func mode(hasDefault, marked bool) defaultMode {
Expand Down
24 changes: 12 additions & 12 deletions internal/core/adt/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *OpContext) evaluate(v *Vertex, r Resolver, state VertexStatus) Value {
}
err := c.Newf("cycle with field %v", r)
b := &Bottom{Code: CycleError, Err: err}
v.SetValue(c, v.status, b)
v.setValue(c, v.status, 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 Expand Up @@ -200,7 +200,7 @@ func (c *OpContext) unify(v *Vertex, state VertexStatus) {

defer c.PopArc(c.PushArc(v))

v.UpdateStatus(Evaluating)
v.updateStatus(Evaluating)

if p := v.Parent; p != nil && p.state != nil && v.Label.IsString() {
for _, s := range p.state.node.Structs {
Expand Down Expand Up @@ -230,7 +230,7 @@ func (c *OpContext) unify(v *Vertex, state VertexStatus) {
n.conjuncts = v.Conjuncts
if n.insertConjuncts(state) {
n.maybeSetCache()
v.UpdateStatus(Partial)
v.updateStatus(Partial)
return
}

Expand Down Expand Up @@ -260,14 +260,14 @@ func (c *OpContext) unify(v *Vertex, state VertexStatus) {
if !n.done() {
switch {
case state < Conjuncts:
n.node.UpdateStatus(Partial)
n.node.updateStatus(Partial)
return

case state == Conjuncts:
if err := n.incompleteErrors(true); err != nil && err.Code < CycleError {
n.node.AddErr(c, err)
} else {
n.node.UpdateStatus(Partial)
n.node.updateStatus(Partial)
}
return
}
Expand Down Expand Up @@ -348,7 +348,7 @@ func (c *OpContext) unify(v *Vertex, state VertexStatus) {
}

// Free memory here?
v.UpdateStatus(Finalized)
v.updateStatus(Finalized)

case Finalized:
}
Expand Down Expand Up @@ -710,7 +710,7 @@ func (n *nodeContext) checkClosed(state VertexStatus) bool {
// conflicts at the appropriate place, to allow valid fields to
// be represented normally and, most importantly, to avoid
// recursive processing of a disallowed field.
v.SetValue(ctx, Finalized, err)
v.SetValue(ctx, err)
return false
}
}
Expand All @@ -736,11 +736,11 @@ func (n *nodeContext) completeArcs(state VertexStatus) {
n.node.status <= state+1 &&
(!n.node.hasVoidArc || n.node.ArcType == ArcMember) {

n.node.UpdateStatus(Conjuncts)
n.node.updateStatus(Conjuncts)
return
}

n.node.UpdateStatus(EvaluatingArcs)
n.node.updateStatus(EvaluatingArcs)

ctx := n.ctx

Expand All @@ -750,7 +750,7 @@ func (n *nodeContext) completeArcs(state VertexStatus) {
for _, a := range n.node.Arcs {
// Call UpdateStatus here to be absolutely sure the status is set
// correctly and that we are not regressing.
n.node.UpdateStatus(EvaluatingArcs)
n.node.updateStatus(EvaluatingArcs)

wasVoid := a.ArcType == ArcVoid

Expand Down Expand Up @@ -853,7 +853,7 @@ func (n *nodeContext) completeArcs(state VertexStatus) {
}
n.node.Structs = n.node.Structs[:k]

n.node.UpdateStatus(Finalized)
n.node.updateStatus(Finalized)
}

// TODO: this is now a sentinel. Use a user-facing error that traces where
Expand Down Expand Up @@ -2287,7 +2287,7 @@ outer:
}

if m, ok := n.node.BaseValue.(*ListMarker); !ok {
n.node.SetValue(c, Partial, &ListMarker{
n.node.setValue(c, Partial, &ListMarker{
Src: ast.NewBinExpr(token.AND, sources...),
IsOpen: isOpen,
})
Expand Down
2 changes: 1 addition & 1 deletion internal/core/adt/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,7 @@ func (x *ForClause) yield(s *compState) {
v := &Vertex{Label: x.Key}
key := a.Label.ToValue(c)
v.AddConjunct(MakeRootConjunct(c.Env(0), key))
v.SetValue(c, Finalized, key)
v.SetValue(c, key)
n.Arcs = append(n.Arcs, v)
}

Expand Down
8 changes: 4 additions & 4 deletions internal/core/convert/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
// There is no closedness or cycle info for Go structs, so we
// pass an empty CloseInfo.
v.AddStruct(obj, env, adt.CloseInfo{})
v.SetValue(ctx, adt.Finalized, &adt.StructMarker{})
v.SetValue(ctx, &adt.StructMarker{})

t := value.Type()
for i := 0; i < value.NumField(); i++ {
Expand Down Expand Up @@ -464,7 +464,7 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
arc.Label = f
} else {
arc = &adt.Vertex{Label: f, BaseValue: sub}
arc.UpdateStatus(adt.Finalized)
arc.ForceDone()
arc.AddConjunct(adt.MakeRootConjunct(nil, sub))
}
v.Arcs = append(v.Arcs, arc)
Expand All @@ -474,7 +474,7 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {

case reflect.Map:
v := &adt.Vertex{BaseValue: &adt.StructMarker{}}
v.SetValue(ctx, adt.Finalized, &adt.StructMarker{})
v.SetValue(ctx, &adt.StructMarker{})

t := value.Type()
switch key := t.Key(); key.Kind() {
Expand Down Expand Up @@ -518,7 +518,7 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
arc.Label = f
} else {
arc = &adt.Vertex{Label: f, BaseValue: sub}
arc.UpdateStatus(adt.Finalized)
arc.ForceDone()
arc.AddConjunct(adt.MakeRootConjunct(nil, sub))
}
v.Arcs = append(v.Arcs, arc)
Expand Down
2 changes: 1 addition & 1 deletion internal/core/dep/dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var empty *adt.Vertex
func init() {
// TODO: Consider setting a non-nil BaseValue.
empty = &adt.Vertex{}
empty.UpdateStatus(adt.Finalized)
empty.ForceDone()
}

func visit(c *adt.OpContext, n *adt.Vertex, f VisitFunc, all, top bool) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/core/export/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestGenerated(t *testing.T) {
}, {
in: func(r *adt.OpContext) (adt.Expr, error) {
v := &adt.Vertex{}
v.SetValue(r, adt.Finalized, &adt.StructMarker{})
v.SetValue(r, &adt.StructMarker{})
return v, nil
},
out: ``, // empty file
Expand Down
2 changes: 1 addition & 1 deletion internal/core/export/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var empty *adt.Vertex
func init() {
// TODO: Consider setting a non-nil BaseValue.
empty = &adt.Vertex{}
empty.UpdateStatus(adt.Finalized)
empty.ForceDone()
}

// innerExpr is like expr, but prohibits inlining in certain cases.
Expand Down
2 changes: 1 addition & 1 deletion internal/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Make(ctx *adt.OpContext, v adt.Value) cue.Value {
func MakeError(r *runtime.Runtime, err errors.Error) cue.Value {
b := &adt.Bottom{Err: err}
node := &adt.Vertex{BaseValue: b}
node.UpdateStatus(adt.Finalized)
node.ForceDone()
node.AddConjunct(adt.MakeRootConjunct(nil, b))
return (*cue.Context)(r).Encode(node)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (p *Package) MustCompile(ctx *adt.OpContext, importPath string) *adt.Vertex

// We could compile lazily, but this is easier for debugging.
obj.Finalize(ctx)
if err := obj.Err(ctx, adt.Finalized); err != nil {
if err := obj.Err(ctx); err != nil {
panic(err.Err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/list/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (s *valueSorter) Less(i, j int) bool {

s.less.Finalize(s.ctx)
isLess := s.ctx.BoolValue(s.less)
if b := s.less.Err(s.ctx, adt.Finalized); b != nil && s.err == nil {
if b := s.less.Err(s.ctx); b != nil && s.err == nil {
s.err = b.Err
return true
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/path/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var (

func newStr(s string) *adt.Vertex {
v := &adt.Vertex{}
v.SetValue(nil, adt.Finalized, &adt.String{Str: s})
v.SetValue(nil, &adt.String{Str: s})
return v
}

Expand Down

0 comments on commit 97904e3

Please sign in to comment.