Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // run |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 2 | |
| 3 | // Copyright 2009 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 Pike | fc0dc04 | 2012-02-19 13:19:43 +1100 | [diff] [blame] | 7 | // Test behavior of the blank identifier (_). |
| 8 | |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 9 | package main |
| 10 | |
Alan Donovan | aa5aaab | 2013-02-21 12:48:38 -0500 | [diff] [blame] | 11 | import ( |
| 12 | "os" |
| 13 | "unsafe" |
| 14 | ) |
Rémy Oudompheng | 2ad57b4 | 2013-01-18 18:26:43 +0100 | [diff] [blame] | 15 | |
Russ Cox | aa6e81dd | 2009-09-09 16:59:41 -0700 | [diff] [blame] | 16 | import _ "fmt" |
| 17 | |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 18 | var call string |
| 19 | |
| 20 | type T struct { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 21 | _, _, _ int |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Russ Cox | aa6e81dd | 2009-09-09 16:59:41 -0700 | [diff] [blame] | 24 | func (T) _() { |
| 25 | } |
| 26 | |
| 27 | func (T) _() { |
| 28 | } |
| 29 | |
Rémy Oudompheng | 428ea68 | 2013-07-02 09:08:43 +0200 | [diff] [blame] | 30 | type U struct { |
| 31 | _ struct{ a, b, c int } |
| 32 | } |
| 33 | |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 34 | const ( |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 35 | c0 = iota |
| 36 | _ |
| 37 | _ |
| 38 | _ |
| 39 | c4 |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 42 | var ints = []string{ |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 43 | "1", |
| 44 | "2", |
Robert Griesemer | 542099d | 2009-12-09 19:27:08 -0800 | [diff] [blame] | 45 | "3", |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | func f() (int, int) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 49 | call += "f" |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 50 | return 1, 2 |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 53 | func g() (float64, float64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 54 | call += "g" |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 55 | return 3, 4 |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 58 | func h(_ int, _ float64) { |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func i() int { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 62 | call += "i" |
| 63 | return 23 |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 66 | var _ = i() |
Russ Cox | 4c3a85d | 2009-10-19 19:27:40 -0700 | [diff] [blame] | 67 | |
Russ Cox | aa6e81dd | 2009-09-09 16:59:41 -0700 | [diff] [blame] | 68 | func main() { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 69 | if call != "i" { |
| 70 | panic("init did not run") |
| 71 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 72 | call = "" |
| 73 | _, _ = f() |
| 74 | a, _ := f() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 75 | if a != 1 { |
| 76 | panic(a) |
| 77 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 78 | b, _ := g() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 79 | if b != 3 { |
| 80 | panic(b) |
| 81 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 82 | _, a = f() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 83 | if a != 2 { |
| 84 | panic(a) |
| 85 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 86 | _, b = g() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 87 | if b != 4 { |
| 88 | panic(b) |
| 89 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 90 | _ = i() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 91 | if call != "ffgfgi" { |
| 92 | panic(call) |
| 93 | } |
| 94 | if c4 != 4 { |
| 95 | panic(c4) |
| 96 | } |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 97 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 98 | out := "" |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 99 | for _, s := range ints { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 100 | out += s |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 101 | } |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 102 | if out != "123" { |
| 103 | panic(out) |
| 104 | } |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 105 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 106 | sum := 0 |
Ryan Hitchman | 062406b | 2010-12-08 21:36:56 -0800 | [diff] [blame] | 107 | for s := range ints { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 108 | sum += s |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 109 | } |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 110 | if sum != 3 { |
| 111 | panic(sum) |
| 112 | } |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 113 | |
Alan Donovan | bab2a54 | 2013-10-08 14:36:20 -0400 | [diff] [blame] | 114 | // go.tools/ssa/interp cannot support unsafe.Pointer. |
Alan Donovan | aa5aaab | 2013-02-21 12:48:38 -0500 | [diff] [blame] | 115 | if os.Getenv("GOSSAINTERP") == "" { |
| 116 | type T1 struct{ x, y, z int } |
| 117 | t1 := *(*T)(unsafe.Pointer(&T1{1, 2, 3})) |
| 118 | t2 := *(*T)(unsafe.Pointer(&T1{4, 5, 6})) |
| 119 | if t1 != t2 { |
| 120 | panic("T{} != T{}") |
| 121 | } |
Rémy Oudompheng | 428ea68 | 2013-07-02 09:08:43 +0200 | [diff] [blame] | 122 | |
| 123 | var u1, u2 interface{} |
| 124 | u1 = *(*U)(unsafe.Pointer(&T1{1, 2, 3})) |
| 125 | u2 = *(*U)(unsafe.Pointer(&T1{4, 5, 6})) |
| 126 | if u1 != u2 { |
| 127 | panic("U{} != U{}") |
| 128 | } |
Rémy Oudompheng | 2ad57b4 | 2013-01-18 18:26:43 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 131 | h(a, b) |
Rémy Oudompheng | 2ad57b4 | 2013-01-18 18:26:43 +0100 | [diff] [blame] | 132 | |
Russ Cox | 8e51548 | 2011-11-28 16:40:39 -0500 | [diff] [blame] | 133 | m() |
| 134 | } |
| 135 | |
| 136 | type I interface { |
| 137 | M(_ int, y int) |
| 138 | } |
| 139 | |
| 140 | type TI struct{} |
| 141 | |
Ian Lance Taylor | 373f1a9 | 2012-03-30 08:42:21 -0700 | [diff] [blame] | 142 | func (_ TI) M(x int, y int) { |
Russ Cox | 8e51548 | 2011-11-28 16:40:39 -0500 | [diff] [blame] | 143 | if x != y { |
| 144 | println("invalid M call:", x, y) |
| 145 | panic("bad M") |
| 146 | } |
| 147 | } |
| 148 | |
Russ Cox | 8c0b699 | 2011-12-09 11:59:21 -0500 | [diff] [blame] | 149 | var fp = func(_ int, y int) {} |
| 150 | |
| 151 | func init() { |
| 152 | fp = fp1 |
| 153 | } |
| 154 | |
| 155 | func fp1(x, y int) { |
| 156 | if x != y { |
| 157 | println("invalid fp1 call:", x, y) |
| 158 | panic("bad fp1") |
| 159 | } |
| 160 | } |
| 161 | |
Russ Cox | 8e51548 | 2011-11-28 16:40:39 -0500 | [diff] [blame] | 162 | func m() { |
| 163 | var i I |
Rémy Oudompheng | 2ad57b4 | 2013-01-18 18:26:43 +0100 | [diff] [blame] | 164 | |
Russ Cox | 8e51548 | 2011-11-28 16:40:39 -0500 | [diff] [blame] | 165 | i = TI{} |
| 166 | i.M(1, 1) |
| 167 | i.M(2, 2) |
Rémy Oudompheng | 2ad57b4 | 2013-01-18 18:26:43 +0100 | [diff] [blame] | 168 | |
Russ Cox | 8c0b699 | 2011-12-09 11:59:21 -0500 | [diff] [blame] | 169 | fp(1, 1) |
| 170 | fp(2, 2) |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // useless but legal |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 174 | var _ int = 1 |
| 175 | var _ = 2 |
| 176 | var _, _ = 3, 4 |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 177 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 178 | const _ = 3 |
| 179 | const _, _ = 4, 5 |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 180 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 181 | type _ int |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 182 | |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 183 | func _() { |
| 184 | panic("oops") |
| 185 | } |
| 186 | |
| 187 | func ff() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 188 | var _ int = 1 |
Russ Cox | 5438be4 | 2009-09-08 23:16:19 -0700 | [diff] [blame] | 189 | } |