-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Description
Consider this program:
package main
import "fmt"
func main() {
arr := []int{1, 2}
arr, arr[len(arr)-1] = arr[:len(arr)-1], 3
fmt.Println(arr)
}This currently prints [1], and in fact it prints [1] with all versions of Go since Go 1.
According to the spec, when evaluating an assignment statement, all function calls are evaluated in lexical left to right order. Also, all index expressions on the left are evaluated, and then the assignments are done in left to right order.
len is a function call, so all the calls to len should be evaluated before any assignments. This, the second assignment statement should be equivalent to
arr, arr[2-1] = arr[:2-1], 3so we have
arr, arr[1] = arr[:1], 3So the first assignment in this pair will set arr to [1]. Then the second assignment will set arr[1], but of course there is no such element, so that should panic.
I do not understand why this prints [1] with cmd/compile. With gccgo it panics as I expect with an index out of range error.