Skip to content

Commit 76ea22c

Browse files
committed
internal/core/adt: add Assert for debugging
Change-Id: I0875cd65aa388997990793e3953032b0b9b89a51 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7762 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 110d0bf commit 76ea22c

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

internal/core/adt/context.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package adt
1616

1717
import (
1818
"fmt"
19+
"os"
1920
"reflect"
2021
"regexp"
2122

@@ -28,6 +29,36 @@ import (
2829
"cuelang.org/go/cue/token"
2930
)
3031

32+
// Debug sets whether extra aggressive checking should be done.
33+
// This should typically default to true for pre-releases and default to
34+
// false otherwise.
35+
var Debug bool = os.Getenv("CUE_DEBUG") != "0"
36+
37+
// Assert panics if the condition is false. Assert can be used to check for
38+
// conditions that are considers to break an internal variant or unexpected
39+
// condition, but that nonetheless probably will be handled correctly down the
40+
// line. For instance, a faulty condition could lead to to error being caught
41+
// down the road, but resulting in an inaccurate error message. In production
42+
// code it is better to deal with the bad error message than to panic.
43+
//
44+
// It is advisable for each use of Assert to document how the error is expected
45+
// to be handled down the line.
46+
func Assert(name string, b bool) {
47+
if Debug && !b {
48+
panic("assertion failed: " + name)
49+
}
50+
}
51+
52+
// Assertf either panics or reports an error to c if the condition is not met.
53+
func (c *OpContext) Assertf(pos token.Pos, b bool, format string, args ...interface{}) {
54+
if !b {
55+
if Debug {
56+
panic(fmt.Sprintf("assertion failed: "+format, args...))
57+
}
58+
c.addErrf(0, pos, format, args...)
59+
}
60+
}
61+
3162
// A Unifier implements a strategy for CUE's unification operation. It must
3263
// handle the following aspects of CUE evaluation:
3364
//
@@ -658,6 +689,10 @@ func (c *OpContext) node(x Expr, scalar bool) *Vertex {
658689
return emptyNode
659690

660691
case *StructMarker, *ListMarker:
692+
if node == nil {
693+
Assert("unexpected markers with nil node", false)
694+
return emptyNode
695+
}
661696

662697
default:
663698
if v.Kind()&StructKind != 0 {

internal/core/eval/eval.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,13 +1068,14 @@ type envList struct {
10681068

10691069
func (n *nodeContext) addBottom(b *adt.Bottom) {
10701070
n.errs = adt.CombineErrors(nil, n.errs, b)
1071+
// TODO(errors): consider doing this
1072+
// n.kindExpr = n.errs
1073+
// n.kind = 0
10711074
}
10721075

10731076
func (n *nodeContext) addErr(err errors.Error) {
10741077
if err != nil {
1075-
n.errs = adt.CombineErrors(nil, n.errs, &adt.Bottom{
1076-
Err: err,
1077-
})
1078+
n.addBottom(&adt.Bottom{Err: err})
10781079
}
10791080
}
10801081

0 commit comments

Comments
 (0)