blank.go 2.1 KB
Newer Older
Russ Cox's avatar
Russ Cox committed
1
// run
Russ Cox's avatar
Russ Cox committed
2 3 4 5 6

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

7 8
// Test behavior of the blank identifier (_).

Russ Cox's avatar
Russ Cox committed
9 10
package main

11 12 13 14
import (
	"os"
	"unsafe"
)
15

Russ Cox's avatar
Russ Cox committed
16 17
import _ "fmt"

Russ Cox's avatar
Russ Cox committed
18 19 20
var call string

type T struct {
Rob Pike's avatar
Rob Pike committed
21
	_, _, _ int
Russ Cox's avatar
Russ Cox committed
22 23
}

Russ Cox's avatar
Russ Cox committed
24 25 26 27 28 29
func (T) _() {
}

func (T) _() {
}

30 31 32 33
type U struct {
	_ struct{ a, b, c int }
}

Russ Cox's avatar
Russ Cox committed
34
const (
Rob Pike's avatar
Rob Pike committed
35 36 37 38 39
	c0 = iota
	_
	_
	_
	c4
Russ Cox's avatar
Russ Cox committed
40 41
)

42
var ints = []string{
Russ Cox's avatar
Russ Cox committed
43 44
	"1",
	"2",
45
	"3",
Russ Cox's avatar
Russ Cox committed
46 47 48
}

func f() (int, int) {
Rob Pike's avatar
Rob Pike committed
49
	call += "f"
50
	return 1, 2
Russ Cox's avatar
Russ Cox committed
51 52
}

53
func g() (float64, float64) {
Rob Pike's avatar
Rob Pike committed
54
	call += "g"
55
	return 3, 4
Russ Cox's avatar
Russ Cox committed
56 57
}

58
func h(_ int, _ float64) {
Russ Cox's avatar
Russ Cox committed
59 60 61
}

func i() int {
Rob Pike's avatar
Rob Pike committed
62 63
	call += "i"
	return 23
Russ Cox's avatar
Russ Cox committed
64 65
}

Rob Pike's avatar
Rob Pike committed
66
var _ = i()
Russ Cox's avatar
Russ Cox committed
67

Russ Cox's avatar
Russ Cox committed
68
func main() {
69 70 71
	if call != "i" {
		panic("init did not run")
	}
Rob Pike's avatar
Rob Pike committed
72 73 74
	call = ""
	_, _ = f()
	a, _ := f()
75 76 77
	if a != 1 {
		panic(a)
	}
Rob Pike's avatar
Rob Pike committed
78
	b, _ := g()
79 80 81
	if b != 3 {
		panic(b)
	}
Rob Pike's avatar
Rob Pike committed
82
	_, a = f()
83 84 85
	if a != 2 {
		panic(a)
	}
Rob Pike's avatar
Rob Pike committed
86
	_, b = g()
87 88 89
	if b != 4 {
		panic(b)
	}
Rob Pike's avatar
Rob Pike committed
90
	_ = i()
91 92 93 94 95 96
	if call != "ffgfgi" {
		panic(call)
	}
	if c4 != 4 {
		panic(c4)
	}
Russ Cox's avatar
Russ Cox committed
97

Rob Pike's avatar
Rob Pike committed
98
	out := ""
Russ Cox's avatar
Russ Cox committed
99
	for _, s := range ints {
Rob Pike's avatar
Rob Pike committed
100
		out += s
Russ Cox's avatar
Russ Cox committed
101
	}
102 103 104
	if out != "123" {
		panic(out)
	}
Russ Cox's avatar
Russ Cox committed
105

Rob Pike's avatar
Rob Pike committed
106
	sum := 0
107
	for s := range ints {
Rob Pike's avatar
Rob Pike committed
108
		sum += s
Russ Cox's avatar
Russ Cox committed
109
	}
110 111 112
	if sum != 3 {
		panic(sum)
	}
Russ Cox's avatar
Russ Cox committed
113

114
	// go.tools/ssa/interp cannot support unsafe.Pointer.
115 116 117 118 119 120 121
	if os.Getenv("GOSSAINTERP") == "" {
		type T1 struct{ x, y, z int }
		t1 := *(*T)(unsafe.Pointer(&T1{1, 2, 3}))
		t2 := *(*T)(unsafe.Pointer(&T1{4, 5, 6}))
		if t1 != t2 {
			panic("T{} != T{}")
		}
122 123 124 125 126 127 128

		var u1, u2 interface{}
		u1 = *(*U)(unsafe.Pointer(&T1{1, 2, 3}))
		u2 = *(*U)(unsafe.Pointer(&T1{4, 5, 6}))
		if u1 != u2 {
			panic("U{} != U{}")
		}
129 130
	}

131
	h(a, b)
132

133 134 135 136 137 138 139 140 141
	m()
}

type I interface {
	M(_ int, y int)
}

type TI struct{}

142
func (_ TI) M(x int, y int) {
143 144 145 146 147 148
	if x != y {
		println("invalid M call:", x, y)
		panic("bad M")
	}
}

Russ Cox's avatar
Russ Cox committed
149 150 151 152 153 154 155 156 157 158 159 160 161
var fp = func(_ int, y int) {}

func init() {
	fp = fp1
}

func fp1(x, y int) {
	if x != y {
		println("invalid fp1 call:", x, y)
		panic("bad fp1")
	}
}

162 163
func m() {
	var i I
164

165 166 167
	i = TI{}
	i.M(1, 1)
	i.M(2, 2)
168

Russ Cox's avatar
Russ Cox committed
169 170
	fp(1, 1)
	fp(2, 2)
Russ Cox's avatar
Russ Cox committed
171 172 173
}

// useless but legal
Rob Pike's avatar
Rob Pike committed
174 175 176
var _ int = 1
var _ = 2
var _, _ = 3, 4
177

Rob Pike's avatar
Rob Pike committed
178 179
const _ = 3
const _, _ = 4, 5
180

Rob Pike's avatar
Rob Pike committed
181
type _ int
182

Russ Cox's avatar
Russ Cox committed
183 184 185 186 187
func _() {
	panic("oops")
}

func ff() {
Rob Pike's avatar
Rob Pike committed
188
	var _ int = 1
Russ Cox's avatar
Russ Cox committed
189
}