github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/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 wall 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 // issue 17954 329 type T0 *struct { s string } 330 _ = []T0{{}} 331 _ = []T0{{"foo"}} 332 333 type T1 *struct{ int } 334 _ = []T1{} 335 _ = []T1{{0}, {1}, {2}} 336 337 type T2 T1 338 _ = []T2{} 339 _ = []T2{{0}, {1}, {2}} 340 341 _ = map[T0]T2{} 342 _ = map[T0]T2{{}: {}} 343 } 344 345 const index2 int = 2 346 347 type N int 348 func (N) f() {} 349 350 func map_literals() { 351 type M0 map[string]int 352 type M1 map[bool]int 353 type M2 map[*int]int 354 355 _ = M0{} 356 _ = M0{1 /* ERROR "missing key" */ } 357 _ = M0{1 /* ERROR "cannot convert" */ : 2} 358 _ = M0{"foo": "bar" /* ERROR "cannot convert" */ } 359 _ = M0{"foo": 1, "bar": 2, "foo" /* ERROR "duplicate key" */ : 3 } 360 361 _ = map[interface{}]int{2: 1, 2 /* ERROR "duplicate key" */ : 1} 362 _ = map[interface{}]int{int(2): 1, int16(2): 1} 363 _ = map[interface{}]int{int16(2): 1, int16 /* ERROR "duplicate key" */ (2): 1} 364 365 type S string 366 367 _ = map[interface{}]int{"a": 1, "a" /* ERROR "duplicate key" */ : 1} 368 _ = map[interface{}]int{"a": 1, S("a"): 1} 369 _ = map[interface{}]int{S("a"): 1, S /* ERROR "duplicate key" */ ("a"): 1} 370 371 type I interface { 372 f() 373 } 374 375 _ = map[I]int{N(0): 1, N(2): 1} 376 _ = map[I]int{N(2): 1, N /* ERROR "duplicate key" */ (2): 1} 377 378 // map keys must be resolved correctly 379 key1 := "foo" 380 _ = M0{key1: 1} 381 _ = M0{key2: 2} 382 _ = M0{key3 /* ERROR "undeclared name" */ : 2} 383 384 var value int 385 _ = M1{true: 1, false: 0} 386 _ = M2{nil: 0, &value: 1} 387 388 // composite literal element types may be elided 389 type T [2]int 390 _ = map[int]T{0: T{3, 4}, 1: {5, 6}} 391 392 // recursively so 393 _ = map[int][]T{0: {}, 1: {{}, T{1, 2}}} 394 395 // composite literal key types may be elided 396 _ = map[T]int{T{3, 4}: 0, {5, 6}: 1} 397 398 // recursively so 399 _ = map[[2]T]int{{}: 0, {{}}: 1, [2]T{{}}: 2, {T{1, 2}}: 3} 400 401 // composite literal element and key types may be elided 402 _ = map[T]T{{}: {}, {1, 2}: T{3, 4}, T{4, 5}: {}} 403 _ = map[T]M0{{} : {}, T{1, 2}: M0{"foo": 0}, {1, 3}: {"foo": 1}} 404 405 // recursively so 406 _ = map[[2]T][]T{{}: {}, {{}}: {{}, T{1, 2}}, [2]T{{}}: nil, {T{1, 2}}: {{}, {}}} 407 408 // from the spec 409 type Point struct { x, y float32 } 410 _ = map[string]Point{"orig": {0, 0}} 411 _ = map[*Point]string{{0, 0}: "orig"} 412 413 // issue 17954 414 type T0 *struct{ s string } 415 type T1 *struct{ int } 416 type T2 T1 417 418 _ = map[T0]T2{} 419 _ = map[T0]T2{{}: {}} 420 } 421 422 var key2 string = "bar" 423 424 type I interface { 425 m() 426 } 427 428 type I2 interface { 429 m(int) 430 } 431 432 type T1 struct{} 433 type T2 struct{} 434 435 func (T2) m(int) {} 436 437 type mybool bool 438 439 func type_asserts() { 440 var x int 441 _ = x /* ERROR "not an interface" */ .(int) 442 443 var e interface{} 444 var ok bool 445 x, ok = e.(int) 446 _ = ok 447 448 // ok value is of type bool 449 var myok mybool 450 _, myok = e.(int) 451 _ = myok 452 453 var t I 454 _ = t /* ERROR "use of .* outside type switch" */ .(type) 455 _ = t /* ERROR "missing method m" */ .(T) 456 _ = t.(*T) 457 _ = t /* ERROR "missing method m" */ .(T1) 458 _ = t /* ERROR "wrong type for method m" */ .(T2) 459 _ = t /* STRICT "wrong type for method m" */ .(I2) // only an error in strict mode (issue 8561) 460 461 // e doesn't statically have an m, but may have one dynamically. 462 _ = e.(I2) 463 } 464 465 func f0() {} 466 func f1(x int) {} 467 func f2(u float32, s string) {} 468 func fs(s []byte) {} 469 func fv(x ...int) {} 470 func fi(x ... interface{}) {} 471 func (T) fm(x ...int) 472 473 func g0() {} 474 func g1() int { return 0} 475 func g2() (u float32, s string) { return } 476 func gs() []byte { return nil } 477 478 func _calls() { 479 var x int 480 var y float32 481 var s []int 482 483 f0() 484 _ = f0 /* ERROR "used as value" */ () 485 f0(g0 /* ERROR "too many arguments" */ ) 486 487 f1(0) 488 f1(x) 489 f1(10.0) 490 f1() /* ERROR "too few arguments" */ 491 f1(x, y /* ERROR "too many arguments" */ ) 492 f1(s /* ERROR "cannot use .* in argument" */ ) 493 f1(x ... /* ERROR "cannot use ..." */ ) 494 f1(g0 /* ERROR "used as value" */ ()) 495 f1(g1()) 496 // f1(g2()) // TODO(gri) missing position in error message 497 498 f2() /* ERROR "too few arguments" */ 499 f2(3.14) /* ERROR "too few arguments" */ 500 f2(3.14, "foo") 501 f2(x /* ERROR "cannot use .* in argument" */ , "foo") 502 f2(g0 /* ERROR "used as value" */ ()) 503 f2(g1 /* ERROR "cannot use .* in argument" */ ()) /* ERROR "too few arguments" */ 504 f2(g2()) 505 506 fs() /* ERROR "too few arguments" */ 507 fs(g0 /* ERROR "used as value" */ ()) 508 fs(g1 /* ERROR "cannot use .* in argument" */ ()) 509 fs(g2 /* ERROR "cannot use .* in argument" */ /* ERROR "too many arguments" */ ()) 510 fs(gs()) 511 512 fv() 513 fv(1, 2.0, x) 514 fv(s /* ERROR "cannot use .* in argument" */ ) 515 fv(s...) 516 fv(x /* ERROR "cannot use" */ ...) 517 fv(1, s... /* ERROR "can only use ... with matching parameter" */ ) 518 fv(gs /* ERROR "cannot use .* in argument" */ ()) 519 fv(gs /* ERROR "cannot use .* in argument" */ ()...) 520 521 var t T 522 t.fm() 523 t.fm(1, 2.0, x) 524 t.fm(s /* ERROR "cannot use .* in argument" */ ) 525 t.fm(g1()) 526 t.fm(1, s... /* ERROR "can only use ... with matching parameter" */ ) 527 t.fm(gs /* ERROR "cannot use .* in argument" */ ()) 528 t.fm(gs /* ERROR "cannot use .* in argument" */ ()...) 529 530 T.fm(t, ) 531 T.fm(t, 1, 2.0, x) 532 T.fm(t, s /* ERROR "cannot use .* in argument" */ ) 533 T.fm(t, g1()) 534 T.fm(t, 1, s... /* ERROR "can only use ... with matching parameter" */ ) 535 T.fm(t, gs /* ERROR "cannot use .* in argument" */ ()) 536 T.fm(t, gs /* ERROR "cannot use .* in argument" */ ()...) 537 538 var i interface{ fm(x ...int) } = t 539 i.fm() 540 i.fm(1, 2.0, x) 541 i.fm(s /* ERROR "cannot use .* in argument" */ ) 542 i.fm(g1()) 543 i.fm(1, s... /* ERROR "can only use ... with matching parameter" */ ) 544 i.fm(gs /* ERROR "cannot use .* in argument" */ ()) 545 i.fm(gs /* ERROR "cannot use .* in argument" */ ()...) 546 547 fi() 548 fi(1, 2.0, x, 3.14, "foo") 549 fi(g2()) 550 fi(0, g2) 551 fi(0, g2 /* ERROR "2-valued g2" */ ()) 552 } 553 554 func issue6344() { 555 type T []interface{} 556 var x T 557 fi(x...) // ... applies also to named slices 558 }