compiler: copy structs when passing as arguments

In Go structs must be copied when used as arguments.
To do so we must clone struct on VM level.
This is done by appending this struct to an intermediate array.
This commit is contained in:
Evgenii Stratonikov 2020-08-05 09:33:18 +03:00
parent d54c60ded3
commit f2107bfbc4
2 changed files with 50 additions and 0 deletions

View file

@ -28,3 +28,41 @@ func TestPointerDereference(t *testing.T) {
func setA(s Foo, a int) { s.A = a }`
eval(t, src, big.NewInt(4))
}
func TestStructArgCopy(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
src := `package foo
type Foo struct { A int }
func Main() int {
f := Foo{A: 4}
setA(f, 3)
return f.A
}
func setA(s Foo, a int) { s.A = a }`
eval(t, src, big.NewInt(4))
})
t.Run("StructField", func(t *testing.T) {
src := `package foo
type Bar struct { A int }
type Foo struct { B Bar }
func Main() int {
f := Foo{B: Bar{A: 4}}
setA(f, 3)
return f.B.A
}
func setA(s Foo, a int) { s.B.A = a }`
eval(t, src, big.NewInt(4))
})
t.Run("StructPointerField", func(t *testing.T) {
src := `package foo
type Bar struct { A int }
type Foo struct { B *Bar }
func Main() int {
f := Foo{B: &Bar{A: 4}}
setA(f, 3)
return f.B.A
}
func setA(s Foo, a int) { s.B.A = a }`
eval(t, src, big.NewInt(3))
})
}