Skip to content

Commit 6341362

Browse files
committed
internal/astinternal: use reflect.Value.IsNil consistently
The code was using a mix of IsValid and IsNil checks on reflect.Value; use IsNil consistently. This is a few more lines of code, but it's also more obvious. While here, make the comments a bit clearer. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I8fcd0f2cb43e9861807a4eccd78de4e66f6a419b Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200889 Reviewed-by: Roger Peppe <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent f4f38bf commit 6341362

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

internal/astinternal/debug.go

+19-16
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,26 @@ func (d *debugPrinter) value0(v reflect.Value, impliedType reflect.Type) {
7171
if d.cfg.Filter != nil && !d.cfg.Filter(v) {
7272
return
7373
}
74-
// Skip over interface types.
75-
if v.Kind() == reflect.Interface {
74+
// Skip over interfaces and pointers, stopping early if nil.
75+
concreteType := v.Type()
76+
for {
77+
k := v.Kind()
78+
if k != reflect.Interface && k != reflect.Pointer {
79+
break
80+
}
81+
if v.IsNil() {
82+
if !d.cfg.OmitEmpty {
83+
d.printf("nil")
84+
}
85+
return
86+
}
7687
v = v.Elem()
77-
}
78-
// Indirecting a nil interface gives a zero value. We
79-
// can also have a nil pointer inside a non-nil interface.
80-
if !v.IsValid() || (v.Kind() == reflect.Pointer && v.IsNil()) {
81-
if !d.cfg.OmitEmpty {
82-
d.printf("nil")
88+
if k == reflect.Interface {
89+
// For example, *ast.Ident can be the concrete type behind an ast.Expr.
90+
concreteType = v.Type()
8391
}
84-
return
8592
}
8693

87-
// We print the original pointer type if there was one.
88-
origType := v.Type()
89-
90-
v = reflect.Indirect(v)
91-
9294
if d.cfg.OmitEmpty && v.IsZero() {
9395
return
9496
}
@@ -121,8 +123,9 @@ func (d *debugPrinter) value0(v reflect.Value, impliedType reflect.Type) {
121123

122124
case reflect.Slice, reflect.Struct:
123125
valueStart := d.pos()
124-
if origType != impliedType {
125-
d.printf("%s", origType)
126+
// We print the concrete type when it differs from an implied type.
127+
if concreteType != impliedType {
128+
d.printf("%s", concreteType)
126129
}
127130
d.printf("{")
128131
d.level++

0 commit comments

Comments
 (0)