github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/go/types/testdata/expr3.src (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package expr3 6 7 import "time" 8 9 func indexes() { 10 _ = 1 /* ERROR "cannot index" */ [0] 11 _ = indexes /* ERROR "cannot index" */ [0] 12 _ = ( /* ERROR "cannot slice" */ 12 + 3)[1:2] 13 14 var a [10]int 15 _ = a[true /* ERROR "cannot convert" */ ] 16 _ = a["foo" /* ERROR "cannot convert" */ ] 17 _ = a[1.1 /* ERROR "truncated" */ ] 18 _ = a[1.0] 19 _ = a[- /* ERROR "negative" */ 1] 20 _ = a[- /* ERROR "negative" */ 1 :] 21 _ = a[: - /* ERROR "negative" */ 1] 22 _ = a[: /* ERROR "2nd index required" */ : /* ERROR "3rd index required" */ ] 23 _ = a[0: /* ERROR "2nd index required" */ : /* ERROR "3rd index required" */ ] 24 _ = a[0: /* ERROR "2nd index required" */ :10] 25 _ = a[:10:10] 26 27 var a0 int 28 a0 = a[0] 29 _ = a0 30 var a1 int32 31 a1 = a /* ERROR "cannot use .* in assignment" */ [1] 32 _ = a1 33 34 _ = a[9] 35 _ = a[10 /* ERROR "index .* out of bounds" */ ] 36 _ = a[1 /* ERROR "overflows" */ <<100] 37 _ = a[10:] 38 _ = a[:10] 39 _ = a[10:10] 40 _ = a[11 /* ERROR "index .* out of bounds" */ :] 41 _ = a[: 11 /* ERROR "index .* out of bounds" */ ] 42 _ = a[: 1 /* ERROR "overflows" */ <<100] 43 _ = a[:10:10] 44 _ = a[:11 /* ERROR "index .* out of bounds" */ :10] 45 _ = a[:10:11 /* ERROR "index .* out of bounds" */ ] 46 _ = a[10:0:10] /* ERROR "invalid slice indices" */ 47 _ = a[0:10:0] /* ERROR "invalid slice indices" */ 48 _ = a[10:0:0] /* ERROR "invalid slice indices" */ 49 _ = &a /* ERROR "cannot take address" */ [:10] 50 51 pa := &a 52 _ = pa[9] 53 _ = pa[10 /* ERROR "index .* out of bounds" */ ] 54 _ = pa[1 /* ERROR "overflows" */ <<100] 55 _ = pa[10:] 56 _ = pa[:10] 57 _ = pa[10:10] 58 _ = pa[11 /* ERROR "index .* out of bounds" */ :] 59 _ = pa[: 11 /* ERROR "index .* out of bounds" */ ] 60 _ = pa[: 1 /* ERROR "overflows" */ <<100] 61 _ = pa[:10:10] 62 _ = pa[:11 /* ERROR "index .* out of bounds" */ :10] 63 _ = pa[:10:11 /* ERROR "index .* out of bounds" */ ] 64 _ = pa[10:0:10] /* ERROR "invalid slice indices" */ 65 _ = pa[0:10:0] /* ERROR "invalid slice indices" */ 66 _ = pa[10:0:0] /* ERROR "invalid slice indices" */ 67 _ = &pa /* ERROR "cannot take address" */ [:10] 68 69 var b [0]int 70 _ = b[0 /* ERROR "index .* out of bounds" */ ] 71 _ = b[:] 72 _ = b[0:] 73 _ = b[:0] 74 _ = b[0:0] 75 _ = b[0:0:0] 76 _ = b[1 /* ERROR "index .* out of bounds" */ :0:0] 77 78 var s []int 79 _ = s[- /* ERROR "negative" */ 1] 80 _ = s[- /* ERROR "negative" */ 1 :] 81 _ = s[: - /* ERROR "negative" */ 1] 82 _ = s[0] 83 _ = s[1:2] 84 _ = s[2:1] /* ERROR "invalid slice indices" */ 85 _ = s[2:] 86 _ = s[: 1 /* ERROR "overflows" */ <<100] 87 _ = s[1 /* ERROR "overflows" */ <<100 :] 88 _ = s[1 /* ERROR "overflows" */ <<100 : 1 /* ERROR "overflows" */ <<100] 89 _ = s[: /* ERROR "2nd index required" */ : /* ERROR "3rd index required" */ ] 90 _ = s[:10:10] 91 _ = s[10:0:10] /* ERROR "invalid slice indices" */ 92 _ = s[0:10:0] /* ERROR "invalid slice indices" */ 93 _ = s[10:0:0] /* ERROR "invalid slice indices" */ 94 _ = &s /* ERROR "cannot take address" */ [:10] 95 96 var m map[string]int 97 _ = m[0 /* ERROR "cannot convert" */ ] 98 _ = m /* ERROR "cannot slice" */ ["foo" : "bar"] 99 _ = m["foo"] 100 // ok is of type bool 101 type mybool bool 102 var ok mybool 103 _, ok = m["bar"] 104 _ = ok 105 106 var t string 107 _ = t[- /* ERROR "negative" */ 1] 108 _ = t[- /* ERROR "negative" */ 1 :] 109 _ = t[: - /* ERROR "negative" */ 1] 110 _ = t /* ERROR "3-index slice of string" */ [1:2:3] 111 _ = "foo" /* ERROR "3-index slice of string" */ [1:2:3] 112 var t0 byte 113 t0 = t[0] 114 _ = t0 115 var t1 rune 116 t1 = t /* ERROR "cannot use .* in assignment" */ [2] 117 _ = t1 118 _ = ("foo" + "bar")[5] 119 _ = ("foo" + "bar")[6 /* ERROR "index .* out of bounds" */ ] 120 121 const c = "foo" 122 _ = c[- /* ERROR "negative" */ 1] 123 _ = c[- /* ERROR "negative" */ 1 :] 124 _ = c[: - /* ERROR "negative" */ 1] 125 var c0 byte 126 c0 = c[0] 127 _ = c0 128 var c2 float32 129 c2 = c /* ERROR "cannot use .* in assignment" */ [2] 130 _ = c[3 /* ERROR "index .* out of bounds" */ ] 131 _ = ""[0 /* ERROR "index .* out of bounds" */ ] 132 _ = c2 133 134 _ = s[1<<30] // no compile-time error here 135 136 // issue 4913 137 type mystring string 138 var ss string 139 var ms mystring 140 var i, j int 141 ss = "foo"[1:2] 142 ss = "foo"[i:j] 143 ms = "foo" /* ERROR "cannot use .* in assignment" */ [1:2] 144 ms = "foo" /* ERROR "cannot use .* in assignment" */ [i:j] 145 _, _ = ss, ms 146 } 147 148 type T struct { 149 x int 150 y func() 151 } 152 153 func (*T) m() {} 154 155 func method_expressions() { 156 _ = T /* ERROR "no field or method" */ .a 157 _ = T /* ERROR "has no method" */ .x 158 _ = T /* ERROR "not in method set" */ .m 159 _ = (*T).m 160 161 var f func(*T) = T /* ERROR "not in method set" */ .m 162 var g func(*T) = (*T).m 163 _, _ = f, g 164 165 _ = T /* ERROR "has no method" */ .y 166 _ = ( /* ERROR "has no method" */ *T).y 167 } 168 169 func struct_literals() { 170 type T0 struct { 171 a, b, c int 172 } 173 174 type T1 struct { 175 T0 176 a, b int 177 u float64 178 s string 179 } 180 181 // keyed elements 182 _ = T1{} 183 _ = T1{a: 0, 1 /* ERROR "mixture of .* elements" */ } 184 _ = T1{aa /* ERROR "unknown field" */ : 0} 185 _ = T1{1 /* ERROR "invalid field name" */ : 0} 186 _ = T1{a: 0, s: "foo", u: 0, a /* ERROR "duplicate field" */: 10} 187 _ = T1{a: "foo" /* ERROR "cannot convert" */ } 188 _ = T1{c /* ERROR "unknown field" */ : 0} 189 _ = T1{T0: { /* ERROR "missing type" */ }} // struct literal element type may not be elided 190 _ = T1{T0: T0{}} 191 _ = T1{T0 /* ERROR "invalid field name" */ .a: 0} 192 193 // unkeyed elements 194 _ = T0{1, 2, 3} 195 _ = T0{1, b /* ERROR "mixture" */ : 2, 3} 196 _ = T0{1, 2} /* ERROR "too few values" */ 197 _ = T0{1, 2, 3, 4 /* ERROR "too many values" */ } 198 _ = T0{1, "foo" /* ERROR "cannot convert" */, 3.4 /* ERROR "truncated" */} 199 200 // invalid type 201 type P *struct{ 202 x int 203 } 204 _ = P /* ERROR "invalid composite literal type" */ {} 205 206 // unexported fields 207 _ = time.Time{} 208 _ = time.Time{sec /* ERROR "unknown field" */ : 0} 209 _ = time.Time{ 210 0 /* ERROR implicit assignment to unexported field sec in time.Time literal */, 211 0 /* ERROR implicit assignment */ , 212 nil /* ERROR implicit assignment */ , 213 } 214 } 215 216 func array_literals() { 217 type A0 [0]int 218 _ = A0{} 219 _ = A0{0 /* ERROR "index .* out of bounds" */} 220 _ = A0{0 /* ERROR "index .* out of bounds" */ : 0} 221 222 type A1 [10]int 223 _ = A1{} 224 _ = A1{0, 1, 2} 225 _ = A1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 226 _ = A1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /* ERROR "index .* out of bounds" */ } 227 _ = A1{- /* ERROR "negative" */ 1: 0} 228 _ = A1{8: 8, 9} 229 _ = A1{8: 8, 9, 10 /* ERROR "index .* out of bounds" */ } 230 _ = A1{0, 1, 2, 0 /* ERROR "duplicate index" */ : 0, 3: 3, 4} 231 _ = A1{5: 5, 6, 7, 3: 3, 4} 232 _ = A1{5: 5, 6, 7, 3: 3, 4, 5 /* ERROR "duplicate index" */ } 233 _ = A1{10 /* ERROR "index .* out of bounds" */ : 10, 10 /* ERROR "index .* out of bounds" */ : 10} 234 _ = A1{5: 5, 6, 7, 3: 3, 1 /* ERROR "overflows" */ <<100: 4, 5 /* ERROR "duplicate index" */ } 235 _ = A1{5: 5, 6, 7, 4: 4, 1 /* ERROR "overflows" */ <<100: 4} 236 _ = A1{2.0} 237 _ = A1{2.1 /* ERROR "truncated" */ } 238 _ = A1{"foo" /* ERROR "cannot convert" */ } 239 240 // indices must be integer constants 241 i := 1 242 const f = 2.1 243 const s = "foo" 244 _ = A1{i /* ERROR "index i must be integer constant" */ : 0} 245 _ = A1{f /* ERROR "truncated" */ : 0} 246 _ = A1{s /* ERROR "cannot convert" */ : 0} 247 248 a0 := [...]int{} 249 assert(len(a0) == 0) 250 251 a1 := [...]int{0, 1, 2} 252 assert(len(a1) == 3) 253 var a13 [3]int 254 var a14 [4]int 255 a13 = a1 256 a14 = a1 /* ERROR "cannot use .* in assignment" */ 257 _, _ = a13, a14 258 259 a2 := [...]int{- /* ERROR "negative" */ 1: 0} 260 _ = a2 261 262 a3 := [...]int{0, 1, 2, 0 /* ERROR "duplicate index" */ : 0, 3: 3, 4} 263 assert(len(a3) == 5) // somewhat arbitrary 264 265 a4 := [...]complex128{0, 1, 2, 1<<10-2: -1i, 1i, 400: 10, 12, 14} 266 assert(len(a4) == 1024) 267 268 // composite literal element types may be elided 269 type T []int 270 _ = [10]T{T{}, {}, 5: T{1, 2, 3}, 7: {1, 2, 3}} 271 a6 := [...]T{T{}, {}, 5: T{1, 2, 3}, 7: {1, 2, 3}} 272 assert(len(a6) == 8) 273 274 // recursively so 275 _ = [10][10]T{{}, [10]T{{}}, {{1, 2, 3}}} 276 277 // from the spec 278 type Point struct { x, y float32 } 279 _ = [...]Point{Point{1.5, -3.5}, Point{0, 0}} 280 _ = [...]Point{{1.5, -3.5}, {0, 0}} 281 _ = [][]int{[]int{1, 2, 3}, []int{4, 5}} 282 _ = [][]int{{1, 2, 3}, {4, 5}} 283 _ = [...]*Point{&Point{1.5, -3.5}, &Point{0, 0}} 284 _ = [...]*Point{{1.5, -3.5}, {0, 0}} 285 } 286 287 func slice_literals() { 288 type S0 []int 289 _ = S0{} 290 _ = S0{0, 1, 2} 291 _ = S0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 292 _ = S0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 293 _ = S0{- /* ERROR "negative" */ 1: 0} 294 _ = S0{8: 8, 9} 295 _ = S0{8: 8, 9, 10} 296 _ = S0{0, 1, 2, 0 /* ERROR "duplicate index" */ : 0, 3: 3, 4} 297 _ = S0{5: 5, 6, 7, 3: 3, 4} 298 _ = S0{5: 5, 6, 7, 3: 3, 4, 5 /* ERROR "duplicate index" */ } 299 _ = S0{10: 10, 10 /* ERROR "duplicate index" */ : 10} 300 _ = S0{5: 5, 6, 7, 3: 3, 1 /* ERROR "overflows" */ <<100: 4, 5 /* ERROR "duplicate index" */ } 301 _ = S0{5: 5, 6, 7, 4: 4, 1 /* ERROR "overflows" */ <<100: 4} 302 _ = S0{2.0} 303 _ = S0{2.1 /* ERROR "truncated" */ } 304 _ = S0{"foo" /* ERROR "cannot convert" */ } 305 306 // indices must be resolved correctly 307 const index1 = 1 308 _ = S0{index1: 1} 309 _ = S0{index2: 2} 310 _ = S0{index3 /* ERROR "undeclared name" */ : 3} 311 312 // indices must be integer constants 313 i := 1 314 const f = 2.1 315 const s = "foo" 316 _ = S0{i /* ERROR "index i must be integer constant" */ : 0} 317 _ = S0{f /* ERROR "truncated" */ : 0} 318 _ = S0{s /* ERROR "cannot convert" */ : 0} 319 320 // composite literal element types may be elided 321 type T []int 322 _ = []T{T{}, {}, 5: T{1, 2, 3}, 7: {1, 2, 3}} 323 _ = [][]int{{1, 2, 3}, {4, 5}} 324 325 // recursively so 326 _ = [][]T{{}, []T{{}}, {{1, 2, 3}}} 327 } 328 329 const index2 int = 2 330 331 type N int 332 func (N) f() {} 333 334 func map_literals() { 335 type M0 map[string]int 336 type M1 map[bool]int 337 type M2 map[*int]int 338 339 _ = M0{} 340 _ = M0{1 /* ERROR "missing key" */ } 341 _ = M0{1 /* ERROR "cannot convert" */ : 2} 342 _ = M0{"foo": "bar" /* ERROR "cannot convert" */ } 343 _ = M0{"foo": 1, "bar": 2, "foo" /* ERROR "duplicate key" */ : 3 } 344 345 _ = map[interface{}]int{2: 1, 2 /* ERROR "duplicate key" */ : 1} 346 _ = map[interface{}]int{int(2): 1, int16(2): 1} 347 _ = map[interface{}]int{int16(2): 1, int16 /* ERROR "duplicate key" */ (2): 1} 348 349 type S string 350 351 _ = map[interface{}]int{"a": 1, "a" /* ERROR "duplicate key" */ : 1} 352 _ = map[interface{}]int{"a": 1, S("a"): 1} 353 _ = map[interface{}]int{S("a"): 1, S /* ERROR "duplicate key" */ ("a"): 1} 354 355 type I interface { 356 f() 357 } 358 359 _ = map[I]int{N(0): 1, N(2): 1} 360 _ = map[I]int{N(2): 1, N /* ERROR "duplicate key" */ (2): 1} 361 362 // map keys must be resolved correctly 363 key1 := "foo" 364 _ = M0{key1: 1} 365 _ = M0{key2: 2} 366 _ = M0{key3 /* ERROR "undeclared name" */ : 2} 367 368 var value int 369 _ = M1{true: 1, false: 0} 370 _ = M2{nil: 0, &value: 1} 371 372 // composite literal element types may be elided 373 type T [2]int 374 _ = map[int]T{0: T{3, 4}, 1: {5, 6}} 375 376 // recursively so 377 _ = map[int][]T{0: {}, 1: {{}, T{1, 2}}} 378 379 // composite literal key types may be elided 380 _ = map[T]int{T{3, 4}: 0, {5, 6}: 1} 381 382 // recursively so 383 _ = map[[2]T]int{{}: 0, {{}}: 1, [2]T{{}}: 2, {T{1, 2}}: 3} 384 385 // composite literal element and key types may be elided 386 _ = map[T]T{{}: {}, {1, 2}: T{3, 4}, T{4, 5}: {}} 387 _ = map[T]M0{{} : {}, T{1, 2}: M0{"foo": 0}, {1, 3}: {"foo": 1}} 388 389 // recursively so 390 _ = map[[2]T][]T{{}: {}, {{}}: {{}, T{1, 2}}, [2]T{{}}: nil, {T{1, 2}}: {{}, {}}} 391 392 // from the spec 393 type Point struct { x, y float32 } 394 _ = map[string]Point{"orig": {0, 0}} 395 _ = map[*Point]string{{0, 0}: "orig"} 396 } 397 398 var key2 string = "bar" 399 400 type I interface { 401 m() 402 } 403 404 type I2 interface { 405 m(int) 406 } 407 408 type T1 struct{} 409 type T2 struct{} 410 411 func (T2) m(int) {} 412 413 type mybool bool 414 415 func type_asserts() { 416 var x int 417 _ = x /* ERROR "not an interface" */ .(int) 418 419 var e interface{} 420 var ok bool 421 x, ok = e.(int) 422 _ = ok 423 424 // ok value is of type bool 425 var myok mybool 426 _, myok = e.(int) 427 _ = myok 428 429 var t I 430 _ = t /* ERROR "use of .* outside type switch" */ .(type) 431 _ = t /* ERROR "missing method m" */ .(T) 432 _ = t.(*T) 433 _ = t /* ERROR "missing method m" */ .(T1) 434 _ = t /* ERROR "wrong type for method m" */ .(T2) 435 _ = t /* STRICT "wrong type for method m" */ .(I2) // only an error in strict mode (issue 8561) 436 437 // e doesn't statically have an m, but may have one dynamically. 438 _ = e.(I2) 439 } 440 441 func f0() {} 442 func f1(x int) {} 443 func f2(u float32, s string) {} 444 func fs(s []byte) {} 445 func fv(x ...int) {} 446 func fi(x ... interface{}) {} 447 func (T) fm(x ...int) 448 449 func g0() {} 450 func g1() int { return 0} 451 func g2() (u float32, s string) { return } 452 func gs() []byte { return nil } 453 454 func _calls() { 455 var x int 456 var y float32 457 var s []int 458 459 f0() 460 _ = f0 /* ERROR "used as value" */ () 461 f0(g0 /* ERROR "too many arguments" */ ) 462 463 f1(0) 464 f1(x) 465 f1(10.0) 466 f1() /* ERROR "too few arguments" */ 467 f1(x, y /* ERROR "too many arguments" */ ) 468 f1(s /* ERROR "cannot use .* in argument" */ ) 469 f1(x ... /* ERROR "cannot use ..." */ ) 470 f1(g0 /* ERROR "used as value" */ ()) 471 f1(g1()) 472 // f1(g2()) // TODO(gri) missing position in error message 473 474 f2() /* ERROR "too few arguments" */ 475 f2(3.14) /* ERROR "too few arguments" */ 476 f2(3.14, "foo") 477 f2(x /* ERROR "cannot use .* in argument" */ , "foo") 478 f2(g0 /* ERROR "used as value" */ ()) 479 f2(g1 /* ERROR "cannot use .* in argument" */ ()) /* ERROR "too few arguments" */ 480 f2(g2()) 481 482 fs() /* ERROR "too few arguments" */ 483 fs(g0 /* ERROR "used as value" */ ()) 484 fs(g1 /* ERROR "cannot use .* in argument" */ ()) 485 fs(g2 /* ERROR "cannot use .* in argument" */ /* ERROR "too many arguments" */ ()) 486 fs(gs()) 487 488 fv() 489 fv(1, 2.0, x) 490 fv(s /* ERROR "cannot use .* in argument" */ ) 491 fv(s...) 492 fv(x /* ERROR "cannot use" */ ...) 493 fv(1, s... /* ERROR "can only use ... with matching parameter" */ ) 494 fv(gs /* ERROR "cannot use .* in argument" */ ()) 495 fv(gs /* ERROR "cannot use .* in argument" */ ()...) 496 497 var t T 498 t.fm() 499 t.fm(1, 2.0, x) 500 t.fm(s /* ERROR "cannot use .* in argument" */ ) 501 t.fm(g1()) 502 t.fm(1, s... /* ERROR "can only use ... with matching parameter" */ ) 503 t.fm(gs /* ERROR "cannot use .* in argument" */ ()) 504 t.fm(gs /* ERROR "cannot use .* in argument" */ ()...) 505 506 T.fm(t, ) 507 T.fm(t, 1, 2.0, x) 508 T.fm(t, s /* ERROR "cannot use .* in argument" */ ) 509 T.fm(t, g1()) 510 T.fm(t, 1, s... /* ERROR "can only use ... with matching parameter" */ ) 511 T.fm(t, gs /* ERROR "cannot use .* in argument" */ ()) 512 T.fm(t, gs /* ERROR "cannot use .* in argument" */ ()...) 513 514 var i interface{ fm(x ...int) } = t 515 i.fm() 516 i.fm(1, 2.0, x) 517 i.fm(s /* ERROR "cannot use .* in argument" */ ) 518 i.fm(g1()) 519 i.fm(1, s... /* ERROR "can only use ... with matching parameter" */ ) 520 i.fm(gs /* ERROR "cannot use .* in argument" */ ()) 521 i.fm(gs /* ERROR "cannot use .* in argument" */ ()...) 522 523 fi() 524 fi(1, 2.0, x, 3.14, "foo") 525 fi(g2()) 526 fi(0, g2) 527 fi(0, g2 /* ERROR "2-valued g2" */ ()) 528 } 529 530 func issue6344() { 531 type T []interface{} 532 var x T 533 fi(x...) // ... applies also to named slices 534 }