compiler: initialize struct fields explicitly
By default VM initializes every field of a new struct to a Boolean. If field contains another struct, we need to initialize it to default value explicitly. This commit sets default values for uninitialized field.
This commit is contained in:
parent
051e3608ff
commit
5b0e73ddf0
3 changed files with 35 additions and 37 deletions
|
@ -368,6 +368,32 @@ var structTestCases = []testCase{
|
|||
}`,
|
||||
big.NewInt(11),
|
||||
},
|
||||
{
|
||||
"complex struct default value",
|
||||
`package foo
|
||||
type S1 struct { x S2 }
|
||||
type S2 struct { y S3 }
|
||||
type S3 struct { a int }
|
||||
func Main() int {
|
||||
var s1 S1
|
||||
s1.x.y.a = 11
|
||||
return s1.x.y.a
|
||||
}`,
|
||||
big.NewInt(11),
|
||||
},
|
||||
{
|
||||
"nested selectors (complex write)",
|
||||
`package foo
|
||||
type S1 struct { x S2 }
|
||||
type S2 struct { y, z S3 }
|
||||
type S3 struct { a int }
|
||||
func Main() int {
|
||||
var s1 S1
|
||||
s1.x.y.a, s1.x.z.a = 11, 31
|
||||
return s1.x.y.a + s1.x.z.a
|
||||
}`,
|
||||
big.NewInt(42),
|
||||
},
|
||||
}
|
||||
|
||||
func TestStructs(t *testing.T) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue