1 // Copyright 2020 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 types 6 7 type errorCode int 8 9 // This file defines the error codes that can be produced during type-checking. 10 // Collectively, these codes provide an identifier that may be used to 11 // implement special handling for certain types of errors. 12 // 13 // Error code values should not be changed: add new codes at the end. 14 // 15 // Error codes should be fine-grained enough that the exact nature of the error 16 // can be easily determined, but coarse enough that they are not an 17 // implementation detail of the type checking algorithm. As a rule-of-thumb, 18 // errors should be considered equivalent if there is a theoretical refactoring 19 // of the type checker in which they are emitted in exactly one place. For 20 // example, the type checker emits different error messages for "too many 21 // arguments" and "too few arguments", but one can imagine an alternative type 22 // checker where this check instead just emits a single "wrong number of 23 // arguments", so these errors should have the same code. 24 // 25 // Error code names should be as brief as possible while retaining accuracy and 26 // distinctiveness. In most cases names should start with an adjective 27 // describing the nature of the error (e.g. "invalid", "unused", "misplaced"), 28 // and end with a noun identifying the relevant language object. For example, 29 // "_DuplicateDecl" or "_InvalidSliceExpr". For brevity, naming follows the 30 // convention that "bad" implies a problem with syntax, and "invalid" implies a 31 // problem with types. 32 33 const ( 34 _ errorCode = iota 35 36 // _Test is reserved for errors that only apply while in self-test mode. 37 _Test 38 39 // _BlankPkgName occurs when a package name is the blank identifier "_". 40 // 41 // Per the spec: 42 // "The PackageName must not be the blank identifier." 43 _BlankPkgName 44 45 // _MismatchedPkgName occurs when a file's package name doesn't match the 46 // package name already established by other files. 47 _MismatchedPkgName 48 49 // _InvalidPkgUse occurs when a package identifier is used outside of a 50 // selector expression. 51 // 52 // Example: 53 // import "fmt" 54 // 55 // var _ = fmt 56 _InvalidPkgUse 57 58 // _BadImportPath occurs when an import path is not valid. 59 _BadImportPath 60 61 // _BrokenImport occurs when importing a package fails. 62 // 63 // Example: 64 // import "amissingpackage" 65 _BrokenImport 66 67 // _ImportCRenamed occurs when the special import "C" is renamed. "C" is a 68 // pseudo-package, and must not be renamed. 69 // 70 // Example: 71 // import _ "C" 72 _ImportCRenamed 73 74 // _UnusedImport occurs when an import is unused. 75 // 76 // Example: 77 // import "fmt" 78 // 79 // func main() {} 80 _UnusedImport 81 82 // _InvalidInitCycle occurs when an invalid cycle is detected within the 83 // initialization graph. 84 // 85 // Example: 86 // var x int = f() 87 // 88 // func f() int { return x } 89 _InvalidInitCycle 90 91 // _DuplicateDecl occurs when an identifier is declared multiple times. 92 // 93 // Example: 94 // var x = 1 95 // var x = 2 96 _DuplicateDecl 97 98 // _InvalidDeclCycle occurs when a declaration cycle is not valid. 99 // 100 // Example: 101 // import "unsafe" 102 // 103 // type T struct { 104 // a [n]int 105 // } 106 // 107 // var n = unsafe.Sizeof(T{}) 108 _InvalidDeclCycle 109 110 // _InvalidTypeCycle occurs when a cycle in type definitions results in a 111 // type that is not well-defined. 112 // 113 // Example: 114 // import "unsafe" 115 // 116 // type T [unsafe.Sizeof(T{})]int 117 _InvalidTypeCycle 118 119 // _InvalidConstInit occurs when a const declaration has a non-constant 120 // initializer. 121 // 122 // Example: 123 // var x int 124 // const _ = x 125 _InvalidConstInit 126 127 // _InvalidConstVal occurs when a const value cannot be converted to its 128 // target type. 129 // 130 // TODO(findleyr): this error code and example are not very clear. Consider 131 // removing it. 132 // 133 // Example: 134 // const _ = 1 << "hello" 135 _InvalidConstVal 136 137 // _InvalidConstType occurs when the underlying type in a const declaration 138 // is not a valid constant type. 139 // 140 // Example: 141 // const c *int = 4 142 _InvalidConstType 143 144 // _UntypedNil occurs when the predeclared (untyped) value nil is used to 145 // initialize a variable declared without an explicit type. 146 // 147 // Example: 148 // var x = nil 149 _UntypedNil 150 151 // _WrongAssignCount occurs when the number of values on the right-hand side 152 // of an assignment or initialization expression does not match the number 153 // of variables on the left-hand side. 154 // 155 // Example: 156 // var x = 1, 2 157 _WrongAssignCount 158 159 // _UnassignableOperand occurs when the left-hand side of an assignment is 160 // not assignable. 161 // 162 // Example: 163 // func f() { 164 // const c = 1 165 // c = 2 166 // } 167 _UnassignableOperand 168 169 // _NoNewVar occurs when a short variable declaration (':=') does not declare 170 // new variables. 171 // 172 // Example: 173 // func f() { 174 // x := 1 175 // x := 2 176 // } 177 _NoNewVar 178 179 // _MultiValAssignOp occurs when an assignment operation (+=, *=, etc) does 180 // not have single-valued left-hand or right-hand side. 181 // 182 // Per the spec: 183 // "In assignment operations, both the left- and right-hand expression lists 184 // must contain exactly one single-valued expression" 185 // 186 // Example: 187 // func f() int { 188 // x, y := 1, 2 189 // x, y += 1 190 // return x + y 191 // } 192 _MultiValAssignOp 193 194 // _InvalidIfaceAssign occurs when a value of type T is used as an 195 // interface, but T does not implement a method of the expected interface. 196 // 197 // Example: 198 // type I interface { 199 // f() 200 // } 201 // 202 // type T int 203 // 204 // var x I = T(1) 205 _InvalidIfaceAssign 206 207 // _InvalidChanAssign occurs when a chan assignment is invalid. 208 // 209 // Per the spec, a value x is assignable to a channel type T if: 210 // "x is a bidirectional channel value, T is a channel type, x's type V and 211 // T have identical element types, and at least one of V or T is not a 212 // defined type." 213 // 214 // Example: 215 // type T1 chan int 216 // type T2 chan int 217 // 218 // var x T1 219 // // Invalid assignment because both types are named 220 // var _ T2 = x 221 _InvalidChanAssign 222 223 // _IncompatibleAssign occurs when the type of the right-hand side expression 224 // in an assignment cannot be assigned to the type of the variable being 225 // assigned. 226 // 227 // Example: 228 // var x []int 229 // var _ int = x 230 _IncompatibleAssign 231 232 // _UnaddressableFieldAssign occurs when trying to assign to a struct field 233 // in a map value. 234 // 235 // Example: 236 // func f() { 237 // m := make(map[string]struct{i int}) 238 // m["foo"].i = 42 239 // } 240 _UnaddressableFieldAssign 241 242 // _NotAType occurs when the identifier used as the underlying type in a type 243 // declaration or the right-hand side of a type alias does not denote a type. 244 // 245 // Example: 246 // var S = 2 247 // 248 // type T S 249 _NotAType 250 251 // _InvalidArrayLen occurs when an array length is not a constant value. 252 // 253 // Example: 254 // var n = 3 255 // var _ = [n]int{} 256 _InvalidArrayLen 257 258 // _BlankIfaceMethod occurs when a method name is '_'. 259 // 260 // Per the spec: 261 // "The name of each explicitly specified method must be unique and not 262 // blank." 263 // 264 // Example: 265 // type T interface { 266 // _(int) 267 // } 268 _BlankIfaceMethod 269 270 // _IncomparableMapKey occurs when a map key type does not support the == and 271 // != operators. 272 // 273 // Per the spec: 274 // "The comparison operators == and != must be fully defined for operands of 275 // the key type; thus the key type must not be a function, map, or slice." 276 // 277 // Example: 278 // var x map[T]int 279 // 280 // type T []int 281 _IncomparableMapKey 282 283 // _InvalidIfaceEmbed occurs when a non-interface type is embedded in an 284 // interface. 285 // 286 // Example: 287 // type T struct {} 288 // 289 // func (T) m() 290 // 291 // type I interface { 292 // T 293 // } 294 _InvalidIfaceEmbed 295 296 // _InvalidPtrEmbed occurs when an embedded field is of the pointer form *T, 297 // and T itself is itself a pointer, an unsafe.Pointer, or an interface. 298 // 299 // Per the spec: 300 // "An embedded field must be specified as a type name T or as a pointer to 301 // a non-interface type name *T, and T itself may not be a pointer type." 302 // 303 // Example: 304 // type T *int 305 // 306 // type S struct { 307 // *T 308 // } 309 _InvalidPtrEmbed 310 311 // _BadRecv occurs when a method declaration does not have exactly one 312 // receiver parameter. 313 // 314 // Example: 315 // func () _() {} 316 _BadRecv 317 318 // _InvalidRecv occurs when a receiver type expression is not of the form T 319 // or *T, or T is a pointer type. 320 // 321 // Example: 322 // type T struct {} 323 // 324 // func (**T) m() {} 325 _InvalidRecv 326 327 // _DuplicateFieldAndMethod occurs when an identifier appears as both a field 328 // and method name. 329 // 330 // Example: 331 // type T struct { 332 // m int 333 // } 334 // 335 // func (T) m() {} 336 _DuplicateFieldAndMethod 337 338 // _DuplicateMethod occurs when two methods on the same receiver type have 339 // the same name. 340 // 341 // Example: 342 // type T struct {} 343 // func (T) m() {} 344 // func (T) m(i int) int { return i } 345 _DuplicateMethod 346 347 // _InvalidBlank occurs when a blank identifier is used as a value or type. 348 // 349 // Per the spec: 350 // "The blank identifier may appear as an operand only on the left-hand side 351 // of an assignment." 352 // 353 // Example: 354 // var x = _ 355 _InvalidBlank 356 357 // _InvalidIota occurs when the predeclared identifier iota is used outside 358 // of a constant declaration. 359 // 360 // Example: 361 // var x = iota 362 _InvalidIota 363 364 // _MissingInitBody occurs when an init function is missing its body. 365 // 366 // Example: 367 // func init() 368 _MissingInitBody 369 370 // _InvalidInitSig occurs when an init function declares parameters or 371 // results. 372 // 373 // Deprecated: no longer emitted by the type checker. _InvalidInitDecl is 374 // used instead. 375 _InvalidInitSig 376 377 // _InvalidInitDecl occurs when init is declared as anything other than a 378 // function. 379 // 380 // Example: 381 // var init = 1 382 // 383 // Example: 384 // func init() int { return 1 } 385 _InvalidInitDecl 386 387 // _InvalidMainDecl occurs when main is declared as anything other than a 388 // function, in a main package. 389 _InvalidMainDecl 390 391 // _TooManyValues occurs when a function returns too many values for the 392 // expression context in which it is used. 393 // 394 // Example: 395 // func ReturnTwo() (int, int) { 396 // return 1, 2 397 // } 398 // 399 // var x = ReturnTwo() 400 _TooManyValues 401 402 // _NotAnExpr occurs when a type expression is used where a value expression 403 // is expected. 404 // 405 // Example: 406 // type T struct {} 407 // 408 // func f() { 409 // T 410 // } 411 _NotAnExpr 412 413 // _TruncatedFloat occurs when a float constant is truncated to an integer 414 // value. 415 // 416 // Example: 417 // var _ int = 98.6 418 _TruncatedFloat 419 420 // _NumericOverflow occurs when a numeric constant overflows its target type. 421 // 422 // Example: 423 // var x int8 = 1000 424 _NumericOverflow 425 426 // _UndefinedOp occurs when an operator is not defined for the type(s) used 427 // in an operation. 428 // 429 // Example: 430 // var c = "a" - "b" 431 _UndefinedOp 432 433 // _MismatchedTypes occurs when operand types are incompatible in a binary 434 // operation. 435 // 436 // Example: 437 // var a = "hello" 438 // var b = 1 439 // var c = a - b 440 _MismatchedTypes 441 442 // _DivByZero occurs when a division operation is provable at compile 443 // time to be a division by zero. 444 // 445 // Example: 446 // const divisor = 0 447 // var x int = 1/divisor 448 _DivByZero 449 450 // _NonNumericIncDec occurs when an increment or decrement operator is 451 // applied to a non-numeric value. 452 // 453 // Example: 454 // func f() { 455 // var c = "c" 456 // c++ 457 // } 458 _NonNumericIncDec 459 460 // _UnaddressableOperand occurs when the & operator is applied to an 461 // unaddressable expression. 462 // 463 // Example: 464 // var x = &1 465 _UnaddressableOperand 466 467 // _InvalidIndirection occurs when a non-pointer value is indirected via the 468 // '*' operator. 469 // 470 // Example: 471 // var x int 472 // var y = *x 473 _InvalidIndirection 474 475 // _NonIndexableOperand occurs when an index operation is applied to a value 476 // that cannot be indexed. 477 // 478 // Example: 479 // var x = 1 480 // var y = x[1] 481 _NonIndexableOperand 482 483 // _InvalidIndex occurs when an index argument is not of integer type, 484 // negative, or out-of-bounds. 485 // 486 // Example: 487 // var s = [...]int{1,2,3} 488 // var x = s[5] 489 // 490 // Example: 491 // var s = []int{1,2,3} 492 // var _ = s[-1] 493 // 494 // Example: 495 // var s = []int{1,2,3} 496 // var i string 497 // var _ = s[i] 498 _InvalidIndex 499 500 // _SwappedSliceIndices occurs when constant indices in a slice expression 501 // are decreasing in value. 502 // 503 // Example: 504 // var _ = []int{1,2,3}[2:1] 505 _SwappedSliceIndices 506 507 // _NonSliceableOperand occurs when a slice operation is applied to a value 508 // whose type is not sliceable, or is unaddressable. 509 // 510 // Example: 511 // var x = [...]int{1, 2, 3}[:1] 512 // 513 // Example: 514 // var x = 1 515 // var y = 1[:1] 516 _NonSliceableOperand 517 518 // _InvalidSliceExpr occurs when a three-index slice expression (a[x:y:z]) is 519 // applied to a string. 520 // 521 // Example: 522 // var s = "hello" 523 // var x = s[1:2:3] 524 _InvalidSliceExpr 525 526 // _InvalidShiftCount occurs when the right-hand side of a shift operation is 527 // either non-integer, negative, or too large. 528 // 529 // Example: 530 // var ( 531 // x string 532 // y int = 1 << x 533 // ) 534 _InvalidShiftCount 535 536 // _InvalidShiftOperand occurs when the shifted operand is not an integer. 537 // 538 // Example: 539 // var s = "hello" 540 // var x = s << 2 541 _InvalidShiftOperand 542 543 // _InvalidReceive occurs when there is a channel receive from a value that 544 // is either not a channel, or is a send-only channel. 545 // 546 // Example: 547 // func f() { 548 // var x = 1 549 // <-x 550 // } 551 _InvalidReceive 552 553 // _InvalidSend occurs when there is a channel send to a value that is not a 554 // channel, or is a receive-only channel. 555 // 556 // Example: 557 // func f() { 558 // var x = 1 559 // x <- "hello!" 560 // } 561 _InvalidSend 562 563 // _DuplicateLitKey occurs when an index is duplicated in a slice, array, or 564 // map literal. 565 // 566 // Example: 567 // var _ = []int{0:1, 0:2} 568 // 569 // Example: 570 // var _ = map[string]int{"a": 1, "a": 2} 571 _DuplicateLitKey 572 573 // _MissingLitKey occurs when a map literal is missing a key expression. 574 // 575 // Example: 576 // var _ = map[string]int{1} 577 _MissingLitKey 578 579 // _InvalidLitIndex occurs when the key in a key-value element of a slice or 580 // array literal is not an integer constant. 581 // 582 // Example: 583 // var i = 0 584 // var x = []string{i: "world"} 585 _InvalidLitIndex 586 587 // _OversizeArrayLit occurs when an array literal exceeds its length. 588 // 589 // Example: 590 // var _ = [2]int{1,2,3} 591 _OversizeArrayLit 592 593 // _MixedStructLit occurs when a struct literal contains a mix of positional 594 // and named elements. 595 // 596 // Example: 597 // var _ = struct{i, j int}{i: 1, 2} 598 _MixedStructLit 599 600 // _InvalidStructLit occurs when a positional struct literal has an incorrect 601 // number of values. 602 // 603 // Example: 604 // var _ = struct{i, j int}{1,2,3} 605 _InvalidStructLit 606 607 // _MissingLitField occurs when a struct literal refers to a field that does 608 // not exist on the struct type. 609 // 610 // Example: 611 // var _ = struct{i int}{j: 2} 612 _MissingLitField 613 614 // _DuplicateLitField occurs when a struct literal contains duplicated 615 // fields. 616 // 617 // Example: 618 // var _ = struct{i int}{i: 1, i: 2} 619 _DuplicateLitField 620 621 // _UnexportedLitField occurs when a positional struct literal implicitly 622 // assigns an unexported field of an imported type. 623 _UnexportedLitField 624 625 // _InvalidLitField occurs when a field name is not a valid identifier. 626 // 627 // Example: 628 // var _ = struct{i int}{1: 1} 629 _InvalidLitField 630 631 // _UntypedLit occurs when a composite literal omits a required type 632 // identifier. 633 // 634 // Example: 635 // type outer struct{ 636 // inner struct { i int } 637 // } 638 // 639 // var _ = outer{inner: {1}} 640 _UntypedLit 641 642 // _InvalidLit occurs when a composite literal expression does not match its 643 // type. 644 // 645 // Example: 646 // type P *struct{ 647 // x int 648 // } 649 // var _ = P {} 650 _InvalidLit 651 652 // _AmbiguousSelector occurs when a selector is ambiguous. 653 // 654 // Example: 655 // type E1 struct { i int } 656 // type E2 struct { i int } 657 // type T struct { E1; E2 } 658 // 659 // var x T 660 // var _ = x.i 661 _AmbiguousSelector 662 663 // _UndeclaredImportedName occurs when a package-qualified identifier is 664 // undeclared by the imported package. 665 // 666 // Example: 667 // import "go/types" 668 // 669 // var _ = types.NotAnActualIdentifier 670 _UndeclaredImportedName 671 672 // _UnexportedName occurs when a selector refers to an unexported identifier 673 // of an imported package. 674 // 675 // Example: 676 // import "reflect" 677 // 678 // type _ reflect.flag 679 _UnexportedName 680 681 // _UndeclaredName occurs when an identifier is not declared in the current 682 // scope. 683 // 684 // Example: 685 // var x T 686 _UndeclaredName 687 688 // _MissingFieldOrMethod occurs when a selector references a field or method 689 // that does not exist. 690 // 691 // Example: 692 // type T struct {} 693 // 694 // var x = T{}.f 695 _MissingFieldOrMethod 696 697 // _BadDotDotDotSyntax occurs when a "..." occurs in a context where it is 698 // not valid. 699 // 700 // Example: 701 // var _ = map[int][...]int{0: {}} 702 _BadDotDotDotSyntax 703 704 // _NonVariadicDotDotDot occurs when a "..." is used on the final argument to 705 // a non-variadic function. 706 // 707 // Example: 708 // func printArgs(s []string) { 709 // for _, a := range s { 710 // println(a) 711 // } 712 // } 713 // 714 // func f() { 715 // s := []string{"a", "b", "c"} 716 // printArgs(s...) 717 // } 718 _NonVariadicDotDotDot 719 720 // _MisplacedDotDotDot occurs when a "..." is used somewhere other than the 721 // final argument in a function declaration. 722 // 723 // Example: 724 // func f(...int, int) 725 _MisplacedDotDotDot 726 727 _ // _InvalidDotDotDotOperand was removed. 728 729 // _InvalidDotDotDot occurs when a "..." is used in a non-variadic built-in 730 // function. 731 // 732 // Example: 733 // var s = []int{1, 2, 3} 734 // var l = len(s...) 735 _InvalidDotDotDot 736 737 // _UncalledBuiltin occurs when a built-in function is used as a 738 // function-valued expression, instead of being called. 739 // 740 // Per the spec: 741 // "The built-in functions do not have standard Go types, so they can only 742 // appear in call expressions; they cannot be used as function values." 743 // 744 // Example: 745 // var _ = copy 746 _UncalledBuiltin 747 748 // _InvalidAppend occurs when append is called with a first argument that is 749 // not a slice. 750 // 751 // Example: 752 // var _ = append(1, 2) 753 _InvalidAppend 754 755 // _InvalidCap occurs when an argument to the cap built-in function is not of 756 // supported type. 757 // 758 // See https://golang.org/ref/spec#Length_and_capacity for information on 759 // which underlying types are supported as arguments to cap and len. 760 // 761 // Example: 762 // var s = 2 763 // var x = cap(s) 764 _InvalidCap 765 766 // _InvalidClose occurs when close(...) is called with an argument that is 767 // not of channel type, or that is a receive-only channel. 768 // 769 // Example: 770 // func f() { 771 // var x int 772 // close(x) 773 // } 774 _InvalidClose 775 776 // _InvalidCopy occurs when the arguments are not of slice type or do not 777 // have compatible type. 778 // 779 // See https://golang.org/ref/spec#Appending_and_copying_slices for more 780 // information on the type requirements for the copy built-in. 781 // 782 // Example: 783 // func f() { 784 // var x []int 785 // y := []int64{1,2,3} 786 // copy(x, y) 787 // } 788 _InvalidCopy 789 790 // _InvalidComplex occurs when the complex built-in function is called with 791 // arguments with incompatible types. 792 // 793 // Example: 794 // var _ = complex(float32(1), float64(2)) 795 _InvalidComplex 796 797 // _InvalidDelete occurs when the delete built-in function is called with a 798 // first argument that is not a map. 799 // 800 // Example: 801 // func f() { 802 // m := "hello" 803 // delete(m, "e") 804 // } 805 _InvalidDelete 806 807 // _InvalidImag occurs when the imag built-in function is called with an 808 // argument that does not have complex type. 809 // 810 // Example: 811 // var _ = imag(int(1)) 812 _InvalidImag 813 814 // _InvalidLen occurs when an argument to the len built-in function is not of 815 // supported type. 816 // 817 // See https://golang.org/ref/spec#Length_and_capacity for information on 818 // which underlying types are supported as arguments to cap and len. 819 // 820 // Example: 821 // var s = 2 822 // var x = len(s) 823 _InvalidLen 824 825 // _SwappedMakeArgs occurs when make is called with three arguments, and its 826 // length argument is larger than its capacity argument. 827 // 828 // Example: 829 // var x = make([]int, 3, 2) 830 _SwappedMakeArgs 831 832 // _InvalidMake occurs when make is called with an unsupported type argument. 833 // 834 // See https://golang.org/ref/spec#Making_slices_maps_and_channels for 835 // information on the types that may be created using make. 836 // 837 // Example: 838 // var x = make(int) 839 _InvalidMake 840 841 // _InvalidReal occurs when the real built-in function is called with an 842 // argument that does not have complex type. 843 // 844 // Example: 845 // var _ = real(int(1)) 846 _InvalidReal 847 848 // _InvalidAssert occurs when a type assertion is applied to a 849 // value that is not of interface type. 850 // 851 // Example: 852 // var x = 1 853 // var _ = x.(float64) 854 _InvalidAssert 855 856 // _ImpossibleAssert occurs for a type assertion x.(T) when the value x of 857 // interface cannot have dynamic type T, due to a missing or mismatching 858 // method on T. 859 // 860 // Example: 861 // type T int 862 // 863 // func (t *T) m() int { return int(*t) } 864 // 865 // type I interface { m() int } 866 // 867 // var x I 868 // var _ = x.(T) 869 _ImpossibleAssert 870 871 // _InvalidConversion occurs when the argument type cannot be converted to the 872 // target. 873 // 874 // See https://golang.org/ref/spec#Conversions for the rules of 875 // convertibility. 876 // 877 // Example: 878 // var x float64 879 // var _ = string(x) 880 _InvalidConversion 881 882 // _InvalidUntypedConversion occurs when an there is no valid implicit 883 // conversion from an untyped value satisfying the type constraints of the 884 // context in which it is used. 885 // 886 // Example: 887 // var _ = 1 + "" 888 _InvalidUntypedConversion 889 890 // _BadOffsetofSyntax occurs when unsafe.Offsetof is called with an argument 891 // that is not a selector expression. 892 // 893 // Example: 894 // import "unsafe" 895 // 896 // var x int 897 // var _ = unsafe.Offsetof(x) 898 _BadOffsetofSyntax 899 900 // _InvalidOffsetof occurs when unsafe.Offsetof is called with a method 901 // selector, rather than a field selector, or when the field is embedded via 902 // a pointer. 903 // 904 // Per the spec: 905 // 906 // "If f is an embedded field, it must be reachable without pointer 907 // indirections through fields of the struct. " 908 // 909 // Example: 910 // import "unsafe" 911 // 912 // type T struct { f int } 913 // type S struct { *T } 914 // var s S 915 // var _ = unsafe.Offsetof(s.f) 916 // 917 // Example: 918 // import "unsafe" 919 // 920 // type S struct{} 921 // 922 // func (S) m() {} 923 // 924 // var s S 925 // var _ = unsafe.Offsetof(s.m) 926 _InvalidOffsetof 927 928 // _UnusedExpr occurs when a side-effect free expression is used as a 929 // statement. Such a statement has no effect. 930 // 931 // Example: 932 // func f(i int) { 933 // i*i 934 // } 935 _UnusedExpr 936 937 // _UnusedVar occurs when a variable is declared but unused. 938 // 939 // Example: 940 // func f() { 941 // x := 1 942 // } 943 _UnusedVar 944 945 // _MissingReturn occurs when a function with results is missing a return 946 // statement. 947 // 948 // Example: 949 // func f() int {} 950 _MissingReturn 951 952 // _WrongResultCount occurs when a return statement returns an incorrect 953 // number of values. 954 // 955 // Example: 956 // func ReturnOne() int { 957 // return 1, 2 958 // } 959 _WrongResultCount 960 961 // _OutOfScopeResult occurs when the name of a value implicitly returned by 962 // an empty return statement is shadowed in a nested scope. 963 // 964 // Example: 965 // func factor(n int) (i int) { 966 // for i := 2; i < n; i++ { 967 // if n%i == 0 { 968 // return 969 // } 970 // } 971 // return 0 972 // } 973 _OutOfScopeResult 974 975 // _InvalidCond occurs when an if condition is not a boolean expression. 976 // 977 // Example: 978 // func checkReturn(i int) { 979 // if i { 980 // panic("non-zero return") 981 // } 982 // } 983 _InvalidCond 984 985 // _InvalidPostDecl occurs when there is a declaration in a for-loop post 986 // statement. 987 // 988 // Example: 989 // func f() { 990 // for i := 0; i < 10; j := 0 {} 991 // } 992 _InvalidPostDecl 993 994 _ // _InvalidChanRange was removed. 995 996 // _InvalidIterVar occurs when two iteration variables are used while ranging 997 // over a channel. 998 // 999 // Example: 1000 // func f(c chan int) { 1001 // for k, v := range c { 1002 // println(k, v) 1003 // } 1004 // } 1005 _InvalidIterVar 1006 1007 // _InvalidRangeExpr occurs when the type of a range expression is not array, 1008 // slice, string, map, or channel. 1009 // 1010 // Example: 1011 // func f(i int) { 1012 // for j := range i { 1013 // println(j) 1014 // } 1015 // } 1016 _InvalidRangeExpr 1017 1018 // _MisplacedBreak occurs when a break statement is not within a for, switch, 1019 // or select statement of the innermost function definition. 1020 // 1021 // Example: 1022 // func f() { 1023 // break 1024 // } 1025 _MisplacedBreak 1026 1027 // _MisplacedContinue occurs when a continue statement is not within a for 1028 // loop of the innermost function definition. 1029 // 1030 // Example: 1031 // func sumeven(n int) int { 1032 // proceed := func() { 1033 // continue 1034 // } 1035 // sum := 0 1036 // for i := 1; i <= n; i++ { 1037 // if i % 2 != 0 { 1038 // proceed() 1039 // } 1040 // sum += i 1041 // } 1042 // return sum 1043 // } 1044 _MisplacedContinue 1045 1046 // _MisplacedFallthrough occurs when a fallthrough statement is not within an 1047 // expression switch. 1048 // 1049 // Example: 1050 // func typename(i interface{}) string { 1051 // switch i.(type) { 1052 // case int64: 1053 // fallthrough 1054 // case int: 1055 // return "int" 1056 // } 1057 // return "unsupported" 1058 // } 1059 _MisplacedFallthrough 1060 1061 // _DuplicateCase occurs when a type or expression switch has duplicate 1062 // cases. 1063 // 1064 // Example: 1065 // func printInt(i int) { 1066 // switch i { 1067 // case 1: 1068 // println("one") 1069 // case 1: 1070 // println("One") 1071 // } 1072 // } 1073 _DuplicateCase 1074 1075 // _DuplicateDefault occurs when a type or expression switch has multiple 1076 // default clauses. 1077 // 1078 // Example: 1079 // func printInt(i int) { 1080 // switch i { 1081 // case 1: 1082 // println("one") 1083 // default: 1084 // println("One") 1085 // default: 1086 // println("1") 1087 // } 1088 // } 1089 _DuplicateDefault 1090 1091 // _BadTypeKeyword occurs when a .(type) expression is used anywhere other 1092 // than a type switch. 1093 // 1094 // Example: 1095 // type I interface { 1096 // m() 1097 // } 1098 // var t I 1099 // var _ = t.(type) 1100 _BadTypeKeyword 1101 1102 // _InvalidTypeSwitch occurs when .(type) is used on an expression that is 1103 // not of interface type. 1104 // 1105 // Example: 1106 // func f(i int) { 1107 // switch x := i.(type) {} 1108 // } 1109 _InvalidTypeSwitch 1110 1111 // _InvalidExprSwitch occurs when a switch expression is not comparable. 1112 // 1113 // Example: 1114 // func _() { 1115 // var a struct{ _ func() } 1116 // switch a /* ERROR cannot switch on a */ { 1117 // } 1118 // } 1119 _InvalidExprSwitch 1120 1121 // _InvalidSelectCase occurs when a select case is not a channel send or 1122 // receive. 1123 // 1124 // Example: 1125 // func checkChan(c <-chan int) bool { 1126 // select { 1127 // case c: 1128 // return true 1129 // default: 1130 // return false 1131 // } 1132 // } 1133 _InvalidSelectCase 1134 1135 // _UndeclaredLabel occurs when an undeclared label is jumped to. 1136 // 1137 // Example: 1138 // func f() { 1139 // goto L 1140 // } 1141 _UndeclaredLabel 1142 1143 // _DuplicateLabel occurs when a label is declared more than once. 1144 // 1145 // Example: 1146 // func f() int { 1147 // L: 1148 // L: 1149 // return 1 1150 // } 1151 _DuplicateLabel 1152 1153 // _MisplacedLabel occurs when a break or continue label is not on a for, 1154 // switch, or select statement. 1155 // 1156 // Example: 1157 // func f() { 1158 // L: 1159 // a := []int{1,2,3} 1160 // for _, e := range a { 1161 // if e > 10 { 1162 // break L 1163 // } 1164 // println(a) 1165 // } 1166 // } 1167 _MisplacedLabel 1168 1169 // _UnusedLabel occurs when a label is declared but not used. 1170 // 1171 // Example: 1172 // func f() { 1173 // L: 1174 // } 1175 _UnusedLabel 1176 1177 // _JumpOverDecl occurs when a label jumps over a variable declaration. 1178 // 1179 // Example: 1180 // func f() int { 1181 // goto L 1182 // x := 2 1183 // L: 1184 // x++ 1185 // return x 1186 // } 1187 _JumpOverDecl 1188 1189 // _JumpIntoBlock occurs when a forward jump goes to a label inside a nested 1190 // block. 1191 // 1192 // Example: 1193 // func f(x int) { 1194 // goto L 1195 // if x > 0 { 1196 // L: 1197 // print("inside block") 1198 // } 1199 // } 1200 _JumpIntoBlock 1201 1202 // _InvalidMethodExpr occurs when a pointer method is called but the argument 1203 // is not addressable. 1204 // 1205 // Example: 1206 // type T struct {} 1207 // 1208 // func (*T) m() int { return 1 } 1209 // 1210 // var _ = T.m(T{}) 1211 _InvalidMethodExpr 1212 1213 // _WrongArgCount occurs when too few or too many arguments are passed by a 1214 // function call. 1215 // 1216 // Example: 1217 // func f(i int) {} 1218 // var x = f() 1219 _WrongArgCount 1220 1221 // _InvalidCall occurs when an expression is called that is not of function 1222 // type. 1223 // 1224 // Example: 1225 // var x = "x" 1226 // var y = x() 1227 _InvalidCall 1228 1229 // _UnusedResults occurs when a restricted expression-only built-in function 1230 // is suspended via go or defer. Such a suspension discards the results of 1231 // these side-effect free built-in functions, and therefore is ineffectual. 1232 // 1233 // Example: 1234 // func f(a []int) int { 1235 // defer len(a) 1236 // return i 1237 // } 1238 _UnusedResults 1239 1240 // _InvalidDefer occurs when a deferred expression is not a function call, 1241 // for example if the expression is a type conversion. 1242 // 1243 // Example: 1244 // func f(i int) int { 1245 // defer int32(i) 1246 // return i 1247 // } 1248 _InvalidDefer 1249 1250 // _InvalidGo occurs when a go expression is not a function call, for example 1251 // if the expression is a type conversion. 1252 // 1253 // Example: 1254 // func f(i int) int { 1255 // go int32(i) 1256 // return i 1257 // } 1258 _InvalidGo 1259 1260 // All codes below were added in Go 1.17. 1261 1262 // _BadDecl occurs when a declaration has invalid syntax. 1263 _BadDecl 1264 1265 // _RepeatedDecl occurs when an identifier occurs more than once on the left 1266 // hand side of a short variable declaration. 1267 // 1268 // Example: 1269 // func _() { 1270 // x, y, y := 1, 2, 3 1271 // } 1272 _RepeatedDecl 1273 1274 // _InvalidUnsafeAdd occurs when unsafe.Add is called with a 1275 // length argument that is not of integer type. 1276 // 1277 // Example: 1278 // import "unsafe" 1279 // 1280 // var p unsafe.Pointer 1281 // var _ = unsafe.Add(p, float64(1)) 1282 _InvalidUnsafeAdd 1283 1284 // _InvalidUnsafeSlice occurs when unsafe.Slice is called with a 1285 // pointer argument that is not of pointer type or a length argument 1286 // that is not of integer type, negative, or out of bounds. 1287 // 1288 // Example: 1289 // import "unsafe" 1290 // 1291 // var x int 1292 // var _ = unsafe.Slice(x, 1) 1293 // 1294 // Example: 1295 // import "unsafe" 1296 // 1297 // var x int 1298 // var _ = unsafe.Slice(&x, float64(1)) 1299 // 1300 // Example: 1301 // import "unsafe" 1302 // 1303 // var x int 1304 // var _ = unsafe.Slice(&x, -1) 1305 // 1306 // Example: 1307 // import "unsafe" 1308 // 1309 // var x int 1310 // var _ = unsafe.Slice(&x, uint64(1) << 63) 1311 _InvalidUnsafeSlice 1312 1313 // _Todo is a placeholder for error codes that have not been decided. 1314 // TODO(rFindley) remove this error code after deciding on errors for generics code. 1315 _Todo 1316 ) 1317