1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
// Copyright 2012 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.
// comparisons
package expr2
func _bool() {
const t = true == true
const f = true == false
_ = t /* ERROR "cannot compare" */ < f
_ = 0 /* ERROR "cannot convert" */ == t
var b bool
var x, y float32
b = x < y
_ = b
_ = struct{b bool}{x < y}
}
// corner cases
var (
v0 = nil /* ERROR "cannot compare" */ == nil
)
func arrays() {
// basics
var a, b [10]int
_ = a == b
_ = a != b
_ = a /* ERROR < not defined */ < b
_ = a == nil /* ERROR cannot convert */
type C [10]int
var c C
_ = a == c
type D [10]int
var d D
_ = c /* ERROR mismatched types */ == d
var e [10]func() int
_ = e /* ERROR == not defined */ == e
}
func structs() {
// basics
var s, t struct {
x int
a [10]float32
_ bool
}
_ = s == t
_ = s != t
_ = s /* ERROR < not defined */ < t
_ = s == nil /* ERROR cannot convert */
type S struct {
x int
a [10]float32
_ bool
}
type T struct {
x int
a [10]float32
_ bool
}
var ss S
var tt T
_ = s == ss
_ = ss /* ERROR mismatched types */ == tt
var u struct {
x int
a [10]map[string]int
}
_ = u /* ERROR cannot compare */ == u
}
func pointers() {
// nil
_ = nil /* ERROR == not defined */ == nil
_ = nil /* ERROR != not defined */ != nil
_ = nil /* ERROR < not defined */ < nil
_ = nil /* ERROR <= not defined */ <= nil
_ = nil /* ERROR > not defined */ > nil
_ = nil /* ERROR >= not defined */ >= nil
// basics
var p, q *int
_ = p == q
_ = p != q
_ = p == nil
_ = p != nil
_ = nil == q
_ = nil != q
_ = p /* ERROR < not defined */ < q
_ = p /* ERROR <= not defined */ <= q
_ = p /* ERROR > not defined */ > q
_ = p /* ERROR >= not defined */ >= q
// various element types
type (
S1 struct{}
S2 struct{}
P1 *S1
P2 *S2
)
var (
ps1 *S1
ps2 *S2
p1 P1
p2 P2
)
_ = ps1 == ps1
_ = ps1 /* ERROR mismatched types */ == ps2
_ = ps2 /* ERROR mismatched types */ == ps1
_ = p1 == p1
_ = p1 /* ERROR mismatched types */ == p2
_ = p1 == ps1
}
func channels() {
// basics
var c, d chan int
_ = c == d
_ = c != d
_ = c == nil
_ = c /* ERROR < not defined */ < d
// various element types (named types)
type (
C1 chan int
C1r <-chan int
C1s chan<- int
C2 chan float32
)
var (
c1 C1
c1r C1r
c1s C1s
c1a chan int
c2 C2
)
_ = c1 == c1
_ = c1 /* ERROR mismatched types */ == c1r
_ = c1 /* ERROR mismatched types */ == c1s
_ = c1r /* ERROR mismatched types */ == c1s
_ = c1 == c1a
_ = c1a == c1
_ = c1 /* ERROR mismatched types */ == c2
_ = c1a /* ERROR mismatched types */ == c2
// various element types (unnamed types)
var (
d1 chan int
d1r <-chan int
d1s chan<- int
d1a chan<- int
d2 chan float32
)
_ = d1 == d1
_ = d1 == d1r
_ = d1 == d1s
_ = d1r /* ERROR mismatched types */ == d1s
_ = d1 == d1a
_ = d1a == d1
_ = d1 /* ERROR mismatched types */ == d2
_ = d1a /* ERROR mismatched types */ == d2
}
// for interfaces test
type S1 struct{}
type S11 struct{}
type S2 struct{}
func (*S1) m() int
func (*S11) m() int
func (*S11) n()
func (*S2) m() float32
func interfaces() {
// basics
var i, j interface{ m() int }
_ = i == j
_ = i != j
_ = i == nil
_ = i /* ERROR < not defined */ < j
// various interfaces
var ii interface { m() int; n() }
var k interface { m() float32 }
_ = i == ii
_ = i /* ERROR mismatched types */ == k
// interfaces vs values
var s1 S1
var s11 S11
var s2 S2
_ = i == 0 /* ERROR cannot convert */
_ = i /* ERROR mismatched types */ == s1
_ = i == &s1
_ = i == &s11
_ = i /* ERROR mismatched types */ == s2
_ = i /* ERROR mismatched types */ == &s2
}
func slices() {
// basics
var s []int
_ = s == nil
_ = s != nil
_ = s /* ERROR < not defined */ < nil
// slices are not otherwise comparable
_ = s /* ERROR == not defined */ == s
_ = s /* ERROR < not defined */ < s
}
func maps() {
// basics
var m map[string]int
_ = m == nil
_ = m != nil
_ = m /* ERROR < not defined */ < nil
// maps are not otherwise comparable
_ = m /* ERROR == not defined */ == m
_ = m /* ERROR < not defined */ < m
}
func funcs() {
// basics
var f func(int) float32
_ = f == nil
_ = f != nil
_ = f /* ERROR < not defined */ < nil
// funcs are not otherwise comparable
_ = f /* ERROR == not defined */ == f
_ = f /* ERROR < not defined */ < f
}
|