diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index c07ce7e8c..2328e8f50 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -324,14 +324,11 @@ func (c *codegen) emitDefault(t types.Type) { } case *types.Struct: num := t.NumFields() - emit.Int(c.prog.BinWriter, int64(num)) - emit.Opcodes(c.prog.BinWriter, opcode.NEWSTRUCT) - for i := 0; i < num; i++ { - emit.Opcodes(c.prog.BinWriter, opcode.DUP) - emit.Int(c.prog.BinWriter, int64(i)) + for i := num - 1; i >= 0; i-- { c.emitDefault(t.Field(i).Type()) - emit.Opcodes(c.prog.BinWriter, opcode.SETITEM) } + emit.Int(c.prog.BinWriter, int64(num)) + emit.Opcodes(c.prog.BinWriter, opcode.PACKSTRUCT) default: emit.Opcodes(c.prog.BinWriter, opcode.PUSHNULL) } diff --git a/pkg/compiler/struct_test.go b/pkg/compiler/struct_test.go index fe48e9429..046cdb629 100644 --- a/pkg/compiler/struct_test.go +++ b/pkg/compiler/struct_test.go @@ -394,6 +394,16 @@ var structTestCases = []testCase{ }`, big.NewInt(11), }, + { + "lengthy struct default value", + `package foo + type S struct { x int; y []byte; z bool } + func Main() int { + var s S + return s.x + }`, + big.NewInt(0), + }, { "nested selectors (complex write)", `package foo