@@ -16,6 +16,7 @@ package adt
1616
1717import (
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 {
0 commit comments