gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/linters/simpler/ssa/testdata/valueforexpr.go (about) 1 //+build ignore 2 3 package main 4 5 // This file is the input to TestValueForExpr in source_test.go, which 6 // ensures that each expression e immediately following a /*@kind*/(x) 7 // annotation, when passed to Function.ValueForExpr(e), returns a 8 // non-nil Value of the same type as e and of kind 'kind'. 9 10 func f(spilled, unspilled int) { 11 _ = /*@UnOp*/ (spilled) 12 _ = /*@Parameter*/ (unspilled) 13 _ = /*@<nil>*/ (1 + 2) // (constant) 14 i := 0 15 16 f := func() (int, int) { return 0, 0 } 17 18 /*@Call*/ 19 (print( /*@BinOp*/ (i + 1))) 20 _, _ = /*@Call*/ (f()) 21 ch := /*@MakeChan*/ (make(chan int)) 22 /*@UnOp*/ (<-ch) 23 x := /*@UnOp*/ (<-ch) 24 _ = x 25 select { 26 case /*@Extract*/ (<-ch): 27 case x := /*@Extract*/ (<-ch): 28 _ = x 29 } 30 defer /*@Function*/ (func() { 31 })() 32 go /*@Function*/ (func() { 33 })() 34 y := 0 35 if true && /*@BinOp*/ (bool(y > 0)) { 36 y = 1 37 } 38 _ = /*@Phi*/ (y) 39 map1 := /*@MakeMap*/ (make(map[string]string)) 40 _ = map1 41 _ = /*@Slice*/ (make([]int, 0)) 42 _ = /*@MakeClosure*/ (func() { print(spilled) }) 43 44 sl := []int{} 45 _ = /*@Slice*/ (sl[:0]) 46 47 tmp := /*@Alloc*/ (new(int)) 48 _ = tmp 49 var iface interface{} 50 _ = /*@TypeAssert*/ (iface.(int)) 51 _ = /*@UnOp*/ (sl[0]) 52 _ = /*@IndexAddr*/ (&sl[0]) 53 _ = /*@Index*/ ([2]int{}[0]) 54 var p *int 55 _ = /*@UnOp*/ (*p) 56 57 _ = /*@UnOp*/ (global) 58 /*@UnOp*/ (global)[""] = "" 59 /*@Global*/ (global) = map[string]string{} 60 61 var local t 62 /*UnOp*/ (local.x) = 1 63 64 // Exercise corner-cases of lvalues vs rvalues. 65 type N *N 66 var n N 67 /*@UnOp*/ (n) = /*@UnOp*/ (n) 68 /*@ChangeType*/ (n) = /*@Alloc*/ (&n) 69 /*@UnOp*/ (n) = /*@UnOp*/ (*n) 70 /*@UnOp*/ (n) = /*@UnOp*/ (**n) 71 } 72 73 func complit() { 74 // Composite literals. 75 // We get different results for 76 // - composite literal as value (e.g. operand to print) 77 // - composite literal initializer for addressable value 78 // - composite literal value assigned to blank var 79 80 // 1. Slices 81 print( /*@Slice*/ ([]int{})) 82 print( /*@Alloc*/ (&[]int{})) 83 print(& /*@Slice*/ ([]int{})) 84 85 sl1 := /*@Slice*/ ([]int{}) 86 sl2 := /*@Alloc*/ (&[]int{}) 87 sl3 := & /*@Slice*/ ([]int{}) 88 _, _, _ = sl1, sl2, sl3 89 90 _ = /*@Slice*/ ([]int{}) 91 _ = & /*@Slice*/ ([]int{}) 92 93 // 2. Arrays 94 print( /*@UnOp*/ ([1]int{})) 95 print( /*@Alloc*/ (&[1]int{})) 96 print(& /*@Alloc*/ ([1]int{})) 97 98 arr1 := /*@Alloc*/ ([1]int{}) 99 arr2 := /*@Alloc*/ (&[1]int{}) 100 arr3 := & /*@Alloc*/ ([1]int{}) 101 _, _, _ = arr1, arr2, arr3 102 103 _ = /*@UnOp*/ ([1]int{}) 104 _ = /*@Alloc*/ (& /*@Alloc*/ ([1]int{})) 105 _ = & /*@Alloc*/ ([1]int{}) 106 107 // 3. Maps 108 type M map[int]int 109 print( /*@MakeMap*/ (M{})) 110 print( /*@Alloc*/ (&M{})) 111 print(& /*@MakeMap*/ (M{})) 112 113 m1 := /*@MakeMap*/ (M{}) 114 m2 := /*@Alloc*/ (&M{}) 115 m3 := & /*@MakeMap*/ (M{}) 116 _, _, _ = m1, m2, m3 117 118 _ = /*@MakeMap*/ (M{}) 119 _ = & /*@MakeMap*/ (M{}) 120 121 // 4. Structs 122 print( /*@UnOp*/ (struct{}{})) 123 print( /*@Alloc*/ (&struct{}{})) 124 print(& /*@Alloc*/ (struct{}{})) 125 126 s1 := /*@Alloc*/ (struct{}{}) 127 s2 := /*@Alloc*/ (&struct{}{}) 128 s3 := & /*@Alloc*/ (struct{}{}) 129 _, _, _ = s1, s2, s3 130 131 _ = /*@UnOp*/ (struct{}{}) 132 _ = /*@Alloc*/ (& /*@Alloc*/ (struct{}{})) 133 _ = & /*@Alloc*/ (struct{}{}) 134 } 135 136 type t struct{ x int } 137 138 // Ensure we can locate methods of named types. 139 func (t) f(param int) { 140 _ = /*@Parameter*/ (param) 141 } 142 143 // Ensure we can locate init functions. 144 func init() { 145 m := /*@MakeMap*/ (make(map[string]string)) 146 _ = m 147 } 148 149 // Ensure we can locate variables in initializer expressions. 150 var global = /*@MakeMap*/ (make(map[string]string))