1// run 2 3// Copyright 2011 The Go Authors. All rights reserved. 4// Use of this source code is governed by a BSD-style 5// license that can be found in the LICENSE file. 6 7// Test reordering of assignments. 8 9package main 10 11import "fmt" 12 13func main() { 14 p1() 15 p2() 16 p3() 17 p4() 18 p5() 19 p6() 20 p7() 21 p8() 22} 23 24var gx []int 25 26func f(i int) int { 27 return gx[i] 28} 29 30func check(x []int, x0, x1, x2 int) { 31 if x[0] != x0 || x[1] != x1 || x[2] != x2 { 32 fmt.Printf("%v, want %d,%d,%d\n", x, x0, x1, x2) 33 panic("failed") 34 } 35} 36 37func check3(x, y, z, xx, yy, zz int) { 38 if x != xx || y != yy || z != zz { 39 fmt.Printf("%d,%d,%d, want %d,%d,%d\n", x, y, z, xx, yy, zz) 40 panic("failed") 41 } 42} 43 44func p1() { 45 x := []int{1, 2, 3} 46 i := 0 47 i, x[i] = 1, 100 48 _ = i 49 check(x, 100, 2, 3) 50} 51 52func p2() { 53 x := []int{1, 2, 3} 54 i := 0 55 x[i], i = 100, 1 56 _ = i 57 check(x, 100, 2, 3) 58} 59 60func p3() { 61 x := []int{1, 2, 3} 62 y := x 63 gx = x 64 x[1], y[0] = f(0), f(1) 65 check(x, 2, 1, 3) 66} 67 68func p4() { 69 x := []int{1, 2, 3} 70 y := x 71 gx = x 72 x[1], y[0] = gx[0], gx[1] 73 check(x, 2, 1, 3) 74} 75 76func p5() { 77 x := []int{1, 2, 3} 78 y := x 79 p := &x[0] 80 q := &x[1] 81 *p, *q = x[1], y[0] 82 check(x, 2, 1, 3) 83} 84 85func p6() { 86 x := 1 87 y := 2 88 z := 3 89 px := &x 90 py := &y 91 *px, *py = y, x 92 check3(x, y, z, 2, 1, 3) 93} 94 95func f1(x, y, z int) (xx, yy, zz int) { 96 return x, y, z 97} 98 99func f2() (x, y, z int) { 100 return f1(2, 1, 3) 101} 102 103func p7() { 104 x, y, z := f2() 105 check3(x, y, z, 2, 1, 3) 106} 107 108func p8() { 109 m := make(map[int]int) 110 m[0] = len(m) 111 if m[0] != 0 { 112 panic(m[0]) 113 } 114} 115