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 // func f[T ~int8 | ~int16 | ~int32 | ~int64](x T) T { 885 // return x + 1024 886 // } 887 InvalidUntypedConversion 888 889 // BadOffsetofSyntax occurs when unsafe.Offsetof is called with an argument 890 // that is not a selector expression. 891 // 892 // Example: 893 // import "unsafe" 894 // 895 // var x int 896 // var _ = unsafe.Offsetof(x) 897 BadOffsetofSyntax 898 899 // InvalidOffsetof occurs when unsafe.Offsetof is called with a method 900 // selector, rather than a field selector, or when the field is embedded via 901 // a pointer. 902 // 903 // Per the spec: 904 // 905 // "If f is an embedded field, it must be reachable without pointer 906 // indirections through fields of the struct. " 907 // 908 // Example: 909 // import "unsafe" 910 // 911 // type T struct { f int } 912 // type S struct { *T } 913 // var s S 914 // var _ = unsafe.Offsetof(s.f) 915 // 916 // Example: 917 // import "unsafe" 918 // 919 // type S struct{} 920 // 921 // func (S) m() {} 922 // 923 // var s S 924 // var _ = unsafe.Offsetof(s.m) 925 InvalidOffsetof 926 927 // UnusedExpr occurs when a side-effect free expression is used as a 928 // statement. Such a statement has no effect. 929 // 930 // Example: 931 // func f(i int) { 932 // i*i 933 // } 934 UnusedExpr 935 936 // UnusedVar occurs when a variable is declared but unused. 937 // 938 // Example: 939 // func f() { 940 // x := 1 941 // } 942 UnusedVar 943 944 // MissingReturn occurs when a function with results is missing a return 945 // statement. 946 // 947 // Example: 948 // func f() int {} 949 MissingReturn 950 951 // WrongResultCount occurs when a return statement returns an incorrect 952 // number of values. 953 // 954 // Example: 955 // func ReturnOne() int { 956 // return 1, 2 957 // } 958 WrongResultCount 959 960 // OutOfScopeResult occurs when the name of a value implicitly returned by 961 // an empty return statement is shadowed in a nested scope. 962 // 963 // Example: 964 // func factor(n int) (i int) { 965 // for i := 2; i < n; i++ { 966 // if n%i == 0 { 967 // return 968 // } 969 // } 970 // return 0 971 // } 972 OutOfScopeResult 973 974 // InvalidCond occurs when an if condition is not a boolean expression. 975 // 976 // Example: 977 // func checkReturn(i int) { 978 // if i { 979 // panic("non-zero return") 980 // } 981 // } 982 InvalidCond 983 984 // InvalidPostDecl occurs when there is a declaration in a for-loop post 985 // statement. 986 // 987 // Example: 988 // func f() { 989 // for i := 0; i < 10; j := 0 {} 990 // } 991 InvalidPostDecl 992 993 _ // InvalidChanRange was removed. 994 995 // InvalidIterVar occurs when two iteration variables are used while ranging 996 // over a channel. 997 // 998 // Example: 999 // func f(c chan int) { 1000 // for k, v := range c { 1001 // println(k, v) 1002 // } 1003 // } 1004 InvalidIterVar 1005 1006 // InvalidRangeExpr occurs when the type of a range expression is not 1007 // a valid type for use with a range loop. 1008 // 1009 // Example: 1010 // func f(f float64) { 1011 // for j := range f { 1012 // println(j) 1013 // } 1014 // } 1015 InvalidRangeExpr 1016 1017 // MisplacedBreak occurs when a break statement is not within a for, switch, 1018 // or select statement of the innermost function definition. 1019 // 1020 // Example: 1021 // func f() { 1022 // break 1023 // } 1024 MisplacedBreak 1025 1026 // MisplacedContinue occurs when a continue statement is not within a for 1027 // loop of the innermost function definition. 1028 // 1029 // Example: 1030 // func sumeven(n int) int { 1031 // proceed := func() { 1032 // continue 1033 // } 1034 // sum := 0 1035 // for i := 1; i <= n; i++ { 1036 // if i % 2 != 0 { 1037 // proceed() 1038 // } 1039 // sum += i 1040 // } 1041 // return sum 1042 // } 1043 MisplacedContinue 1044 1045 // MisplacedFallthrough occurs when a fallthrough statement is not within an 1046 // expression switch. 1047 // 1048 // Example: 1049 // func typename(i interface{}) string { 1050 // switch i.(type) { 1051 // case int64: 1052 // fallthrough 1053 // case int: 1054 // return "int" 1055 // } 1056 // return "unsupported" 1057 // } 1058 MisplacedFallthrough 1059 1060 // DuplicateCase occurs when a type or expression switch has duplicate 1061 // cases. 1062 // 1063 // Example: 1064 // func printInt(i int) { 1065 // switch i { 1066 // case 1: 1067 // println("one") 1068 // case 1: 1069 // println("One") 1070 // } 1071 // } 1072 DuplicateCase 1073 1074 // DuplicateDefault occurs when a type or expression switch has multiple 1075 // default clauses. 1076 // 1077 // Example: 1078 // func printInt(i int) { 1079 // switch i { 1080 // case 1: 1081 // println("one") 1082 // default: 1083 // println("One") 1084 // default: 1085 // println("1") 1086 // } 1087 // } 1088 DuplicateDefault 1089 1090 // BadTypeKeyword occurs when a .(type) expression is used anywhere other 1091 // than a type switch. 1092 // 1093 // Example: 1094 // type I interface { 1095 // m() 1096 // } 1097 // var t I 1098 // var _ = t.(type) 1099 BadTypeKeyword 1100 1101 // InvalidTypeSwitch occurs when .(type) is used on an expression that is 1102 // not of interface type. 1103 // 1104 // Example: 1105 // func f(i int) { 1106 // switch x := i.(type) {} 1107 // } 1108 InvalidTypeSwitch 1109 1110 // InvalidExprSwitch occurs when a switch expression is not comparable. 1111 // 1112 // Example: 1113 // func _() { 1114 // var a struct{ _ func() } 1115 // switch a /* ERROR cannot switch on a */ { 1116 // } 1117 // } 1118 InvalidExprSwitch 1119 1120 // InvalidSelectCase occurs when a select case is not a channel send or 1121 // receive. 1122 // 1123 // Example: 1124 // func checkChan(c <-chan int) bool { 1125 // select { 1126 // case c: 1127 // return true 1128 // default: 1129 // return false 1130 // } 1131 // } 1132 InvalidSelectCase 1133 1134 // UndeclaredLabel occurs when an undeclared label is jumped to. 1135 // 1136 // Example: 1137 // func f() { 1138 // goto L 1139 // } 1140 UndeclaredLabel 1141 1142 // DuplicateLabel occurs when a label is declared more than once. 1143 // 1144 // Example: 1145 // func f() int { 1146 // L: 1147 // L: 1148 // return 1 1149 // } 1150 DuplicateLabel 1151 1152 // MisplacedLabel occurs when a break or continue label is not on a for, 1153 // switch, or select statement. 1154 // 1155 // Example: 1156 // func f() { 1157 // L: 1158 // a := []int{1,2,3} 1159 // for _, e := range a { 1160 // if e > 10 { 1161 // break L 1162 // } 1163 // println(a) 1164 // } 1165 // } 1166 MisplacedLabel 1167 1168 // UnusedLabel occurs when a label is declared and not used. 1169 // 1170 // Example: 1171 // func f() { 1172 // L: 1173 // } 1174 UnusedLabel 1175 1176 // JumpOverDecl occurs when a label jumps over a variable declaration. 1177 // 1178 // Example: 1179 // func f() int { 1180 // goto L 1181 // x := 2 1182 // L: 1183 // x++ 1184 // return x 1185 // } 1186 JumpOverDecl 1187 1188 // JumpIntoBlock occurs when a forward jump goes to a label inside a nested 1189 // block. 1190 // 1191 // Example: 1192 // func f(x int) { 1193 // goto L 1194 // if x > 0 { 1195 // L: 1196 // print("inside block") 1197 // } 1198 // } 1199 JumpIntoBlock 1200 1201 // InvalidMethodExpr occurs when a pointer method is called but the argument 1202 // is not addressable. 1203 // 1204 // Example: 1205 // type T struct {} 1206 // 1207 // func (*T) m() int { return 1 } 1208 // 1209 // var _ = T.m(T{}) 1210 InvalidMethodExpr 1211 1212 // WrongArgCount occurs when too few or too many arguments are passed by a 1213 // function call. 1214 // 1215 // Example: 1216 // func f(i int) {} 1217 // var x = f() 1218 WrongArgCount 1219 1220 // InvalidCall occurs when an expression is called that is not of function 1221 // type. 1222 // 1223 // Example: 1224 // var x = "x" 1225 // var y = x() 1226 InvalidCall 1227 1228 // UnusedResults occurs when a restricted expression-only built-in function 1229 // is suspended via go or defer. Such a suspension discards the results of 1230 // these side-effect free built-in functions, and therefore is ineffectual. 1231 // 1232 // Example: 1233 // func f(a []int) int { 1234 // defer len(a) 1235 // return i 1236 // } 1237 UnusedResults 1238 1239 // InvalidDefer occurs when a deferred expression is not a function call, 1240 // for example if the expression is a type conversion. 1241 // 1242 // Example: 1243 // func f(i int) int { 1244 // defer int32(i) 1245 // return i 1246 // } 1247 InvalidDefer 1248 1249 // InvalidGo occurs when a go expression is not a function call, for example 1250 // if the expression is a type conversion. 1251 // 1252 // Example: 1253 // func f(i int) int { 1254 // go int32(i) 1255 // return i 1256 // } 1257 InvalidGo 1258 1259 // All codes below were added in Go 1.17. 1260 1261 // BadDecl occurs when a declaration has invalid syntax. 1262 BadDecl 1263 1264 // RepeatedDecl occurs when an identifier occurs more than once on the left 1265 // hand side of a short variable declaration. 1266 // 1267 // Example: 1268 // func _() { 1269 // x, y, y := 1, 2, 3 1270 // } 1271 RepeatedDecl 1272 1273 // InvalidUnsafeAdd occurs when unsafe.Add is called with a 1274 // length argument that is not of integer type. 1275 // It also occurs if it is used in a package compiled for a 1276 // language version before go1.17. 1277 // 1278 // Example: 1279 // import "unsafe" 1280 // 1281 // var p unsafe.Pointer 1282 // var _ = unsafe.Add(p, float64(1)) 1283 InvalidUnsafeAdd 1284 1285 // InvalidUnsafeSlice occurs when unsafe.Slice is called with a 1286 // pointer argument that is not of pointer type or a length argument 1287 // that is not of integer type, negative, or out of bounds. 1288 // It also occurs if it is used in a package compiled for a language 1289 // version before go1.17. 1290 // 1291 // Example: 1292 // import "unsafe" 1293 // 1294 // var x int 1295 // var _ = unsafe.Slice(x, 1) 1296 // 1297 // Example: 1298 // import "unsafe" 1299 // 1300 // var x int 1301 // var _ = unsafe.Slice(&x, float64(1)) 1302 // 1303 // Example: 1304 // import "unsafe" 1305 // 1306 // var x int 1307 // var _ = unsafe.Slice(&x, -1) 1308 // 1309 // Example: 1310 // import "unsafe" 1311 // 1312 // var x int 1313 // var _ = unsafe.Slice(&x, uint64(1) << 63) 1314 InvalidUnsafeSlice 1315 1316 // All codes below were added in Go 1.18. 1317 1318 // UnsupportedFeature occurs when a language feature is used that is not 1319 // supported at this Go version. 1320 UnsupportedFeature 1321 1322 // NotAGenericType occurs when a non-generic type is used where a generic 1323 // type is expected: in type or function instantiation. 1324 // 1325 // Example: 1326 // type T int 1327 // 1328 // var _ T[int] 1329 NotAGenericType 1330 1331 // WrongTypeArgCount occurs when a type or function is instantiated with an 1332 // incorrect number of type arguments, including when a generic type or 1333 // function is used without instantiation. 1334 // 1335 // Errors involving failed type inference are assigned other error codes. 1336 // 1337 // Example: 1338 // type T[p any] int 1339 // 1340 // var _ T[int, string] 1341 // 1342 // Example: 1343 // func f[T any]() {} 1344 // 1345 // var x = f 1346 WrongTypeArgCount 1347 1348 // CannotInferTypeArgs occurs when type or function type argument inference 1349 // fails to infer all type arguments. 1350 // 1351 // Example: 1352 // func f[T any]() {} 1353 // 1354 // func _() { 1355 // f() 1356 // } 1357 CannotInferTypeArgs 1358 1359 // InvalidTypeArg occurs when a type argument does not satisfy its 1360 // corresponding type parameter constraints. 1361 // 1362 // Example: 1363 // type T[P ~int] struct{} 1364 // 1365 // var _ T[string] 1366 InvalidTypeArg // arguments? InferenceFailed 1367 1368 // InvalidInstanceCycle occurs when an invalid cycle is detected 1369 // within the instantiation graph. 1370 // 1371 // Example: 1372 // func f[T any]() { f[*T]() } 1373 InvalidInstanceCycle 1374 1375 // InvalidUnion occurs when an embedded union or approximation element is 1376 // not valid. 1377 // 1378 // Example: 1379 // type _ interface { 1380 // ~int | interface{ m() } 1381 // } 1382 InvalidUnion 1383 1384 // MisplacedConstraintIface occurs when a constraint-type interface is used 1385 // outside of constraint position. 1386 // 1387 // Example: 1388 // type I interface { ~int } 1389 // 1390 // var _ I 1391 MisplacedConstraintIface 1392 1393 // InvalidMethodTypeParams occurs when methods have type parameters. 1394 // 1395 // It cannot be encountered with an AST parsed using go/parser. 1396 InvalidMethodTypeParams 1397 1398 // MisplacedTypeParam occurs when a type parameter is used in a place where 1399 // it is not permitted. 1400 // 1401 // Example: 1402 // type T[P any] P 1403 // 1404 // Example: 1405 // type T[P any] struct{ *P } 1406 MisplacedTypeParam 1407 1408 // InvalidUnsafeSliceData occurs when unsafe.SliceData is called with 1409 // an argument that is not of slice type. It also occurs if it is used 1410 // in a package compiled for a language version before go1.20. 1411 // 1412 // Example: 1413 // import "unsafe" 1414 // 1415 // var x int 1416 // var _ = unsafe.SliceData(x) 1417 InvalidUnsafeSliceData 1418 1419 // InvalidUnsafeString occurs when unsafe.String is called with 1420 // a length argument that is not of integer type, negative, or 1421 // out of bounds. It also occurs if it is used in a package 1422 // compiled for a language version before go1.20. 1423 // 1424 // Example: 1425 // import "unsafe" 1426 // 1427 // var b [10]byte 1428 // var _ = unsafe.String(&b[0], -1) 1429 InvalidUnsafeString 1430 1431 // InvalidUnsafeStringData occurs if it is used in a package 1432 // compiled for a language version before go1.20. 1433 _ // not used anymore 1434 1435 // InvalidClear occurs when clear is called with an argument 1436 // that is not of map or slice type. 1437 // 1438 // Example: 1439 // func _(x int) { 1440 // clear(x) 1441 // } 1442 InvalidClear 1443 1444 // TypeTooLarge occurs if unsafe.Sizeof or unsafe.Offsetof is 1445 // called with an expression whose type is too large. 1446 // 1447 // Example: 1448 // import "unsafe" 1449 // 1450 // type E [1 << 31 - 1]int 1451 // var a [1 << 31]E 1452 // var _ = unsafe.Sizeof(a) 1453 // 1454 // Example: 1455 // import "unsafe" 1456 // 1457 // type E [1 << 31 - 1]int 1458 // var s struct { 1459 // _ [1 << 31]E 1460 // x int 1461 // } 1462 // var _ = unsafe.Offsetof(s.x) 1463 TypeTooLarge 1464 1465 // InvalidMinMaxOperand occurs if min or max is called 1466 // with an operand that cannot be ordered because it 1467 // does not support the < operator. 1468 // 1469 // Example: 1470 // const _ = min(true) 1471 // 1472 // Example: 1473 // var s, t []byte 1474 // var _ = max(s, t) 1475 InvalidMinMaxOperand 1476 1477 // TooNew indicates that, through build tags or a go.mod file, 1478 // a source file requires a version of Go that is newer than 1479 // the logic of the type checker. As a consequence, the type 1480 // checker may produce spurious errors or fail to report real 1481 // errors. The solution is to rebuild the application with a 1482 // newer Go release. 1483 TooNew 1484 ) 1485