Skip to content

Commit

Permalink
interp: handle explicit nil values in literal composite values
Browse files Browse the repository at this point in the history
Fixes #922.
  • Loading branch information
mvertes committed Oct 27, 2020
1 parent 513f5e3 commit 7f8ffa6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 15 additions & 0 deletions _test/composite14.go
@@ -0,0 +1,15 @@
package main

import "fmt"

type T struct {
b []byte
}

func main() {
t := T{nil}
fmt.Println(t)
}

// Output:
// {[]}
14 changes: 9 additions & 5 deletions interp/run.go
Expand Up @@ -2104,16 +2104,20 @@ func doComposite(n *node, hasType bool, keyed bool) {
val = c
fieldIndex = i
}
convertLiteralValue(val, typ.field[fieldIndex].typ.TypeOf())
ft := typ.field[fieldIndex].typ
rft := ft.TypeOf()
convertLiteralValue(val, rft)
switch {
case val.typ.cat == nilT:
values[fieldIndex] = func(*frame) reflect.Value { return reflect.New(rft).Elem() }
case val.typ.cat == funcT:
values[fieldIndex] = genFunctionWrapper(val)
case isArray(val.typ) && val.typ.val != nil && val.typ.val.cat == interfaceT:
values[fieldIndex] = genValueInterfaceArray(val)
case isRecursiveType(typ.field[fieldIndex].typ, typ.field[fieldIndex].typ.rtype):
values[fieldIndex] = genValueRecursiveInterface(val, typ.field[fieldIndex].typ.rtype)
case isInterface(typ.field[fieldIndex].typ):
values[fieldIndex] = genInterfaceWrapper(val, typ.field[fieldIndex].typ.rtype)
case isRecursiveType(ft, rft):
values[fieldIndex] = genValueRecursiveInterface(val, rft)
case isInterface(ft):
values[fieldIndex] = genInterfaceWrapper(val, rft)
default:
values[fieldIndex] = genValue(val)
}
Expand Down

0 comments on commit 7f8ffa6

Please sign in to comment.