-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: don't pass value types by reference (#1263)
Addresses #1096 These changes ensure that passing non-primitive value types to a function do not result in the modification of the original value, even if the value type is represented as a pointer inside the VM.
- Loading branch information
Showing
3 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type X struct { | ||
Array [8]int | ||
Test bool | ||
} | ||
|
||
type Y [8]int | ||
|
||
func main() { | ||
x := X{} | ||
x.Array[1] = 888 | ||
println(x.Array[1]) | ||
println(x.Array[2]) | ||
println(x.Test) | ||
|
||
x.manip() | ||
println(x.Array[1]) | ||
println(x.Array[2]) | ||
println(x.Test) | ||
|
||
println("-----") | ||
|
||
y := Y{} | ||
y[1] = 888 | ||
println(y[1]) | ||
println(y[2]) | ||
|
||
y.manip() | ||
println(y[1]) | ||
println(y[2]) | ||
println("-----") | ||
|
||
x = X{} | ||
println(x.Array[1]) | ||
println(x.Array[2]) | ||
println(x.Test) | ||
|
||
x.Array[1] = 888 | ||
println(x.Array[1]) | ||
println(x.Array[2]) | ||
println(x.Test) | ||
|
||
manip(x) | ||
println(x.Array[1]) | ||
println(x.Array[2]) | ||
println(x.Test) | ||
} | ||
|
||
func (x X) manip() { | ||
x.Array[2] = 999 | ||
x.Test = true | ||
} | ||
|
||
func manip(x X) { | ||
x.Array[2] = 999 | ||
x.Test = true | ||
} | ||
|
||
func (y Y) manip() { | ||
y[2] = 111 | ||
} | ||
|
||
// Output: | ||
// 888 | ||
// 0 | ||
// false | ||
// 888 | ||
// 0 | ||
// false | ||
// ----- | ||
// 888 | ||
// 0 | ||
// 888 | ||
// 0 | ||
// ----- | ||
// 0 | ||
// 0 | ||
// false | ||
// 888 | ||
// 0 | ||
// false | ||
// 888 | ||
// 0 | ||
// false |