github.com/aloncn/graphics-go@v0.0.1/src/go/types/testdata/stmt0.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 // statements 6 7 package stmt0 8 9 func assignments0() (int, int) { 10 var a, b, c int 11 var ch chan int 12 f0 := func() {} 13 f1 := func() int { return 1 } 14 f2 := func() (int, int) { return 1, 2 } 15 f3 := func() (int, int, int) { return 1, 2, 3 } 16 17 a, b, c = 1, 2, 3 18 a, b, c = 1 /* ERROR "assignment count mismatch" */ , 2 19 a, b, c = 1 /* ERROR "assignment count mismatch" */ , 2, 3, 4 20 _, _, _ = a, b, c 21 22 a = f0 /* ERROR "used as value" */ () 23 a = f1() 24 a = f2 /* ERROR "assignment count mismatch" */ () 25 a, b = f2() 26 a, b, c = f2 /* ERROR "assignment count mismatch" */ () 27 a, b, c = f3() 28 a, b = f3 /* ERROR "assignment count mismatch" */ () 29 30 a, b, c = <- /* ERROR "assignment count mismatch" */ ch 31 32 return /* ERROR "wrong number of return values" */ 33 return /* ERROR "wrong number of return values" */ 1 34 return 1, 2 35 return /* ERROR "wrong number of return values" */ 1, 2, 3 36 } 37 38 func assignments1() { 39 b, i, f, c, s := false, 1, 1.0, 1i, "foo" 40 b = i /* ERROR "cannot use .* in assignment" */ 41 i = f /* ERROR "cannot use .* in assignment" */ 42 f = c /* ERROR "cannot use .* in assignment" */ 43 c = s /* ERROR "cannot use .* in assignment" */ 44 s = b /* ERROR "cannot use .* in assignment" */ 45 46 v0, v1, v2 := 1 /* ERROR "mismatch" */ , 2, 3, 4 47 _, _, _ = v0, v1, v2 48 49 b = true 50 51 i += 1 52 i += "foo" /* ERROR "cannot convert.*int" */ 53 54 f -= 1 55 f /= 0 56 f = float32(0)/0 /* ERROR "division by zero" */ 57 f -= "foo" /* ERROR "cannot convert.*float64" */ 58 59 c *= 1 60 c /= 0 61 62 s += "bar" 63 s += 1 /* ERROR "cannot convert.*string" */ 64 65 var u64 uint64 66 u64 += 1<<u64 67 68 undeclared /* ERROR "undeclared" */ = 991 69 70 // test cases for issue 5800 71 var ( 72 _ int = nil /* ERROR "untyped nil value" */ 73 _ [10]int = nil /* ERROR "untyped nil value" */ 74 _ []byte = nil 75 _ struct{} = nil /* ERROR "untyped nil value" */ 76 _ func() = nil 77 _ map[int]string = nil 78 _ chan int = nil 79 ) 80 81 // test cases for issue 5500 82 _ = func() (int, bool) { 83 var m map[int]int 84 return /* ERROR "wrong number of return values" */ m[0] 85 } 86 87 g := func(int, bool){} 88 var m map[int]int 89 g(m[0]) /* ERROR "too few arguments" */ 90 91 // assignments to _ 92 _ = nil /* ERROR "use of untyped nil" */ 93 _ = 1 /* ERROR overflow */ <<1000 94 (_) = 0 95 } 96 97 func assignments2() { 98 type mybool bool 99 var m map[string][]bool 100 var s []bool 101 var b bool 102 var d mybool 103 _ = s 104 _ = b 105 _ = d 106 107 // assignments to map index expressions are ok 108 s, b = m["foo"] 109 _, d = m["bar"] 110 m["foo"] = nil 111 m["foo"] = nil /* ERROR assignment count mismatch */ , false 112 _ = append(m["foo"]) 113 _ = append(m["foo"], true) 114 115 var c chan int 116 _, b = <-c 117 _, d = <-c 118 <- /* ERROR cannot assign */ c = 0 119 <-c = 0 /* ERROR assignment count mismatch */ , false 120 121 var x interface{} 122 _, b = x.(int) 123 x /* ERROR cannot assign */ .(int) = 0 124 x.(int) = 0 /* ERROR assignment count mismatch */ , false 125 126 assignments2 /* ERROR used as value */ () = nil 127 int /* ERROR not an expression */ = 0 128 } 129 130 func issue6487() { 131 type S struct{x int} 132 _ = &S /* ERROR "cannot take address" */ {}.x 133 _ = &( /* ERROR "cannot take address" */ S{}.x) 134 _ = (&S{}).x 135 S /* ERROR "cannot assign" */ {}.x = 0 136 (&S{}).x = 0 137 138 type M map[string]S 139 var m M 140 m /* ERROR "cannot assign" */ ["foo"].x = 0 141 _ = &( /* ERROR "cannot take address" */ m["foo"].x) 142 _ = &m /* ERROR "cannot take address" */ ["foo"].x 143 } 144 145 func issue6766a() { 146 a, a /* ERROR redeclared */ := 1, 2 147 _ = a 148 a, b, b /* ERROR redeclared */ := 1, 2, 3 149 _ = b 150 c, c /* ERROR redeclared */, b := 1, 2, 3 151 _ = c 152 a, b := /* ERROR no new variables */ 1, 2 153 } 154 155 func shortVarDecls1() { 156 const c = 0 157 type d int 158 a, b, c /* ERROR "cannot assign" */ , d /* ERROR "cannot assign" */ := 1, "zwei", 3.0, 4 159 var _ int = a // a is of type int 160 var _ string = b // b is of type string 161 } 162 163 func incdecs() { 164 const c = 3.14 165 c /* ERROR "cannot assign" */ ++ 166 s := "foo" 167 s /* ERROR "cannot convert" */ -- 168 3.14 /* ERROR "cannot assign" */ ++ 169 var ( 170 x int 171 y float32 172 z complex128 173 ) 174 x++ 175 y-- 176 z++ 177 } 178 179 func sends() { 180 var ch chan int 181 var rch <-chan int 182 var x int 183 x <- /* ERROR "cannot send" */ x 184 rch <- /* ERROR "cannot send" */ x 185 ch <- "foo" /* ERROR "cannot convert" */ 186 ch <- x 187 } 188 189 func selects() { 190 select {} 191 var ( 192 ch chan int 193 sc chan <- bool 194 ) 195 select { 196 case <-ch: 197 case (<-ch): 198 case t := <-ch: 199 _ = t 200 case t := (<-ch): 201 _ = t 202 case t, ok := <-ch: 203 _, _ = t, ok 204 case t, ok := (<-ch): 205 _, _ = t, ok 206 case <-sc /* ERROR "cannot receive from send-only channel" */ : 207 } 208 select { 209 default: 210 default /* ERROR "multiple defaults" */ : 211 } 212 select { 213 case a, b := <-ch: 214 _, b = a, b 215 case x /* ERROR send or receive */ : 216 case a /* ERROR send or receive */ := ch: 217 } 218 219 // test for issue 9570: ch2 in second case falsely resolved to 220 // ch2 declared in body of first case 221 ch1 := make(chan int) 222 ch2 := make(chan int) 223 select { 224 case <-ch1: 225 var ch2 /* ERROR ch2 declared but not used */ chan bool 226 case i := <-ch2: 227 print(i + 1) 228 } 229 } 230 231 func gos() { 232 go 1 /* ERROR HERE "function must be invoked" */ 233 go int /* ERROR "go requires function call, not conversion" */ (0) 234 go gos() 235 var c chan int 236 go close(c) 237 go len /* ERROR "go discards result" */ (c) 238 } 239 240 func defers() { 241 defer 1 /* ERROR HERE "function must be invoked" */ 242 defer int /* ERROR "defer requires function call, not conversion" */ (0) 243 defer defers() 244 var c chan int 245 defer close(c) 246 defer len /* ERROR "defer discards result" */ (c) 247 } 248 249 func breaks() { 250 var x, y int 251 252 break /* ERROR "break" */ 253 { 254 break /* ERROR "break" */ 255 } 256 if x < y { 257 break /* ERROR "break" */ 258 } 259 260 switch x { 261 case 0: 262 break 263 case 1: 264 if x == y { 265 break 266 } 267 default: 268 break 269 break 270 } 271 272 var z interface{} 273 switch z.(type) { 274 case int: 275 break 276 } 277 278 for { 279 break 280 } 281 282 var a []int 283 for _ = range a { 284 break 285 } 286 287 for { 288 if x == y { 289 break 290 } 291 } 292 293 var ch chan int 294 select { 295 case <-ch: 296 break 297 } 298 299 select { 300 case <-ch: 301 if x == y { 302 break 303 } 304 default: 305 break 306 } 307 } 308 309 func continues() { 310 var x, y int 311 312 continue /* ERROR "continue" */ 313 { 314 continue /* ERROR "continue" */ 315 } 316 317 if x < y { 318 continue /* ERROR "continue" */ 319 } 320 321 switch x { 322 case 0: 323 continue /* ERROR "continue" */ 324 } 325 326 var z interface{} 327 switch z.(type) { 328 case int: 329 continue /* ERROR "continue" */ 330 } 331 332 var ch chan int 333 select { 334 case <-ch: 335 continue /* ERROR "continue" */ 336 } 337 338 for i := 0; i < 10; i++ { 339 continue 340 if x < y { 341 continue 342 break 343 } 344 switch x { 345 case y: 346 continue 347 default: 348 break 349 } 350 select { 351 case <-ch: 352 continue 353 } 354 } 355 356 var a []int 357 for _ = range a { 358 continue 359 if x < y { 360 continue 361 break 362 } 363 switch x { 364 case y: 365 continue 366 default: 367 break 368 } 369 select { 370 case <-ch: 371 continue 372 } 373 } 374 } 375 376 func returns0() { 377 return 378 return 0 /* ERROR no result values expected */ 379 } 380 381 func returns1(x float64) (int, *float64) { 382 return 0, &x 383 return /* ERROR wrong number of return values */ 384 return "foo" /* ERROR "cannot convert" */, x /* ERROR "cannot use .* in return statement" */ 385 return /* ERROR wrong number of return values */ 0, &x, 1 386 } 387 388 func returns2() (a, b int) { 389 return 390 return 1, "foo" /* ERROR cannot convert */ 391 return /* ERROR wrong number of return values */ 1, 2, 3 392 { 393 type a int 394 return 1, 2 395 return /* ERROR a not in scope at return */ 396 } 397 } 398 399 func returns3() (_ int) { 400 return 401 { 402 var _ int // blank (_) identifiers never shadow since they are in no scope 403 return 404 } 405 } 406 407 func switches0() { 408 var x int 409 410 switch x { 411 } 412 413 switch x { 414 default: 415 default /* ERROR "multiple defaults" */ : 416 } 417 418 switch { 419 case 1 /* ERROR "cannot convert" */ : 420 } 421 422 true := "false" 423 _ = true 424 // A tagless switch is equivalent to the bool 425 // constant true, not the identifier 'true'. 426 switch { 427 case "false" /* ERROR "cannot convert" */: 428 } 429 430 switch int32(x) { 431 case 1, 2: 432 case x /* ERROR "cannot compare" */ : 433 } 434 435 switch x { 436 case 1 /* ERROR "overflows" */ << 100: 437 } 438 439 switch x { 440 case 1: 441 case 1 /* ERROR "duplicate case" */ : 442 case ( /* ERROR "duplicate case" */ 1): 443 case 2, 3, 4: 444 case 5, 1 /* ERROR "duplicate case" */ : 445 } 446 447 switch uint64(x) { 448 case 1<<64 - 1: 449 case 1 /* ERROR duplicate case */ <<64 - 1: 450 case 2, 3, 4: 451 case 5, 1 /* ERROR duplicate case */ <<64 - 1: 452 } 453 454 var y32 float32 455 switch y32 { 456 case 1.1: 457 case 11/10: // integer division! 458 case 11. /* ERROR duplicate case */ /10: 459 case 2, 3.0, 4.1: 460 case 5.2, 1.10 /* ERROR duplicate case */ : 461 } 462 463 var y64 float64 464 switch y64 { 465 case 1.1: 466 case 11/10: // integer division! 467 case 11. /* ERROR duplicate case */ /10: 468 case 2, 3.0, 4.1: 469 case 5.2, 1.10 /* ERROR duplicate case */ : 470 } 471 472 var s string 473 switch s { 474 case "foo": 475 case "foo" /* ERROR duplicate case */ : 476 case "f" /* ERROR duplicate case */ + "oo": 477 case "abc", "def", "ghi": 478 case "jkl", "foo" /* ERROR duplicate case */ : 479 } 480 481 type T int 482 type F float64 483 type S string 484 type B bool 485 var i interface{} 486 switch i { 487 case nil: 488 case nil: // no duplicate detection 489 case (*int)(nil): 490 case (*int)(nil): // do duplicate detection 491 case 1: 492 case byte(1): 493 case int /* ERROR duplicate case */ (1): 494 case T(1): 495 case 1.0: 496 case F(1.0): 497 case F /* ERROR duplicate case */ (1.0): 498 case "hello": 499 case S("hello"): 500 case S /* ERROR duplicate case */ ("hello"): 501 case 1==1, B(false): 502 case false, B(2==2): 503 } 504 505 // switch on array 506 var a [3]int 507 switch a { 508 case [3]int{1, 2, 3}: 509 case [3]int{1, 2, 3}: // no duplicate detection 510 case [ /* ERROR "mismatched types */ 4]int{4, 5, 6}: 511 } 512 513 // switch on channel 514 var c1, c2 chan int 515 switch c1 { 516 case nil: 517 case c1: 518 case c2: 519 case c1, c2: // no duplicate detection 520 } 521 } 522 523 func switches1() { 524 fallthrough /* ERROR "fallthrough statement out of place" */ 525 526 var x int 527 switch x { 528 case 0: 529 fallthrough /* ERROR "fallthrough statement out of place" */ 530 break 531 case 1: 532 fallthrough 533 case 2: 534 default: 535 fallthrough 536 case 3: 537 fallthrough /* ERROR "fallthrough statement out of place" */ 538 } 539 540 var y interface{} 541 switch y.(type) { 542 case int: 543 fallthrough /* ERROR "fallthrough statement out of place" */ 544 default: 545 } 546 547 switch x { 548 case 0: 549 if x == 0 { 550 fallthrough /* ERROR "fallthrough statement out of place" */ 551 } 552 } 553 554 switch x { 555 case 0: 556 goto L1 557 L1: fallthrough 558 case 1: 559 goto L2 560 goto L3 561 goto L4 562 L2: L3: L4: fallthrough 563 default: 564 } 565 566 switch x { 567 case 0: 568 goto L5 569 L5: fallthrough 570 default: 571 goto L6 572 goto L7 573 goto L8 574 L6: L7: L8: fallthrough /* ERROR "fallthrough statement out of place" */ 575 } 576 577 switch x { 578 case 0: 579 { 580 fallthrough /* ERROR "fallthrough statement out of place" */ 581 } 582 default: 583 } 584 } 585 586 func switches2() { 587 // untyped nil is not permitted as switch expression 588 switch nil /* ERROR "use of untyped nil" */ { 589 case 1, 2, "foo": // don't report additional errors here 590 } 591 592 // untyped constants are converted to default types 593 switch 1<<63-1 { 594 } 595 switch 1 /* ERROR "overflows int" */ << 63 { 596 } 597 var x int 598 switch 1.0 { 599 case 1.0, 2.0, x /* ERROR "mismatched types int and float64" */ : 600 } 601 switch x { 602 case 1.0: 603 } 604 605 // untyped bools become of type bool 606 type B bool 607 var b B = true 608 switch x == x { 609 case b /* ERROR "mismatched types B and bool" */ : 610 } 611 switch { 612 case b /* ERROR "mismatched types B and bool" */ : 613 } 614 } 615 616 func issue11667() { 617 switch 9223372036854775808 /* ERROR "overflows int" */ { 618 } 619 switch 9223372036854775808 /* ERROR "overflows int" */ { 620 case 9223372036854775808: 621 } 622 var x int 623 switch x { 624 case 9223372036854775808 /* ERROR "overflows int" */ : 625 } 626 var y float64 627 switch y { 628 case 9223372036854775808: 629 } 630 } 631 632 func issue11687() { 633 f := func() (_, _ int) { return } 634 switch f /* ERROR "2-valued f" */ () { 635 } 636 var x int 637 switch f /* ERROR "2-valued f" */ () { 638 case x: 639 } 640 switch x { 641 case f /* ERROR "2-valued f" */ (): 642 } 643 } 644 645 type I interface { 646 m() 647 } 648 649 type I2 interface { 650 m(int) 651 } 652 653 type T struct{} 654 type T1 struct{} 655 type T2 struct{} 656 657 func (T) m() {} 658 func (T2) m(int) {} 659 660 func typeswitches() { 661 var i int 662 var x interface{} 663 664 switch x.(type) {} 665 switch (x /* ERROR "outside type switch" */ .(type)) {} 666 667 switch x.(type) { 668 default: 669 default /* ERROR "multiple defaults" */ : 670 } 671 672 switch x /* ERROR "declared but not used" */ := x.(type) {} 673 switch _ /* ERROR "no new variable on left side of :=" */ := x.(type) {} 674 675 switch x := x.(type) { 676 case int: 677 var y int = x 678 _ = y 679 } 680 681 switch x := i /* ERROR "not an interface" */ .(type) {} 682 683 switch t := x.(type) { 684 case nil: 685 var v bool = t /* ERROR "cannot use .* in variable declaration" */ 686 _ = v 687 case int: 688 var v int = t 689 _ = v 690 case float32, complex64: 691 var v float32 = t /* ERROR "cannot use .* in variable declaration" */ 692 _ = v 693 default: 694 var v float32 = t /* ERROR "cannot use .* in variable declaration" */ 695 _ = v 696 } 697 698 var t I 699 switch t.(type) { 700 case T: 701 case T1 /* ERROR "missing method m" */ : 702 case T2 /* ERROR "wrong type for method m" */ : 703 case I2 /* STRICT "wrong type for method m" */ : // only an error in strict mode (issue 8561) 704 } 705 } 706 707 // Test that each case clause uses the correct type of the variable 708 // declared by the type switch (issue 5504). 709 func typeswitch0() { 710 switch y := interface{}(nil).(type) { 711 case int: 712 func() int { return y + 0 }() 713 case float32: 714 func() float32 { return y }() 715 } 716 } 717 718 // Test correct scope setup. 719 // (no redeclaration errors expected in the type switch) 720 func typeswitch1() { 721 var t I 722 switch t := t; t := t.(type) { 723 case nil: 724 var _ I = t 725 case T: 726 var _ T = t 727 default: 728 var _ I = t 729 } 730 } 731 732 // Test correct typeswitch against interface types. 733 type A interface { a() } 734 type B interface { b() } 735 type C interface { a(int) } 736 737 func typeswitch2() { 738 switch A(nil).(type) { 739 case A: 740 case B: 741 case C /* STRICT "cannot have dynamic type" */: // only an error in strict mode (issue 8561) 742 } 743 } 744 745 func typeswitch3(x interface{}) { 746 switch x.(type) { 747 case int: 748 case float64: 749 case int /* ERROR duplicate case */ : 750 } 751 752 switch x.(type) { 753 case nil: 754 case int: 755 case nil /* ERROR duplicate case */ , nil /* ERROR duplicate case */ : 756 } 757 758 type F func(int) 759 switch x.(type) { 760 case nil: 761 case int, func(int): 762 case float32, func /* ERROR duplicate case */ (x int): 763 case F: 764 } 765 } 766 767 func fors1() { 768 for {} 769 var i string 770 _ = i 771 for i := 0; i < 10; i++ {} 772 for i := 0; i < 10; j /* ERROR cannot declare */ := 0 {} 773 } 774 775 func rangeloops1() { 776 var ( 777 x int 778 a [10]float32 779 b []string 780 p *[10]complex128 781 pp **[10]complex128 782 s string 783 m map[int]bool 784 c chan int 785 sc chan<- int 786 rc <-chan int 787 ) 788 789 for range x /* ERROR "cannot range over" */ {} 790 for _ = range x /* ERROR "cannot range over" */ {} 791 for i := range x /* ERROR "cannot range over" */ {} 792 793 for range a {} 794 for i := range a { 795 var ii int 796 ii = i 797 _ = ii 798 } 799 for i, x := range a { 800 var ii int 801 ii = i 802 _ = ii 803 var xx float64 804 xx = x /* ERROR "cannot use .* in assignment" */ 805 _ = xx 806 } 807 var ii int 808 var xx float32 809 for ii, xx = range a {} 810 _, _ = ii, xx 811 812 for range b {} 813 for i := range b { 814 var ii int 815 ii = i 816 _ = ii 817 } 818 for i, x := range b { 819 var ii int 820 ii = i 821 _ = ii 822 var xx string 823 xx = x 824 _ = xx 825 } 826 827 for range s {} 828 for i := range s { 829 var ii int 830 ii = i 831 _ = ii 832 } 833 for i, x := range s { 834 var ii int 835 ii = i 836 _ = ii 837 var xx rune 838 xx = x 839 _ = xx 840 } 841 842 for range p {} 843 for _, x := range p { 844 var xx complex128 845 xx = x 846 _ = xx 847 } 848 849 for range pp /* ERROR "cannot range over" */ {} 850 for _, x := range pp /* ERROR "cannot range over" */ {} 851 852 for range m {} 853 for k := range m { 854 var kk int32 855 kk = k /* ERROR "cannot use .* in assignment" */ 856 _ = kk 857 } 858 for k, v := range m { 859 var kk int 860 kk = k 861 _ = kk 862 if v {} 863 } 864 865 for range c {} 866 for _, _ /* ERROR "only one iteration variable" */ = range c {} 867 for e := range c { 868 var ee int 869 ee = e 870 _ = ee 871 } 872 for _ = range sc /* ERROR "cannot range over send-only channel" */ {} 873 for _ = range rc {} 874 875 // constant strings 876 const cs = "foo" 877 for range cs {} 878 for range "" {} 879 for i, x := range cs { _, _ = i, x } 880 for i, x := range "" { 881 var ii int 882 ii = i 883 _ = ii 884 var xx rune 885 xx = x 886 _ = xx 887 } 888 } 889 890 func rangeloops2() { 891 type I int 892 type R rune 893 894 var a [10]int 895 var i I 896 _ = i 897 for i /* ERROR cannot use .* in assignment */ = range a {} 898 for i /* ERROR cannot use .* in assignment */ = range &a {} 899 for i /* ERROR cannot use .* in assignment */ = range a[:] {} 900 901 var s string 902 var r R 903 _ = r 904 for i /* ERROR cannot use .* in assignment */ = range s {} 905 for i /* ERROR cannot use .* in assignment */ = range "foo" {} 906 for _, r /* ERROR cannot use .* in assignment */ = range s {} 907 for _, r /* ERROR cannot use .* in assignment */ = range "foo" {} 908 } 909 910 func issue6766b() { 911 for _ := /* ERROR no new variables */ range "" {} 912 for a, a /* ERROR redeclared */ := range "" { _ = a } 913 var a int 914 _ = a 915 for a, a /* ERROR redeclared */ := range []int{1, 2, 3} { _ = a } 916 } 917 918 // Test that despite errors in the range clause, 919 // the loop body is still type-checked (and thus 920 // errors reported). 921 func issue10148() { 922 for y /* ERROR declared but not used */ := range "" { 923 _ = "" /* ERROR cannot convert */ + 1 924 } 925 for range 1 /* ERROR cannot range over 1 */ { 926 _ = "" /* ERROR cannot convert */ + 1 927 } 928 for y := range 1 /* ERROR cannot range over 1 */ { 929 _ = "" /* ERROR cannot convert */ + 1 930 } 931 } 932 933 func labels0() { 934 goto L0 935 goto L1 936 L0: 937 L1: 938 L1 /* ERROR "already declared" */ : 939 if true { 940 goto L2 941 L2: 942 L0 /* ERROR "already declared" */ : 943 } 944 _ = func() { 945 goto L0 946 goto L1 947 goto L2 948 L0: 949 L1: 950 L2: 951 } 952 } 953 954 func expression_statements(ch chan int) { 955 expression_statements(ch) 956 <-ch 957 println() 958 959 0 /* ERROR "not used" */ 960 1 /* ERROR "not used" */ +2 961 cap /* ERROR "not used" */ (ch) 962 println /* ERROR "must be called" */ 963 }