2018-01-24 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / go.test / test / blank.go
blob0539debb1f25249788954bf0704db109ae1d635b
1 // run
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.
7 // Test behavior of the blank identifier (_).
9 package main
11 import (
12 "os"
13 "unsafe"
16 import _ "fmt"
18 var call string
20 type T struct {
21 _, _, _ int
24 func (T) _() {
27 func (T) _() {
30 type U struct {
31 _ struct{ a, b, c int }
34 const (
35 c0 = iota
42 var ints = []string{
43 "1",
44 "2",
45 "3",
48 func f() (int, int) {
49 call += "f"
50 return 1, 2
53 func g() (float64, float64) {
54 call += "g"
55 return 3, 4
58 func h(_ int, _ float64) {
61 func i() int {
62 call += "i"
63 return 23
66 var _ = i()
68 func main() {
69 if call != "i" {
70 panic("init did not run")
72 call = ""
73 _, _ = f()
74 a, _ := f()
75 if a != 1 {
76 panic(a)
78 b, _ := g()
79 if b != 3 {
80 panic(b)
82 _, a = f()
83 if a != 2 {
84 panic(a)
86 _, b = g()
87 if b != 4 {
88 panic(b)
90 _ = i()
91 if call != "ffgfgi" {
92 panic(call)
94 if c4 != 4 {
95 panic(c4)
98 out := ""
99 for _, s := range ints {
100 out += s
102 if out != "123" {
103 panic(out)
106 sum := 0
107 for s := range ints {
108 sum += s
110 if sum != 3 {
111 panic(sum)
114 // go.tools/ssa/interp cannot support unsafe.Pointer.
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{}")
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{}")
131 h(a, b)
136 type I interface {
137 M(_ int, y int)
140 type TI struct{}
142 func (_ TI) M(x int, y int) {
143 if x != y {
144 println("invalid M call:", x, y)
145 panic("bad M")
149 var fp = func(_ int, y int) {}
151 func init() {
152 fp = fp1
155 func fp1(x, y int) {
156 if x != y {
157 println("invalid fp1 call:", x, y)
158 panic("bad fp1")
162 func m() {
163 var i I
165 i = TI{}
166 i.M(1, 1)
167 i.M(2, 2)
169 fp(1, 1)
170 fp(2, 2)
173 // useless but legal
174 var _ int = 1
175 var _ = 2
176 var _, _ = 3, 4
178 const _ = 3
179 const _, _ = 4, 5
181 type _ int
183 func _() {
184 panic("oops")
187 func ff() {
188 var _ int = 1