Skip to content

Commit

Permalink
internal/core/adt: add Assert for debugging
Browse files Browse the repository at this point in the history
Change-Id: I0875cd65aa388997990793e3953032b0b9b89a51
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7762
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Nov 27, 2020
1 parent 110d0bf commit 76ea22c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
35 changes: 35 additions & 0 deletions internal/core/adt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package adt

import (
"fmt"
"os"
"reflect"
"regexp"

Expand All @@ -28,6 +29,36 @@ import (
"cuelang.org/go/cue/token"
)

// Debug sets whether extra aggressive checking should be done.
// This should typically default to true for pre-releases and default to
// false otherwise.
var Debug bool = os.Getenv("CUE_DEBUG") != "0"

// Assert panics if the condition is false. Assert can be used to check for
// conditions that are considers to break an internal variant or unexpected
// condition, but that nonetheless probably will be handled correctly down the
// line. For instance, a faulty condition could lead to to error being caught
// down the road, but resulting in an inaccurate error message. In production
// code it is better to deal with the bad error message than to panic.
//
// It is advisable for each use of Assert to document how the error is expected
// to be handled down the line.
func Assert(name string, b bool) {
if Debug && !b {
panic("assertion failed: " + name)
}
}

// Assertf either panics or reports an error to c if the condition is not met.
func (c *OpContext) Assertf(pos token.Pos, b bool, format string, args ...interface{}) {
if !b {
if Debug {
panic(fmt.Sprintf("assertion failed: "+format, args...))
}
c.addErrf(0, pos, format, args...)
}
}

// A Unifier implements a strategy for CUE's unification operation. It must
// handle the following aspects of CUE evaluation:
//
Expand Down Expand Up @@ -658,6 +689,10 @@ func (c *OpContext) node(x Expr, scalar bool) *Vertex {
return emptyNode

case *StructMarker, *ListMarker:
if node == nil {
Assert("unexpected markers with nil node", false)
return emptyNode
}

default:
if v.Kind()&StructKind != 0 {
Expand Down
7 changes: 4 additions & 3 deletions internal/core/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,13 +1068,14 @@ type envList struct {

func (n *nodeContext) addBottom(b *adt.Bottom) {
n.errs = adt.CombineErrors(nil, n.errs, b)
// TODO(errors): consider doing this
// n.kindExpr = n.errs
// n.kind = 0
}

func (n *nodeContext) addErr(err errors.Error) {
if err != nil {
n.errs = adt.CombineErrors(nil, n.errs, &adt.Bottom{
Err: err,
})
n.addBottom(&adt.Bottom{Err: err})
}
}

Expand Down

0 comments on commit 76ea22c

Please sign in to comment.