github.com/kdevb0x/go@v0.0.0-20180115030120-39687051e9e7/test/reorder.go (about)

     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  
     9  package main
    10  
    11  import "fmt"
    12  
    13  func main() {
    14  	p1()
    15  	p2()
    16  	p3()
    17  	p4()
    18  	p5()
    19  	p6()
    20  	p7()
    21  	p8()
    22  	p9()
    23  }
    24  
    25  var gx []int
    26  
    27  func f(i int) int {
    28  	return gx[i]
    29  }
    30  
    31  func check(x []int, x0, x1, x2 int) {
    32  	if x[0] != x0 || x[1] != x1 || x[2] != x2 {
    33  		fmt.Printf("%v, want %d,%d,%d\n", x, x0, x1, x2)
    34  		panic("failed")
    35  	}
    36  }
    37  
    38  func check3(x, y, z, xx, yy, zz int) {
    39  	if x != xx || y != yy || z != zz {
    40  		fmt.Printf("%d,%d,%d, want %d,%d,%d\n", x, y, z, xx, yy, zz)
    41  		panic("failed")
    42  	}
    43  }
    44  
    45  func p1() {
    46  	x := []int{1, 2, 3}
    47  	i := 0
    48  	i, x[i] = 1, 100
    49  	_ = i
    50  	check(x, 100, 2, 3)
    51  }
    52  
    53  func p2() {
    54  	x := []int{1, 2, 3}
    55  	i := 0
    56  	x[i], i = 100, 1
    57  	_ = i
    58  	check(x, 100, 2, 3)
    59  }
    60  
    61  func p3() {
    62  	x := []int{1, 2, 3}
    63  	y := x
    64  	gx = x
    65  	x[1], y[0] = f(0), f(1)
    66  	check(x, 2, 1, 3)
    67  }
    68  
    69  func p4() {
    70  	x := []int{1, 2, 3}
    71  	y := x
    72  	gx = x
    73  	x[1], y[0] = gx[0], gx[1]
    74  	check(x, 2, 1, 3)
    75  }
    76  
    77  func p5() {
    78  	x := []int{1, 2, 3}
    79  	y := x
    80  	p := &x[0]
    81  	q := &x[1]
    82  	*p, *q = x[1], y[0]
    83  	check(x, 2, 1, 3)
    84  }
    85  
    86  func p6() {
    87  	x := 1
    88  	y := 2
    89  	z := 3
    90  	px := &x
    91  	py := &y
    92  	*px, *py = y, x
    93  	check3(x, y, z, 2, 1, 3)
    94  }
    95  
    96  func f1(x, y, z int) (xx, yy, zz int) {
    97  	return x, y, z
    98  }
    99  
   100  func f2() (x, y, z int) {
   101  	return f1(2, 1, 3)
   102  }
   103  
   104  func p7() {
   105  	x, y, z := f2()
   106  	check3(x, y, z, 2, 1, 3)
   107  }
   108  
   109  func p8() {
   110  	m := make(map[int]int)
   111  	m[0] = len(m)
   112  	if m[0] != 0 {
   113  		panic(m[0])
   114  	}
   115  }
   116  
   117  // Issue #13433: Left-to-right assignment of OAS2XXX nodes.
   118  func p9() {
   119  	var x bool
   120  
   121  	// OAS2FUNC
   122  	x, x = fn()
   123  	checkOAS2XXX(x, "x, x = fn()")
   124  
   125  	// OAS2RECV
   126  	var c = make(chan bool, 10)
   127  	c <- false
   128  	x, x = <-c
   129  	checkOAS2XXX(x, "x, x <-c")
   130  
   131  	// OAS2MAPR
   132  	var m = map[int]bool{0: false}
   133  	x, x = m[0]
   134  	checkOAS2XXX(x, "x, x = m[0]")
   135  
   136  	// OAS2DOTTYPE
   137  	var i interface{} = false
   138  	x, x = i.(bool)
   139  	checkOAS2XXX(x, "x, x = i.(bool)")
   140  }
   141  
   142  //go:noinline
   143  func fn() (bool, bool) { return false, true }
   144  
   145  // checks the order of OAS2XXX.
   146  func checkOAS2XXX(x bool, s string) {
   147  	if !x {
   148  		fmt.Printf("%s; got=(false); want=(true)\n", s)
   149  		panic("failed")
   150  	}
   151  }