Source file src/internal/types/errors/codes.go
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 errors 6 7 //go:generate go run golang.org/x/tools/cmd/stringer@latest -type Code codes.go 8 9 type Code int 10 11 // This file defines the error codes that can be produced during type-checking. 12 // Collectively, these codes provide an identifier that may be used to 13 // implement special handling for certain types of errors. 14 // 15 // Error code values should not be changed: add new codes at the end. 16 // 17 // Error codes should be fine-grained enough that the exact nature of the error 18 // can be easily determined, but coarse enough that they are not an 19 // implementation detail of the type checking algorithm. As a rule-of-thumb, 20 // errors should be considered equivalent if there is a theoretical refactoring 21 // of the type checker in which they are emitted in exactly one place. For 22 // example, the type checker emits different error messages for "too many 23 // arguments" and "too few arguments", but one can imagine an alternative type 24 // checker where this check instead just emits a single "wrong number of 25 // arguments", so these errors should have the same code. 26 // 27 // Error code names should be as brief as possible while retaining accuracy and 28 // distinctiveness. In most cases names should start with an adjective 29 // describing the nature of the error (e.g. "invalid", "unused", "misplaced"), 30 // and end with a noun identifying the relevant language object. For example, 31 // "_DuplicateDecl" or "_InvalidSliceExpr". For brevity, naming follows the 32 // convention that "bad" implies a problem with syntax, and "invalid" implies a 33 // problem with types. 34 35 const ( 36 // InvalidSyntaxTree occurs if an invalid syntax tree is provided 37 // to the type checker. It should never happen. 38 InvalidSyntaxTree Code = -1 39 ) 40 41 const ( 42 // The zero Code value indicates an unset (invalid) error code. 43 _ Code = iota 44 45 // Test is reserved for errors that only apply while in self-test mode. 46 Test 47 48 // BlankPkgName occurs when a package name is the blank identifier "_". 49 // 50 // Per the spec: 51 // "The PackageName must not be the blank identifier." 52 // 53 // Example: 54 // package _ 55 BlankPkgName 56 57 // MismatchedPkgName occurs when a file's package name doesn't match the 58 // package name already established by other files. 59 MismatchedPkgName 60 61 // InvalidPkgUse occurs when a package identifier is used outside of a 62 // selector expression. 63 // 64 // Example: 65 // import "fmt" 66 // 67 // var _ = fmt 68 InvalidPkgUse 69 70 // BadImportPath occurs when an import path is not valid. 71 BadImportPath 72 73 // BrokenImport occurs when importing a package fails. 74 // 75 // Example: 76 // import "amissingpackage" 77 BrokenImport 78 79 // ImportCRenamed occurs when the special import "C" is renamed. "C" is a 80 // pseudo-package, and must not be renamed. 81 // 82 // Example: 83 // import _ "C" 84 ImportCRenamed 85 86 // UnusedImport occurs when an import is unused. 87 // 88 // Example: 89 // import "fmt" 90 // 91 // func main() {} 92 UnusedImport 93 94 // InvalidInitCycle occurs when an invalid cycle is detected within the 95 // initialization graph. 96 // 97 // Example: 98 // var x int = f() 99 // 100 // func f() int { return x } 101 InvalidInitCycle 102 103 // DuplicateDecl occurs when an identifier is declared multiple times. 104 // 105 // Example: 106 // var x = 1 107 // var x = 2 108 DuplicateDecl 109 110 // InvalidDeclCycle occurs when a declaration cycle is not valid. 111 // 112 // Example: 113 // type S struct { 114 // S 115 // } 116 // 117 InvalidDeclCycle 118 119 // InvalidTypeCycle occurs when a cycle in type definitions results in a 120 // type that is not well-defined. 121 // 122 // Example: 123 // import "unsafe" 124 // 125 // type T [unsafe.Sizeof(T{})]int 126 InvalidTypeCycle 127 128 // InvalidConstInit occurs when a const declaration has a non-constant 129 // initializer. 130 // 131 // Example: 132 // var x int 133 // const _ = x 134 InvalidConstInit 135 136 // InvalidConstVal occurs when a const value cannot be converted to its 137 // target type. 138 // 139 // TODO(findleyr): this error code and example are not very clear. Consider 140 // removing it. 141 // 142 // Example: 143 // const _ = 1 << "hello" 144 InvalidConstVal 145 146 // InvalidConstType occurs when the underlying type in a const declaration 147 // is not a valid constant type. 148 // 149 // Example: 150 // const c *int = 4 151 InvalidConstType 152 153 // UntypedNilUse occurs when the predeclared (untyped) value nil is used to 154 // initialize a variable declared without an explicit type. 155 // 156 // Example: 157 // var x = nil 158 UntypedNilUse 159 160 // WrongAssignCount occurs when the number of values on the right-hand side 161 // of an assignment or initialization expression does not match the number 162 // of variables on the left-hand side. 163 // 164 // Example: 165 // var x = 1, 2 166 WrongAssignCount 167 168 // UnassignableOperand occurs when the left-hand side of an assignment is 169 // not assignable. 170 // 171 // Example: 172 // func f() { 173 // const c = 1 174 // c = 2 175 // } 176 UnassignableOperand 177 178 // NoNewVar occurs when a short variable declaration (':=') does not declare 179 // new variables. 180 // 181 // Example: 182 // func f() { 183 // x := 1 184 // x := 2 185 // } 186 NoNewVar 187 188 // MultiValAssignOp occurs when an assignment operation (+=, *=, etc) does 189 // not have single-valued left-hand or right-hand side. 190 // 191 // Per the spec: 192 // "In assignment operations, both the left- and right-hand expression lists 193 // must contain exactly one single-valued expression" 194 // 195 // Example: 196 // func f() int { 197 // x, y := 1, 2 198 // x, y += 1 199 // return x + y 200 // } 201 MultiValAssignOp 202 203 // InvalidIfaceAssign occurs when a value of type T is used as an 204 // interface, but T does not implement a method of the expected interface. 205 // 206 // Example: 207 // type I interface { 208 // f() 209 // } 210 // 211 // type T int 212 // 213 // var x I = T(1) 214 InvalidIfaceAssign 215 216 // InvalidChanAssign occurs when a chan assignment is invalid. 217 // 218 // Per the spec, a value x is assignable to a channel type T if: 219 // "x is a bidirectional channel value, T is a channel type, x's type V and 220 // T have identical element types, and at least one of V or T is not a 221 // defined type." 222 // 223 // Example: 224 // type T1 chan int 225 // type T2 chan int 226 // 227 // var x T1 228 // // Invalid assignment because both types are named 229 // var _ T2 = x 230 InvalidChanAssign 231 232 // IncompatibleAssign occurs when the type of the right-hand side expression 233 // in an assignment cannot be assigned to the type of the variable being 234 // assigned. 235 // 236 // Example: 237 // var x []int 238 // var _ int = x 239 IncompatibleAssign 240 241 // UnaddressableFieldAssign occurs when trying to assign to a struct field 242 // in a map value. 243 // 244 // Example: 245 // func f() { 246 // m := make(map[string]struct{i int}) 247 // m["foo"].i = 42 248 // } 249 UnaddressableFieldAssign 250 251 // NotAType occurs when the identifier used as the underlying type in a type 252 // declaration or the right-hand side of a type alias does not denote a type. 253 // 254 // Example: 255 // var S = 2 256 // 257 // type T S 258 NotAType 259 260 // InvalidArrayLen occurs when an array length is not a constant value. 261 // 262 // Example: 263 // var n = 3 264 // var _ = [n]int{} 265 InvalidArrayLen 266 267 // BlankIfaceMethod occurs when a method name is '_'. 268 // 269 // Per the spec: 270 // "The name of each explicitly specified method must be unique and not 271 // blank." 272 // 273 // Example: 274 // type T interface { 275 // _(int) 276 // } 277 BlankIfaceMethod 278 279 // IncomparableMapKey occurs when a map key type does not support the == and 280 // != operators. 281 // 282 // Per the spec: 283 // "The comparison operators == and != must be fully defined for operands of 284 // the key type; thus the key type must not be a function, map, or slice." 285 // 286 // Example: 287 // var x map[T]int 288 // 289 // type T []int 290 IncomparableMapKey 291 292 // InvalidIfaceEmbed occurs when a non-interface type is embedded in an 293 // interface (for go 1.17 or earlier). 294 _ // not used anymore 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 _ // not used anymore (error reported by parser) 723 724 _ // InvalidDotDotDotOperand was removed. 725 726 // InvalidDotDotDot occurs when a "..." is used in a non-variadic built-in 727 // function. 728 // 729 // Example: 730 // var s = []int{1, 2, 3} 731 // var l = len(s...) 732 InvalidDotDotDot 733 734 // UncalledBuiltin occurs when a built-in function is used as a 735 // function-valued expression, instead of being called. 736 // 737 // Per the spec: 738 // "The built-in functions do not have standard Go types, so they can only 739 // appear in call expressions; they cannot be used as function values." 740 // 741 // Example: 742 // var _ = copy 743 UncalledBuiltin 744 745 // InvalidAppend occurs when append is called with a first argument that is 746 // not a slice. 747 // 748 // Example: 749 // var _ = append(1, 2) 750 InvalidAppend 751 752 // InvalidCap occurs when an argument to the cap built-in function is not of 753 // supported type. 754 // 755 // See https://golang.org/ref/spec#Length_and_capacity for information on 756 // which underlying types are supported as arguments to cap and len. 757 // 758 // Example: 759 // var s = 2 760 // var x = cap(s) 761 InvalidCap 762 763 // InvalidClose occurs when close(...) is called with an argument that is 764 // not of channel type, or that is a receive-only channel. 765 // 766 // Example: 767 // func f() { 768 // var x int 769 // close(x) 770 // } 771 InvalidClose 772 773 // InvalidCopy occurs when the arguments are not of slice type or do not 774 // have compatible type. 775 // 776 // See https://golang.org/ref/spec#Appending_and_copying_slices for more 777 // information on the type requirements for the copy built-in. 778 // 779 // Example: 780 // func f() { 781 // var x []int 782 // y := []int64{1,2,3} 783 // copy(x, y) 784 // } 785 InvalidCopy 786 787 // InvalidComplex occurs when the complex built-in function is called with 788 // arguments with incompatible types. 789 // 790 // Example: 791 // var _ = complex(float32(1), float64(2)) 792 InvalidComplex 793 794 // InvalidDelete occurs when the delete built-in function is called with a 795 // first argument that is not a map. 796 // 797 // Example: 798 // func f() { 799 // m := "hello" 800 // delete(m, "e") 801 // } 802 InvalidDelete 803 804 // InvalidImag occurs when the imag built-in function is called with an 805 // argument that does not have complex type. 806 // 807 // Example: 808 // var _ = imag(int(1)) 809 InvalidImag 810 811 // InvalidLen occurs when an argument to the len built-in function is not of 812 // supported type. 813 // 814 // See https://golang.org/ref/spec#Length_and_capacity for information on 815 // which underlying types are supported as arguments to cap and len. 816 // 817 // Example: 818 // var s = 2 819 // var x = len(s) 820 InvalidLen 821 822 // SwappedMakeArgs occurs when make is called with three arguments, and its 823 // length argument is larger than its capacity argument. 824 // 825 // Example: 826 // var x = make([]int, 3, 2) 827 SwappedMakeArgs 828 829 // InvalidMake occurs when make is called with an unsupported type argument. 830 // 831 // See https://golang.org/ref/spec#Making_slices_maps_and_channels for 832 // information on the types that may be created using make. 833 // 834 // Example: 835 // var x = make(int) 836 InvalidMake 837 838 // InvalidReal occurs when the real built-in function is called with an 839 // argument that does not have complex type. 840 // 841 // Example: 842 // var _ = real(int(1)) 843 InvalidReal 844 845 // InvalidAssert occurs when a type assertion is applied to a 846 // value that is not of interface type. 847 // 848 // Example: 849 // var x = 1 850 // var _ = x.(float64) 851 InvalidAssert 852 853 // ImpossibleAssert occurs for a type assertion x.(T) when the value x of 854 // interface cannot have dynamic type T, due to a missing or mismatching 855 // method on T. 856 // 857 // Example: 858 // type T int 859 // 860 // func (t *T) m() int { return int(*t) } 861 // 862 // type I interface { m() int } 863 // 864 // var x I 865 // var _ = x.(T) 866 ImpossibleAssert 867 868 // InvalidConversion occurs when the argument type cannot be converted to the 869 // target. 870 // 871 // See https://golang.org/ref/spec#Conversions for the rules of 872 // convertibility. 873 // 874 // Example: 875 // var x float64 876 // var _ = string(x) 877 InvalidConversion 878 879 // InvalidUntypedConversion occurs when there is no valid implicit 880 // conversion from an untyped value satisfying the type constraints of the 881 // context in which it is used. 882 // 883 // Example: 884 // var _ = 1 + []int{} 885 InvalidUntypedConversion 886 887 // BadOffsetofSyntax occurs when unsafe.Offsetof is called with an argument 888 // that is not a selector expression. 889 // 890 // Example: 891 // import "unsafe" 892 // 893 // var x int 894 // var _ = unsafe.Offsetof(x) 895 BadOffsetofSyntax 896 897 // InvalidOffsetof occurs when unsafe.Offsetof is called with a method 898 // selector, rather than a field selector, or when the field is embedded via 899 // a pointer. 900 // 901 // Per the spec: 902 // 903 // "If f is an embedded field, it must be reachable without pointer 904 // indirections through fields of the struct. " 905 // 906 // Example: 907 // import "unsafe" 908 // 909 // type T struct { f int } 910 // type S struct { *T } 911 // var s S 912 // var _ = unsafe.Offsetof(s.f) 913 // 914 // Example: 915 // import "unsafe" 916 // 917 // type S struct{} 918 // 919 // func (S) m() {} 920 // 921 // var s S 922 // var _ = unsafe.Offsetof(s.m) 923 InvalidOffsetof 924 925 // UnusedExpr occurs when a side-effect free expression is used as a 926 // statement. Such a statement has no effect. 927 // 928 // Example: 929 // func f(i int) { 930 // i*i 931 // } 932 UnusedExpr 933 934 // UnusedVar occurs when a variable is declared but unused. 935 // 936 // Example: 937 // func f() { 938 // x := 1 939 // } 940 UnusedVar 941 942 // MissingReturn occurs when a function with results is missing a return 943 // statement. 944 // 945 // Example: 946 // func f() int {} 947 MissingReturn 948 949 // WrongResultCount occurs when a return statement returns an incorrect 950 // number of values. 951 // 952 // Example: 953 // func ReturnOne() int { 954 // return 1, 2 955 // } 956 WrongResultCount 957 958 // OutOfScopeResult occurs when the name of a value implicitly returned by 959 // an empty return statement is shadowed in a nested scope. 960 // 961 // Example: 962 // func factor(n int) (i int) { 963 // for i := 2; i < n; i++ { 964 // if n%i == 0 { 965 // return 966 // } 967 // } 968 // return 0 969 // } 970 OutOfScopeResult 971 972 // InvalidCond occurs when an if condition is not a boolean expression. 973 // 974 // Example: 975 // func checkReturn(i int) { 976 // if i { 977 // panic("non-zero return") 978 // } 979 // } 980 InvalidCond 981 982 // InvalidPostDecl occurs when there is a declaration in a for-loop post 983 // statement. 984 // 985 // Example: 986 // func f() { 987 // for i := 0; i < 10; j := 0 {} 988 // } 989 InvalidPostDecl 990 991 _ // InvalidChanRange was removed. 992 993 // InvalidIterVar occurs when two iteration variables are used while ranging 994 // over a channel. 995 // 996 // Example: 997 // func f(c chan int) { 998 // for k, v := range c { 999 // println(k, v) 1000 // } 1001 // } 1002 InvalidIterVar 1003 1004 // InvalidRangeExpr occurs when the type of a range expression is not 1005 // a valid type for use with a range loop. 1006 // 1007 // Example: 1008 // func f(f float64) { 1009 // for j := range f { 1010 // println(j) 1011 // } 1012 // } 1013 InvalidRangeExpr 1014 1015 // MisplacedBreak occurs when a break statement is not within a for, switch, 1016 // or select statement of the innermost function definition. 1017 // 1018 // Example: 1019 // func f() { 1020 // break 1021 // } 1022 MisplacedBreak 1023 1024 // MisplacedContinue occurs when a continue statement is not within a for 1025 // loop of the innermost function definition. 1026 // 1027 // Example: 1028 // func sumeven(n int) int { 1029 // proceed := func() { 1030 // continue 1031 // } 1032 // sum := 0 1033 // for i := 1; i <= n; i++ { 1034 // if i % 2 != 0 { 1035 // proceed() 1036 // } 1037 // sum += i 1038 // } 1039 // return sum 1040 // } 1041 MisplacedContinue 1042 1043 // MisplacedFallthrough occurs when a fallthrough statement is not within an 1044 // expression switch. 1045 // 1046 // Example: 1047 // func typename(i interface{}) string { 1048 // switch i.(type) { 1049 // case int64: 1050 // fallthrough 1051 // case int: 1052 // return "int" 1053 // } 1054 // return "unsupported" 1055 // } 1056 MisplacedFallthrough 1057 1058 // DuplicateCase occurs when a type or expression switch has duplicate 1059 // cases. 1060 // 1061 // Example: 1062 // func printInt(i int) { 1063 // switch i { 1064 // case 1: 1065 // println("one") 1066 // case 1: 1067 // println("One") 1068 // } 1069 // } 1070 DuplicateCase 1071 1072 // DuplicateDefault occurs when a type or expression switch has multiple 1073 // default clauses. 1074 // 1075 // Example: 1076 // func printInt(i int) { 1077 // switch i { 1078 // case 1: 1079 // println("one") 1080 // default: 1081 // println("One") 1082 // default: 1083 // println("1") 1084 // } 1085 // } 1086 DuplicateDefault 1087 1088 // BadTypeKeyword occurs when a .(type) expression is used anywhere other 1089 // than a type switch. 1090 // 1091 // Example: 1092 // type I interface { 1093 // m() 1094 // } 1095 // var t I 1096 // var _ = t.(type) 1097 BadTypeKeyword 1098 1099 // InvalidTypeSwitch occurs when .(type) is used on an expression that is 1100 // not of interface type. 1101 // 1102 // Example: 1103 // func f(i int) { 1104 // switch x := i.(type) {} 1105 // } 1106 InvalidTypeSwitch 1107 1108 // InvalidExprSwitch occurs when a switch expression is not comparable. 1109 // 1110 // Example: 1111 // func _() { 1112 // var a struct{ _ func() } 1113 // switch a /* ERROR cannot switch on a */ { 1114 // } 1115 // } 1116 InvalidExprSwitch 1117 1118 // InvalidSelectCase occurs when a select case is not a channel send or 1119 // receive. 1120 // 1121 // Example: 1122 // func checkChan(c <-chan int) bool { 1123 // select { 1124 // case c: 1125 // return true 1126 // default: 1127 // return false 1128 // } 1129 // } 1130 InvalidSelectCase 1131 1132 // UndeclaredLabel occurs when an undeclared label is jumped to. 1133 // 1134 // Example: 1135 // func f() { 1136 // goto L 1137 // } 1138 UndeclaredLabel 1139 1140 // DuplicateLabel occurs when a label is declared more than once. 1141 // 1142 // Example: 1143 // func f() int { 1144 // L: 1145 // L: 1146 // return 1 1147 // } 1148 DuplicateLabel 1149 1150 // MisplacedLabel occurs when a break or continue label is not on a for, 1151 // switch, or select statement. 1152 // 1153 // Example: 1154 // func f() { 1155 // L: 1156 // a := []int{1,2,3} 1157 // for _, e := range a { 1158 // if e > 10 { 1159 // break L 1160 // } 1161 // println(a) 1162 // } 1163 // } 1164 MisplacedLabel 1165 1166 // UnusedLabel occurs when a label is declared and not used. 1167 // 1168 // Example: 1169 // func f() { 1170 // L: 1171 // } 1172 UnusedLabel 1173 1174 // JumpOverDecl occurs when a label jumps over a variable declaration. 1175 // 1176 // Example: 1177 // func f() int { 1178 // goto L 1179 // x := 2 1180 // L: 1181 // x++ 1182 // return x 1183 // } 1184 JumpOverDecl 1185 1186 // JumpIntoBlock occurs when a forward jump goes to a label inside a nested 1187 // block. 1188 // 1189 // Example: 1190 // func f(x int) { 1191 // goto L 1192 // if x > 0 { 1193 // L: 1194 // print("inside block") 1195 // } 1196 // } 1197 JumpIntoBlock 1198 1199 // InvalidMethodExpr occurs when a pointer method is called but the argument 1200 // is not addressable. 1201 // 1202 // Example: 1203 // type T struct {} 1204 // 1205 // func (*T) m() int { return 1 } 1206 // 1207 // var _ = T.m(T{}) 1208 InvalidMethodExpr 1209 1210 // WrongArgCount occurs when too few or too many arguments are passed by a 1211 // function call. 1212 // 1213 // Example: 1214 // func f(i int) {} 1215 // var x = f() 1216 WrongArgCount 1217 1218 // InvalidCall occurs when an expression is called that is not of function 1219 // type. 1220 // 1221 // Example: 1222 // var x = "x" 1223 // var y = x() 1224 InvalidCall 1225 1226 // UnusedResults occurs when a restricted expression-only built-in function 1227 // is suspended via go or defer. Such a suspension discards the results of 1228 // these side-effect free built-in functions, and therefore is ineffectual. 1229 // 1230 // Example: 1231 // func f(a []int) int { 1232 // defer len(a) 1233 // return i 1234 // } 1235 UnusedResults 1236 1237 // InvalidDefer occurs when a deferred expression is not a function call, 1238 // for example if the expression is a type conversion. 1239 // 1240 // Example: 1241 // func f(i int) int { 1242 // defer int32(i) 1243 // return i 1244 // } 1245 InvalidDefer 1246 1247 // InvalidGo occurs when a go expression is not a function call, for example 1248 // if the expression is a type conversion. 1249 // 1250 // Example: 1251 // func f(i int) int { 1252 // go int32(i) 1253 // return i 1254 // } 1255 InvalidGo 1256 1257 // All codes below were added in Go 1.17. 1258 1259 // BadDecl occurs when a declaration has invalid syntax. 1260 BadDecl 1261 1262 // RepeatedDecl occurs when an identifier occurs more than once on the left 1263 // hand side of a short variable declaration. 1264 // 1265 // Example: 1266 // func _() { 1267 // x, y, y := 1, 2, 3 1268 // } 1269 RepeatedDecl 1270 1271 // InvalidUnsafeAdd occurs when unsafe.Add is called with a 1272 // length argument that is not of integer type. 1273 // It also occurs if it is used in a package compiled for a 1274 // language version before go1.17. 1275 // 1276 // Example: 1277 // import "unsafe" 1278 // 1279 // var p unsafe.Pointer 1280 // var _ = unsafe.Add(p, float64(1)) 1281 InvalidUnsafeAdd 1282 1283 // InvalidUnsafeSlice occurs when unsafe.Slice is called with a 1284 // pointer argument that is not of pointer type or a length argument 1285 // that is not of integer type, negative, or out of bounds. 1286 // It also occurs if it is used in a package compiled for a language 1287 // version before go1.17. 1288 // 1289 // Example: 1290 // import "unsafe" 1291 // 1292 // var x int 1293 // var _ = unsafe.Slice(x, 1) 1294 // 1295 // Example: 1296 // import "unsafe" 1297 // 1298 // var x int 1299 // var _ = unsafe.Slice(&x, float64(1)) 1300 // 1301 // Example: 1302 // import "unsafe" 1303 // 1304 // var x int 1305 // var _ = unsafe.Slice(&x, -1) 1306 // 1307 // Example: 1308 // import "unsafe" 1309 // 1310 // var x int 1311 // var _ = unsafe.Slice(&x, uint64(1) << 63) 1312 InvalidUnsafeSlice 1313 1314 // All codes below were added in Go 1.18. 1315 1316 // UnsupportedFeature occurs when a language feature is used that is not 1317 // supported at this Go version. 1318 UnsupportedFeature 1319 1320 // NotAGenericType occurs when a non-generic type is used where a generic 1321 // type is expected: in type or function instantiation. 1322 // 1323 // Example: 1324 // type T int 1325 // 1326 // var _ T[int] 1327 NotAGenericType 1328 1329 // WrongTypeArgCount occurs when a type or function is instantiated with an 1330 // incorrect number of type arguments, including when a generic type or 1331 // function is used without instantiation. 1332 // 1333 // Errors involving failed type inference are assigned other error codes. 1334 // 1335 // Example: 1336 // type T[p any] int 1337 // 1338 // var _ T[int, string] 1339 // 1340 // Example: 1341 // func f[T any]() {} 1342 // 1343 // var x = f 1344 WrongTypeArgCount 1345 1346 // CannotInferTypeArgs occurs when type or function type argument inference 1347 // fails to infer all type arguments. 1348 // 1349 // Example: 1350 // func f[T any]() {} 1351 // 1352 // func _() { 1353 // f() 1354 // } 1355 CannotInferTypeArgs 1356 1357 // InvalidTypeArg occurs when a type argument does not satisfy its 1358 // corresponding type parameter constraints. 1359 // 1360 // Example: 1361 // type T[P ~int] struct{} 1362 // 1363 // var _ T[string] 1364 InvalidTypeArg // arguments? InferenceFailed 1365 1366 // InvalidInstanceCycle occurs when an invalid cycle is detected 1367 // within the instantiation graph. 1368 // 1369 // Example: 1370 // func f[T any]() { f[*T]() } 1371 InvalidInstanceCycle 1372 1373 // InvalidUnion occurs when an embedded union or approximation element is 1374 // not valid. 1375 // 1376 // Example: 1377 // type _ interface { 1378 // ~int | interface{ m() } 1379 // } 1380 InvalidUnion 1381 1382 // MisplacedConstraintIface occurs when a constraint-type interface is used 1383 // outside of constraint position. 1384 // 1385 // Example: 1386 // type I interface { ~int } 1387 // 1388 // var _ I 1389 MisplacedConstraintIface 1390 1391 // InvalidMethodTypeParams occurs when methods have type parameters. 1392 // 1393 // It cannot be encountered with an AST parsed using go/parser. 1394 InvalidMethodTypeParams 1395 1396 // MisplacedTypeParam occurs when a type parameter is used in a place where 1397 // it is not permitted. 1398 // 1399 // Example: 1400 // type T[P any] P 1401 // 1402 // Example: 1403 // type T[P any] struct{ *P } 1404 MisplacedTypeParam 1405 1406 // InvalidUnsafeSliceData occurs when unsafe.SliceData is called with 1407 // an argument that is not of slice type. It also occurs if it is used 1408 // in a package compiled for a language version before go1.20. 1409 // 1410 // Example: 1411 // import "unsafe" 1412 // 1413 // var x int 1414 // var _ = unsafe.SliceData(x) 1415 InvalidUnsafeSliceData 1416 1417 // InvalidUnsafeString occurs when unsafe.String is called with 1418 // a length argument that is not of integer type, negative, or 1419 // out of bounds. It also occurs if it is used in a package 1420 // compiled for a language version before go1.20. 1421 // 1422 // Example: 1423 // import "unsafe" 1424 // 1425 // var b [10]byte 1426 // var _ = unsafe.String(&b[0], -1) 1427 InvalidUnsafeString 1428 1429 // InvalidUnsafeStringData occurs if it is used in a package 1430 // compiled for a language version before go1.20. 1431 _ // not used anymore 1432 1433 // InvalidClear occurs when clear is called with an argument 1434 // that is not of map or slice type. 1435 // 1436 // Example: 1437 // func _(x int) { 1438 // clear(x) 1439 // } 1440 InvalidClear 1441 1442 // TypeTooLarge occurs if unsafe.Sizeof or unsafe.Offsetof is 1443 // called with an expression whose type is too large. 1444 // 1445 // Example: 1446 // import "unsafe" 1447 // 1448 // type E [1 << 31 - 1]int 1449 // var a [1 << 31]E 1450 // var _ = unsafe.Sizeof(a) 1451 // 1452 // Example: 1453 // import "unsafe" 1454 // 1455 // type E [1 << 31 - 1]int 1456 // var s struct { 1457 // _ [1 << 31]E 1458 // x int 1459 // } 1460 // var _ = unsafe.Offsetof(s.x) 1461 TypeTooLarge 1462 1463 // InvalidMinMaxOperand occurs if min or max is called 1464 // with an operand that cannot be ordered because it 1465 // does not support the < operator. 1466 // 1467 // Example: 1468 // const _ = min(true) 1469 // 1470 // Example: 1471 // var s, t []byte 1472 // var _ = max(s, t) 1473 InvalidMinMaxOperand 1474 1475 // TooNew indicates that, through build tags or a go.mod file, 1476 // a source file requires a version of Go that is newer than 1477 // the logic of the type checker. As a consequence, the type 1478 // checker may produce spurious errors or fail to report real 1479 // errors. The solution is to rebuild the application with a 1480 // newer Go release. 1481 TooNew 1482 ) 1483