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 // Example: 118 // import "unsafe" 119 // 120 // type T [unsafe.Sizeof(T{})]int 121 InvalidDeclCycle 122 123 // TODO(markfreeman): Retire InvalidTypeCycle, as it's never emitted. 124 125 // InvalidTypeCycle occurs when a cycle in type definitions results in a 126 // type that is not well-defined. 127 InvalidTypeCycle 128 129 // InvalidConstInit occurs when a const declaration has a non-constant 130 // initializer. 131 // 132 // Example: 133 // var x int 134 // const _ = x 135 InvalidConstInit 136 137 // InvalidConstVal occurs when a const value cannot be converted to its 138 // target type. 139 // 140 // TODO(findleyr): this error code and example are not very clear. Consider 141 // removing it. 142 // 143 // Example: 144 // const _ = 1 << "hello" 145 InvalidConstVal 146 147 // InvalidConstType occurs when the underlying type in a const declaration 148 // is not a valid constant type. 149 // 150 // Example: 151 // const c *int = 4 152 InvalidConstType 153 154 // UntypedNilUse occurs when the predeclared (untyped) value nil is used to 155 // initialize a variable declared without an explicit type. 156 // 157 // Example: 158 // var x = nil 159 UntypedNilUse 160 161 // WrongAssignCount occurs when the number of values on the right-hand side 162 // of an assignment or initialization expression does not match the number 163 // of variables on the left-hand side. 164 // 165 // Example: 166 // var x = 1, 2 167 WrongAssignCount 168 169 // UnassignableOperand occurs when the left-hand side of an assignment is 170 // not assignable. 171 // 172 // Example: 173 // func f() { 174 // const c = 1 175 // c = 2 176 // } 177 UnassignableOperand 178 179 // NoNewVar occurs when a short variable declaration (':=') does not declare 180 // new variables. 181 // 182 // Example: 183 // func f() { 184 // x := 1 185 // x := 2 186 // } 187 NoNewVar 188 189 // MultiValAssignOp occurs when an assignment operation (+=, *=, etc) does 190 // not have single-valued left-hand or right-hand side. 191 // 192 // Per the spec: 193 // "In assignment operations, both the left- and right-hand expression lists 194 // must contain exactly one single-valued expression" 195 // 196 // Example: 197 // func f() int { 198 // x, y := 1, 2 199 // x, y += 1 200 // return x + y 201 // } 202 MultiValAssignOp 203 204 // InvalidIfaceAssign occurs when a value of type T is used as an 205 // interface, but T does not implement a method of the expected interface. 206 // 207 // Example: 208 // type I interface { 209 // f() 210 // } 211 // 212 // type T int 213 // 214 // var x I = T(1) 215 InvalidIfaceAssign 216 217 // InvalidChanAssign occurs when a chan assignment is invalid. 218 // 219 // Per the spec, a value x is assignable to a channel type T if: 220 // "x is a bidirectional channel value, T is a channel type, x's type V and 221 // T have identical element types, and at least one of V or T is not a 222 // defined type." 223 // 224 // Example: 225 // type T1 chan int 226 // type T2 chan int 227 // 228 // var x T1 229 // // Invalid assignment because both types are named 230 // var _ T2 = x 231 InvalidChanAssign 232 233 // IncompatibleAssign occurs when the type of the right-hand side expression 234 // in an assignment cannot be assigned to the type of the variable being 235 // assigned. 236 // 237 // Example: 238 // var x []int 239 // var _ int = x 240 IncompatibleAssign 241 242 // UnaddressableFieldAssign occurs when trying to assign to a struct field 243 // in a map value. 244 // 245 // Example: 246 // func f() { 247 // m := make(map[string]struct{i int}) 248 // m["foo"].i = 42 249 // } 250 UnaddressableFieldAssign 251 252 // NotAType occurs when the identifier used as the underlying type in a type 253 // declaration or the right-hand side of a type alias does not denote a type. 254 // 255 // Example: 256 // var S = 2 257 // 258 // type T S 259 NotAType 260 261 // InvalidArrayLen occurs when an array length is not a constant value. 262 // 263 // Example: 264 // var n = 3 265 // var _ = [n]int{} 266 InvalidArrayLen 267 268 // BlankIfaceMethod occurs when a method name is '_'. 269 // 270 // Per the spec: 271 // "The name of each explicitly specified method must be unique and not 272 // blank." 273 // 274 // Example: 275 // type T interface { 276 // _(int) 277 // } 278 BlankIfaceMethod 279 280 // IncomparableMapKey occurs when a map key type does not support the == and 281 // != operators. 282 // 283 // Per the spec: 284 // "The comparison operators == and != must be fully defined for operands of 285 // the key type; thus the key type must not be a function, map, or slice." 286 // 287 // Example: 288 // var x map[T]int 289 // 290 // type T []int 291 IncomparableMapKey 292 293 // InvalidIfaceEmbed occurs when a non-interface type is embedded in an 294 // interface (for go 1.17 or earlier). 295 _ // not used anymore 296 297 // InvalidPtrEmbed occurs when an embedded field is of the pointer form *T, 298 // and T itself is itself a pointer, an unsafe.Pointer, or an interface. 299 // 300 // Per the spec: 301 // "An embedded field must be specified as a type name T or as a pointer to 302 // a non-interface type name *T, and T itself may not be a pointer type." 303 // 304 // Example: 305 // type T *int 306 // 307 // type S struct { 308 // *T 309 // } 310 InvalidPtrEmbed 311 312 // BadRecv occurs when a method declaration does not have exactly one 313 // receiver parameter. 314 // 315 // Example: 316 // func () _() {} 317 BadRecv 318 319 // InvalidRecv occurs when a receiver type expression is not of the form T 320 // or *T, or T is a pointer type. 321 // 322 // Example: 323 // type T struct {} 324 // 325 // func (**T) m() {} 326 InvalidRecv 327 328 // DuplicateFieldAndMethod occurs when an identifier appears as both a field 329 // and method name. 330 // 331 // Example: 332 // type T struct { 333 // m int 334 // } 335 // 336 // func (T) m() {} 337 DuplicateFieldAndMethod 338 339 // DuplicateMethod occurs when two methods on the same receiver type have 340 // the same name. 341 // 342 // Example: 343 // type T struct {} 344 // func (T) m() {} 345 // func (T) m(i int) int { return i } 346 DuplicateMethod 347 348 // InvalidBlank occurs when a blank identifier is used as a value or type. 349 // 350 // Per the spec: 351 // "The blank identifier may appear as an operand only on the left-hand side 352 // of an assignment." 353 // 354 // Example: 355 // var x = _ 356 InvalidBlank 357 358 // InvalidIota occurs when the predeclared identifier iota is used outside 359 // of a constant declaration. 360 // 361 // Example: 362 // var x = iota 363 InvalidIota 364 365 // MissingInitBody occurs when an init function is missing its body. 366 // 367 // Example: 368 // func init() 369 MissingInitBody 370 371 // InvalidInitSig occurs when an init function declares parameters or 372 // results. 373 // 374 // Deprecated: no longer emitted by the type checker. _InvalidInitDecl is 375 // used instead. 376 InvalidInitSig 377 378 // InvalidInitDecl occurs when init is declared as anything other than a 379 // function. 380 // 381 // Example: 382 // var init = 1 383 // 384 // Example: 385 // func init() int { return 1 } 386 InvalidInitDecl 387 388 // InvalidMainDecl occurs when main is declared as anything other than a 389 // function, in a main package. 390 InvalidMainDecl 391 392 // TooManyValues occurs when a function returns too many values for the 393 // expression context in which it is used. 394 // 395 // Example: 396 // func ReturnTwo() (int, int) { 397 // return 1, 2 398 // } 399 // 400 // var x = ReturnTwo() 401 TooManyValues 402 403 // NotAnExpr occurs when a type expression is used where a value expression 404 // is expected. 405 // 406 // Example: 407 // type T struct {} 408 // 409 // func f() { 410 // T 411 // } 412 NotAnExpr 413 414 // TruncatedFloat occurs when a float constant is truncated to an integer 415 // value. 416 // 417 // Example: 418 // var _ int = 98.6 419 TruncatedFloat 420 421 // NumericOverflow occurs when a numeric constant overflows its target type. 422 // 423 // Example: 424 // var x int8 = 1000 425 NumericOverflow 426 427 // UndefinedOp occurs when an operator is not defined for the type(s) used 428 // in an operation. 429 // 430 // Example: 431 // var c = "a" - "b" 432 UndefinedOp 433 434 // MismatchedTypes occurs when operand types are incompatible in a binary 435 // operation. 436 // 437 // Example: 438 // var a = "hello" 439 // var b = 1 440 // var c = a - b 441 MismatchedTypes 442 443 // DivByZero occurs when a division operation is provable at compile 444 // time to be a division by zero. 445 // 446 // Example: 447 // const divisor = 0 448 // var x int = 1/divisor 449 DivByZero 450 451 // NonNumericIncDec occurs when an increment or decrement operator is 452 // applied to a non-numeric value. 453 // 454 // Example: 455 // func f() { 456 // var c = "c" 457 // c++ 458 // } 459 NonNumericIncDec 460 461 // UnaddressableOperand occurs when the & operator is applied to an 462 // unaddressable expression. 463 // 464 // Example: 465 // var x = &1 466 UnaddressableOperand 467 468 // InvalidIndirection occurs when a non-pointer value is indirected via the 469 // '*' operator. 470 // 471 // Example: 472 // var x int 473 // var y = *x 474 InvalidIndirection 475 476 // NonIndexableOperand occurs when an index operation is applied to a value 477 // that cannot be indexed. 478 // 479 // Example: 480 // var x = 1 481 // var y = x[1] 482 NonIndexableOperand 483 484 // InvalidIndex occurs when an index argument is not of integer type, 485 // negative, or out-of-bounds. 486 // 487 // Example: 488 // var s = [...]int{1,2,3} 489 // var x = s[5] 490 // 491 // Example: 492 // var s = []int{1,2,3} 493 // var _ = s[-1] 494 // 495 // Example: 496 // var s = []int{1,2,3} 497 // var i string 498 // var _ = s[i] 499 InvalidIndex 500 501 // SwappedSliceIndices occurs when constant indices in a slice expression 502 // are decreasing in value. 503 // 504 // Example: 505 // var _ = []int{1,2,3}[2:1] 506 SwappedSliceIndices 507 508 // NonSliceableOperand occurs when a slice operation is applied to a value 509 // whose type is not sliceable, or is unaddressable. 510 // 511 // Example: 512 // var x = [...]int{1, 2, 3}[:1] 513 // 514 // Example: 515 // var x = 1 516 // var y = 1[:1] 517 NonSliceableOperand 518 519 // InvalidSliceExpr occurs when a three-index slice expression (a[x:y:z]) is 520 // applied to a string. 521 // 522 // Example: 523 // var s = "hello" 524 // var x = s[1:2:3] 525 InvalidSliceExpr 526 527 // InvalidShiftCount occurs when the right-hand side of a shift operation is 528 // either non-integer, negative, or too large. 529 // 530 // Example: 531 // var ( 532 // x string 533 // y int = 1 << x 534 // ) 535 InvalidShiftCount 536 537 // InvalidShiftOperand occurs when the shifted operand is not an integer. 538 // 539 // Example: 540 // var s = "hello" 541 // var x = s << 2 542 InvalidShiftOperand 543 544 // InvalidReceive occurs when there is a channel receive from a value that 545 // is either not a channel, or is a send-only channel. 546 // 547 // Example: 548 // func f() { 549 // var x = 1 550 // <-x 551 // } 552 InvalidReceive 553 554 // InvalidSend occurs when there is a channel send to a value that is not a 555 // channel, or is a receive-only channel. 556 // 557 // Example: 558 // func f() { 559 // var x = 1 560 // x <- "hello!" 561 // } 562 InvalidSend 563 564 // DuplicateLitKey occurs when an index is duplicated in a slice, array, or 565 // map literal. 566 // 567 // Example: 568 // var _ = []int{0:1, 0:2} 569 // 570 // Example: 571 // var _ = map[string]int{"a": 1, "a": 2} 572 DuplicateLitKey 573 574 // MissingLitKey occurs when a map literal is missing a key expression. 575 // 576 // Example: 577 // var _ = map[string]int{1} 578 MissingLitKey 579 580 // InvalidLitIndex occurs when the key in a key-value element of a slice or 581 // array literal is not an integer constant. 582 // 583 // Example: 584 // var i = 0 585 // var x = []string{i: "world"} 586 InvalidLitIndex 587 588 // OversizeArrayLit occurs when an array literal exceeds its length. 589 // 590 // Example: 591 // var _ = [2]int{1,2,3} 592 OversizeArrayLit 593 594 // MixedStructLit occurs when a struct literal contains a mix of positional 595 // and named elements. 596 // 597 // Example: 598 // var _ = struct{i, j int}{i: 1, 2} 599 MixedStructLit 600 601 // InvalidStructLit occurs when a positional struct literal has an incorrect 602 // number of values. 603 // 604 // Example: 605 // var _ = struct{i, j int}{1,2,3} 606 InvalidStructLit 607 608 // MissingLitField occurs when a struct literal refers to a field that does 609 // not exist on the struct type. 610 // 611 // Example: 612 // var _ = struct{i int}{j: 2} 613 MissingLitField 614 615 // DuplicateLitField occurs when a struct literal contains duplicated 616 // fields. 617 // 618 // Example: 619 // var _ = struct{i int}{i: 1, i: 2} 620 DuplicateLitField 621 622 // UnexportedLitField occurs when a positional struct literal implicitly 623 // assigns an unexported field of an imported type. 624 UnexportedLitField 625 626 // InvalidLitField occurs when a field name is not a valid identifier. 627 // 628 // Example: 629 // var _ = struct{i int}{1: 1} 630 InvalidLitField 631 632 // UntypedLit occurs when a composite literal omits a required type 633 // identifier. 634 // 635 // Example: 636 // type outer struct{ 637 // inner struct { i int } 638 // } 639 // 640 // var _ = outer{inner: {1}} 641 UntypedLit 642 643 // InvalidLit occurs when a composite literal expression does not match its 644 // type. 645 // 646 // Example: 647 // type P *struct{ 648 // x int 649 // } 650 // var _ = P {} 651 InvalidLit 652 653 // AmbiguousSelector occurs when a selector is ambiguous. 654 // 655 // Example: 656 // type E1 struct { i int } 657 // type E2 struct { i int } 658 // type T struct { E1; E2 } 659 // 660 // var x T 661 // var _ = x.i 662 AmbiguousSelector 663 664 // UndeclaredImportedName occurs when a package-qualified identifier is 665 // undeclared by the imported package. 666 // 667 // Example: 668 // import "go/types" 669 // 670 // var _ = types.NotAnActualIdentifier 671 UndeclaredImportedName 672 673 // UnexportedName occurs when a selector refers to an unexported identifier 674 // of an imported package. 675 // 676 // Example: 677 // import "reflect" 678 // 679 // type _ reflect.flag 680 UnexportedName 681 682 // UndeclaredName occurs when an identifier is not declared in the current 683 // scope. 684 // 685 // Example: 686 // var x T 687 UndeclaredName 688 689 // MissingFieldOrMethod occurs when a selector references a field or method 690 // that does not exist. 691 // 692 // Example: 693 // type T struct {} 694 // 695 // var x = T{}.f 696 MissingFieldOrMethod 697 698 // BadDotDotDotSyntax occurs when a "..." occurs in a context where it is 699 // not valid. 700 // 701 // Example: 702 // var _ = map[int][...]int{0: {}} 703 BadDotDotDotSyntax 704 705 // NonVariadicDotDotDot occurs when a "..." is used on the final argument to 706 // a non-variadic function. 707 // 708 // Example: 709 // func printArgs(s []string) { 710 // for _, a := range s { 711 // println(a) 712 // } 713 // } 714 // 715 // func f() { 716 // s := []string{"a", "b", "c"} 717 // printArgs(s...) 718 // } 719 NonVariadicDotDotDot 720 721 // MisplacedDotDotDot occurs when a "..." is used somewhere other than the 722 // final argument in a function declaration. 723 _ // not used anymore (error reported by parser) 724 725 _ // InvalidDotDotDotOperand was removed. 726 727 // InvalidDotDotDot occurs when a "..." is used in a non-variadic built-in 728 // function. 729 // 730 // Example: 731 // var s = []int{1, 2, 3} 732 // var l = len(s...) 733 InvalidDotDotDot 734 735 // UncalledBuiltin occurs when a built-in function is used as a 736 // function-valued expression, instead of being called. 737 // 738 // Per the spec: 739 // "The built-in functions do not have standard Go types, so they can only 740 // appear in call expressions; they cannot be used as function values." 741 // 742 // Example: 743 // var _ = copy 744 UncalledBuiltin 745 746 // InvalidAppend occurs when append is called with a first argument that is 747 // not a slice. 748 // 749 // Example: 750 // var _ = append(1, 2) 751 InvalidAppend 752 753 // InvalidCap occurs when an argument to the cap built-in function is not of 754 // supported type. 755 // 756 // See https://golang.org/ref/spec#Length_and_capacity for information on 757 // which underlying types are supported as arguments to cap and len. 758 // 759 // Example: 760 // var s = 2 761 // var x = cap(s) 762 InvalidCap 763 764 // InvalidClose occurs when close(...) is called with an argument that is 765 // not of channel type, or that is a receive-only channel. 766 // 767 // Example: 768 // func f() { 769 // var x int 770 // close(x) 771 // } 772 InvalidClose 773 774 // InvalidCopy occurs when the arguments are not of slice type or do not 775 // have compatible type. 776 // 777 // See https://golang.org/ref/spec#Appending_and_copying_slices for more 778 // information on the type requirements for the copy built-in. 779 // 780 // Example: 781 // func f() { 782 // var x []int 783 // y := []int64{1,2,3} 784 // copy(x, y) 785 // } 786 InvalidCopy 787 788 // InvalidComplex occurs when the complex built-in function is called with 789 // arguments with incompatible types. 790 // 791 // Example: 792 // var _ = complex(float32(1), float64(2)) 793 InvalidComplex 794 795 // InvalidDelete occurs when the delete built-in function is called with a 796 // first argument that is not a map. 797 // 798 // Example: 799 // func f() { 800 // m := "hello" 801 // delete(m, "e") 802 // } 803 InvalidDelete 804 805 // InvalidImag occurs when the imag built-in function is called with an 806 // argument that does not have complex type. 807 // 808 // Example: 809 // var _ = imag(int(1)) 810 InvalidImag 811 812 // InvalidLen occurs when an argument to the len built-in function is not of 813 // supported type. 814 // 815 // See https://golang.org/ref/spec#Length_and_capacity for information on 816 // which underlying types are supported as arguments to cap and len. 817 // 818 // Example: 819 // var s = 2 820 // var x = len(s) 821 InvalidLen 822 823 // SwappedMakeArgs occurs when make is called with three arguments, and its 824 // length argument is larger than its capacity argument. 825 // 826 // Example: 827 // var x = make([]int, 3, 2) 828 SwappedMakeArgs 829 830 // InvalidMake occurs when make is called with an unsupported type argument. 831 // 832 // See https://golang.org/ref/spec#Making_slices_maps_and_channels for 833 // information on the types that may be created using make. 834 // 835 // Example: 836 // var x = make(int) 837 InvalidMake 838 839 // InvalidReal occurs when the real built-in function is called with an 840 // argument that does not have complex type. 841 // 842 // Example: 843 // var _ = real(int(1)) 844 InvalidReal 845 846 // InvalidAssert occurs when a type assertion is applied to a 847 // value that is not of interface type. 848 // 849 // Example: 850 // var x = 1 851 // var _ = x.(float64) 852 InvalidAssert 853 854 // ImpossibleAssert occurs for a type assertion x.(T) when the value x of 855 // interface cannot have dynamic type T, due to a missing or mismatching 856 // method on T. 857 // 858 // Example: 859 // type T int 860 // 861 // func (t *T) m() int { return int(*t) } 862 // 863 // type I interface { m() int } 864 // 865 // var x I 866 // var _ = x.(T) 867 ImpossibleAssert 868 869 // InvalidConversion occurs when the argument type cannot be converted to the 870 // target. 871 // 872 // See https://golang.org/ref/spec#Conversions for the rules of 873 // convertibility. 874 // 875 // Example: 876 // var x float64 877 // var _ = string(x) 878 InvalidConversion 879 880 // InvalidUntypedConversion occurs when there is no valid implicit 881 // conversion from an untyped value satisfying the type constraints of the 882 // context in which it is used. 883 // 884 // Example: 885 // func f[T ~int8 | ~int16 | ~int32 | ~int64](x T) T { 886 // return x + 1024 887 // } 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 1008 // a valid type for use with a range loop. 1009 // 1010 // Example: 1011 // func f(f float64) { 1012 // for j := range f { 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 and 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 // It also occurs if it is used in a package compiled for a 1277 // language version before go1.17. 1278 // 1279 // Example: 1280 // import "unsafe" 1281 // 1282 // var p unsafe.Pointer 1283 // var _ = unsafe.Add(p, float64(1)) 1284 InvalidUnsafeAdd 1285 1286 // InvalidUnsafeSlice occurs when unsafe.Slice is called with a 1287 // pointer argument that is not of pointer type or a length argument 1288 // that is not of integer type, negative, or out of bounds. 1289 // It also occurs if it is used in a package compiled for a language 1290 // version before go1.17. 1291 // 1292 // Example: 1293 // import "unsafe" 1294 // 1295 // var x int 1296 // var _ = unsafe.Slice(x, 1) 1297 // 1298 // Example: 1299 // import "unsafe" 1300 // 1301 // var x int 1302 // var _ = unsafe.Slice(&x, float64(1)) 1303 // 1304 // Example: 1305 // import "unsafe" 1306 // 1307 // var x int 1308 // var _ = unsafe.Slice(&x, -1) 1309 // 1310 // Example: 1311 // import "unsafe" 1312 // 1313 // var x int 1314 // var _ = unsafe.Slice(&x, uint64(1) << 63) 1315 InvalidUnsafeSlice 1316 1317 // All codes below were added in Go 1.18. 1318 1319 // UnsupportedFeature occurs when a language feature is used that is not 1320 // supported at this Go version. 1321 UnsupportedFeature 1322 1323 // NotAGenericType occurs when a non-generic type is used where a generic 1324 // type is expected: in type or function instantiation. 1325 // 1326 // Example: 1327 // type T int 1328 // 1329 // var _ T[int] 1330 NotAGenericType 1331 1332 // WrongTypeArgCount occurs when a type or function is instantiated with an 1333 // incorrect number of type arguments, including when a generic type or 1334 // function is used without instantiation. 1335 // 1336 // Errors involving failed type inference are assigned other error codes. 1337 // 1338 // Example: 1339 // type T[p any] int 1340 // 1341 // var _ T[int, string] 1342 // 1343 // Example: 1344 // func f[T any]() {} 1345 // 1346 // var x = f 1347 WrongTypeArgCount 1348 1349 // CannotInferTypeArgs occurs when type or function type argument inference 1350 // fails to infer all type arguments. 1351 // 1352 // Example: 1353 // func f[T any]() {} 1354 // 1355 // func _() { 1356 // f() 1357 // } 1358 CannotInferTypeArgs 1359 1360 // InvalidTypeArg occurs when a type argument does not satisfy its 1361 // corresponding type parameter constraints. 1362 // 1363 // Example: 1364 // type T[P ~int] struct{} 1365 // 1366 // var _ T[string] 1367 InvalidTypeArg // arguments? InferenceFailed 1368 1369 // InvalidInstanceCycle occurs when an invalid cycle is detected 1370 // within the instantiation graph. 1371 // 1372 // Example: 1373 // func f[T any]() { f[*T]() } 1374 InvalidInstanceCycle 1375 1376 // InvalidUnion occurs when an embedded union or approximation element is 1377 // not valid. 1378 // 1379 // Example: 1380 // type _ interface { 1381 // ~int | interface{ m() } 1382 // } 1383 InvalidUnion 1384 1385 // MisplacedConstraintIface occurs when a constraint-type interface is used 1386 // outside of constraint position. 1387 // 1388 // Example: 1389 // type I interface { ~int } 1390 // 1391 // var _ I 1392 MisplacedConstraintIface 1393 1394 // InvalidMethodTypeParams occurs when methods have type parameters. 1395 // 1396 // It cannot be encountered with an AST parsed using go/parser. 1397 InvalidMethodTypeParams 1398 1399 // MisplacedTypeParam occurs when a type parameter is used in a place where 1400 // it is not permitted. 1401 // 1402 // Example: 1403 // type T[P any] P 1404 // 1405 // Example: 1406 // type T[P any] struct{ *P } 1407 MisplacedTypeParam 1408 1409 // InvalidUnsafeSliceData occurs when unsafe.SliceData is called with 1410 // an argument that is not of slice type. It also occurs if it is used 1411 // in a package compiled for a language version before go1.20. 1412 // 1413 // Example: 1414 // import "unsafe" 1415 // 1416 // var x int 1417 // var _ = unsafe.SliceData(x) 1418 InvalidUnsafeSliceData 1419 1420 // InvalidUnsafeString occurs when unsafe.String is called with 1421 // a length argument that is not of integer type, negative, or 1422 // out of bounds. It also occurs if it is used in a package 1423 // compiled for a language version before go1.20. 1424 // 1425 // Example: 1426 // import "unsafe" 1427 // 1428 // var b [10]byte 1429 // var _ = unsafe.String(&b[0], -1) 1430 InvalidUnsafeString 1431 1432 // InvalidUnsafeStringData occurs if it is used in a package 1433 // compiled for a language version before go1.20. 1434 _ // not used anymore 1435 1436 // InvalidClear occurs when clear is called with an argument 1437 // that is not of map or slice type. 1438 // 1439 // Example: 1440 // func _(x int) { 1441 // clear(x) 1442 // } 1443 InvalidClear 1444 1445 // TypeTooLarge occurs if unsafe.Sizeof or unsafe.Offsetof is 1446 // called with an expression whose type is too large. 1447 // 1448 // Example: 1449 // import "unsafe" 1450 // 1451 // type E [1 << 31 - 1]int 1452 // var a [1 << 31]E 1453 // var _ = unsafe.Sizeof(a) 1454 // 1455 // Example: 1456 // import "unsafe" 1457 // 1458 // type E [1 << 31 - 1]int 1459 // var s struct { 1460 // _ [1 << 31]E 1461 // x int 1462 // } 1463 // var _ = unsafe.Offsetof(s.x) 1464 TypeTooLarge 1465 1466 // InvalidMinMaxOperand occurs if min or max is called 1467 // with an operand that cannot be ordered because it 1468 // does not support the < operator. 1469 // 1470 // Example: 1471 // const _ = min(true) 1472 // 1473 // Example: 1474 // var s, t []byte 1475 // var _ = max(s, t) 1476 InvalidMinMaxOperand 1477 1478 // TooNew indicates that, through build tags or a go.mod file, 1479 // a source file requires a version of Go that is newer than 1480 // the logic of the type checker. As a consequence, the type 1481 // checker may produce spurious errors or fail to report real 1482 // errors. The solution is to rebuild the application with a 1483 // newer Go release. 1484 TooNew 1485 ) 1486