cue: fix top shortcut
A kind representing all values (topKind) may not be
an actual top value.
Change-Id: Iba8aa966d19ecc2ce53c89a88349f2bd478466b0
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2663
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cue/go.go b/cue/go.go
index adf67bc..11ba339 100644
--- a/cue/go.go
+++ b/cue/go.go
@@ -193,7 +193,7 @@
case nil:
// Interpret a nil pointer as an undefined value that is only
// null by default, but may still be set: *null | _.
- return makeNullable(&top{src.base()}).(evaluated)
+ return makeNullable(&top{src.base()}, false).(evaluated)
case ast.Expr:
x := newVisitorCtx(ctx, nil, nil, nil)
@@ -297,7 +297,8 @@
if value.IsNil() {
// Interpret a nil pointer as an undefined value that is only
// null by default, but may still be set: *null | _.
- return makeNullable(&top{src.base()}).(evaluated)
+ elem := goTypeToValue(ctx, false, reflect.TypeOf(v).Elem())
+ return makeNullable(elem, false).(evaluated)
}
return convert(ctx, src, value.Elem().Interface())
@@ -567,12 +568,10 @@
if e.kind().isAnyOf(nullKind) {
return e
}
- return makeNullable(e)
+ return makeNullable(e, true)
}
-const nullIsDefault = true
-
-func makeNullable(e value) value {
+func makeNullable(e value, nullIsDefault bool) value {
return &disjunction{
baseValue: baseValue{e},
values: []dValue{
diff --git a/cue/go_test.go b/cue/go_test.go
index b608fe4..c632f8a 100644
--- a/cue/go_test.go
+++ b/cue/go_test.go
@@ -33,7 +33,7 @@
goVal interface{}
want string
}{{
- nil, "(*null | _)",
+ nil, "(null | _)",
}, {
true, "true",
}, {
@@ -112,7 +112,7 @@
}, {
&struct{ A int }{3}, "<0>{A: 3}",
}, {
- (*struct{ A int })(nil), "(*null | _)",
+ (*struct{ A int })(nil), "(null | <0>{A: (int & >=-9223372036854775808 & int & <=9223372036854775807)})",
}, {
reflect.ValueOf(3), "3",
}, {
diff --git a/cue/kind.go b/cue/kind.go
index e078153..95e5277 100644
--- a/cue/kind.go
+++ b/cue/kind.go
@@ -76,7 +76,8 @@
)
func isTop(v value) bool {
- return v.kind() == topKind
+ _, ok := v.(*top)
+ return ok
}
// isDone means that the value will not evaluate further.