blob: 8fd623c1c7074e4fc1d88b76b380cc48af679c5b [file] [log] [blame]
Russ Cox57eb06f2012-02-16 23:51:04 -05001// run
Russ Coxeb3aba242011-10-13 15:46:39 -04002
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
Rob Pike19bab1d2012-02-24 10:30:39 +11007// Test reordering of assignments.
Russ Coxeb3aba242011-10-13 15:46:39 -04008
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() {
Alan Donovan1c1096e2013-02-11 18:20:52 -050045 x := []int{1, 2, 3}
Russ Coxeb3aba242011-10-13 15:46:39 -040046 i := 0
47 i, x[i] = 1, 100
48 _ = i
49 check(x, 100, 2, 3)
50}
51
52func p2() {
Alan Donovan1c1096e2013-02-11 18:20:52 -050053 x := []int{1, 2, 3}
Russ Coxeb3aba242011-10-13 15:46:39 -040054 i := 0
55 x[i], i = 100, 1
56 _ = i
57 check(x, 100, 2, 3)
58}
59
60func p3() {
Alan Donovan1c1096e2013-02-11 18:20:52 -050061 x := []int{1, 2, 3}
Russ Coxeb3aba242011-10-13 15:46:39 -040062 y := x
63 gx = x
64 x[1], y[0] = f(0), f(1)
65 check(x, 2, 1, 3)
66}
67
68func p4() {
Alan Donovan1c1096e2013-02-11 18:20:52 -050069 x := []int{1, 2, 3}
Russ Coxeb3aba242011-10-13 15:46:39 -040070 y := x
71 gx = x
72 x[1], y[0] = gx[0], gx[1]
73 check(x, 2, 1, 3)
74}
75
76func p5() {
Alan Donovan1c1096e2013-02-11 18:20:52 -050077 x := []int{1, 2, 3}
Russ Coxeb3aba242011-10-13 15:46:39 -040078 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
Alan Donovan1c1096e2013-02-11 18:20:52 -050092 check3(x, y, z, 2, 1, 3)
Russ Coxeb3aba242011-10-13 15:46:39 -040093}
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() {
Ian Lance Taylor76490cf2012-04-24 10:17:26 -0700109 m := make(map[int]int)
110 m[0] = len(m)
111 if m[0] != 0 {
112 panic(m[0])
113 }
114}