Debian Codegraph @

internal/xtools-internal/typesinternal/errorcode.go

raw
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 typesinternal
6
7 type ErrorCode int
8
9 // This file defines the error codes that can be produced during type-checking.
10 // Collectively, these codes provide an identifier that may be used to
11 // implement special handling for certain types of errors.
12 //
13 // Error codes should be fine-grained enough that the exact nature of the error
14 // can be easily determined, but coarse enough that they are not an
15 // implementation detail of the type checking algorithm. As a rule-of-thumb,
16 // errors should be considered equivalent if there is a theoretical refactoring
17 // of the type checker in which they are emitted in exactly one place. For
18 // example, the type checker emits different error messages for "too many
19 // arguments" and "too few arguments", but one can imagine an alternative type
20 // checker where this check instead just emits a single "wrong number of
21 // arguments", so these errors should have the same code.
22 //
23 // Error code names should be as brief as possible while retaining accuracy and
24 // distinctiveness. In most cases names should start with an adjective
25 // describing the nature of the error (e.g. "invalid", "unused", "misplaced"),
26 // and end with a noun identifying the relevant language object. For example,
27 // "DuplicateDecl" or "InvalidSliceExpr". For brevity, naming follows the
28 // convention that "bad" implies a problem with syntax, and "invalid" implies a
29 // problem with types.
30
31 const (
32 // InvalidSyntaxTree occurs if an invalid syntax tree is provided
33 // to the type checker. It should never happen.
34 InvalidSyntaxTree ErrorCode = -1
35 )
36
37 const (
38 _ ErrorCode = iota
39
40 // Test is reserved for errors that only apply while in self-test mode.
41 Test
42
43 /* package names */
44
45 // BlankPkgName occurs when a package name is the blank identifier "_".
46 //
47 // Per the spec:
48 // "The PackageName must not be the blank identifier."
49 BlankPkgName
50
51 // MismatchedPkgName occurs when a file's package name doesn't match the
52 // package name already established by other files.
53 MismatchedPkgName
54
55 // InvalidPkgUse occurs when a package identifier is used outside of a
56 // selector expression.
57 //
58 // Example:
59 // import "fmt"
60 //
61 // var _ = fmt
62 InvalidPkgUse
63
64 /* imports */
65
66 // BadImportPath occurs when an import path is not valid.
67 BadImportPath
68
69 // BrokenImport occurs when importing a package fails.
70 //
71 // Example:
72 // import "amissingpackage"
73 BrokenImport
74
75 // ImportCRenamed occurs when the special import "C" is renamed. "C" is a
76 // pseudo-package, and must not be renamed.
77 //
78 // Example:
79 // import _ "C"
80 ImportCRenamed
81
82 // UnusedImport occurs when an import is unused.
83 //
84 // Example:
85 // import "fmt"
86 //
87 // func main() {}
88 UnusedImport
89
90 /* initialization */
91
92 // InvalidInitCycle occurs when an invalid cycle is detected within the
93 // initialization graph.
94 //
95 // Example:
96 // var x int = f()
97 //
98 // func f() int { return x }
99 InvalidInitCycle
100
101 /* decls */
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 // import "unsafe"
114 //
115 // type T struct {
116 // a [n]int
117 // }
118 //
119 // var n = unsafe.Sizeof(T{})
120 InvalidDeclCycle
121
122 // InvalidTypeCycle occurs when a cycle in type definitions results in a
123 // type that is not well-defined.
124 //
125 // Example:
126 // import "unsafe"
127 //
128 // type T [unsafe.Sizeof(T{})]int
129 InvalidTypeCycle
130
131 /* decls > const */
132
133 // InvalidConstInit occurs when a const declaration has a non-constant
134 // initializer.
135 //
136 // Example:
137 // var x int
138 // const _ = x
139 InvalidConstInit
140
141 // InvalidConstVal occurs when a const value cannot be converted to its
142 // target type.
143 //
144 // TODO(findleyr): this error code and example are not very clear. Consider
145 // removing it.
146 //
147 // Example:
148 // const _ = 1 << "hello"
149 InvalidConstVal
150
151 // InvalidConstType occurs when the underlying type in a const declaration
152 // is not a valid constant type.
153 //
154 // Example:
155 // const c *int = 4
156 InvalidConstType
157
158 /* decls > var (+ other variable assignment codes) */
159
160 // UntypedNilUse occurs when the predeclared (untyped) value nil is used to
161 // initialize a variable declared without an explicit type.
162 //
163 // Example:
164 // var x = nil
165 UntypedNilUse
166
167 // WrongAssignCount occurs when the number of values on the right-hand side
168 // of an assignment or initialization expression does not match the number
169 // of variables on the left-hand side.
170 //
171 // Example:
172 // var x = 1, 2
173 WrongAssignCount
174
175 // UnassignableOperand occurs when the left-hand side of an assignment is
176 // not assignable.
177 //
178 // Example:
179 // func f() {
180 // const c = 1
181 // c = 2
182 // }
183 UnassignableOperand
184
185 // NoNewVar occurs when a short variable declaration (':=') does not declare
186 // new variables.
187 //
188 // Example:
189 // func f() {
190 // x := 1
191 // x := 2
192 // }
193 NoNewVar
194
195 // MultiValAssignOp occurs when an assignment operation (+=, *=, etc) does
196 // not have single-valued left-hand or right-hand side.
197 //
198 // Per the spec:
199 // "In assignment operations, both the left- and right-hand expression lists
200 // must contain exactly one single-valued expression"
201 //
202 // Example:
203 // func f() int {
204 // x, y := 1, 2
205 // x, y += 1
206 // return x + y
207 // }
208 MultiValAssignOp
209
210 // InvalidIfaceAssign occurs when a value of type T is used as an
211 // interface, but T does not implement a method of the expected interface.
212 //
213 // Example:
214 // type I interface {
215 // f()
216 // }
217 //
218 // type T int
219 //
220 // var x I = T(1)
221 InvalidIfaceAssign
222
223 // InvalidChanAssign occurs when a chan assignment is invalid.
224 //
225 // Per the spec, a value x is assignable to a channel type T if:
226 // "x is a bidirectional channel value, T is a channel type, x's type V and
227 // T have identical element types, and at least one of V or T is not a
228 // defined type."
229 //
230 // Example:
231 // type T1 chan int
232 // type T2 chan int
233 //
234 // var x T1
235 // // Invalid assignment because both types are named
236 // var _ T2 = x
237 InvalidChanAssign
238
239 // IncompatibleAssign occurs when the type of the right-hand side expression
240 // in an assignment cannot be assigned to the type of the variable being
241 // assigned.
242 //
243 // Example:
244 // var x []int
245 // var _ int = x
246 IncompatibleAssign
247
248 // UnaddressableFieldAssign occurs when trying to assign to a struct field
249 // in a map value.
250 //
251 // Example:
252 // func f() {
253 // m := make(map[string]struct{i int})
254 // m["foo"].i = 42
255 // }
256 UnaddressableFieldAssign
257
258 /* decls > type (+ other type expression codes) */
259
260 // NotAType occurs when the identifier used as the underlying type in a type
261 // declaration or the right-hand side of a type alias does not denote a type.
262 //
263 // Example:
264 // var S = 2
265 //
266 // type T S
267 NotAType
268
269 // InvalidArrayLen occurs when an array length is not a constant value.
270 //
271 // Example:
272 // var n = 3
273 // var _ = [n]int{}
274 InvalidArrayLen
275
276 // BlankIfaceMethod occurs when a method name is '_'.
277 //
278 // Per the spec:
279 // "The name of each explicitly specified method must be unique and not
280 // blank."
281 //
282 // Example:
283 // type T interface {
284 // _(int)
285 // }
286 BlankIfaceMethod
287
288 // IncomparableMapKey occurs when a map key type does not support the == and
289 // != operators.
290 //
291 // Per the spec:
292 // "The comparison operators == and != must be fully defined for operands of
293 // the key type; thus the key type must not be a function, map, or slice."
294 //
295 // Example:
296 // var x map[T]int
297 //
298 // type T []int
299 IncomparableMapKey
300
301 // InvalidIfaceEmbed occurs when a non-interface type is embedded in an
302 // interface.
303 //
304 // Example:
305 // type T struct {}
306 //
307 // func (T) m()
308 //
309 // type I interface {
310 // T
311 // }
312 InvalidIfaceEmbed
313
314 // InvalidPtrEmbed occurs when an embedded field is of the pointer form *T,
315 // and T itself is itself a pointer, an unsafe.Pointer, or an interface.
316 //
317 // Per the spec:
318 // "An embedded field must be specified as a type name T or as a pointer to
319 // a non-interface type name *T, and T itself may not be a pointer type."
320 //
321 // Example:
322 // type T *int
323 //
324 // type S struct {
325 // *T
326 // }
327 InvalidPtrEmbed
328
329 /* decls > func and method */
330
331 // BadRecv occurs when a method declaration does not have exactly one
332 // receiver parameter.
333 //
334 // Example:
335 // func () _() {}
336 BadRecv
337
338 // InvalidRecv occurs when a receiver type expression is not of the form T
339 // or *T, or T is a pointer type.
340 //
341 // Example:
342 // type T struct {}
343 //
344 // func (**T) m() {}
345 InvalidRecv
346
347 // DuplicateFieldAndMethod occurs when an identifier appears as both a field
348 // and method name.
349 //
350 // Example:
351 // type T struct {
352 // m int
353 // }
354 //
355 // func (T) m() {}
356 DuplicateFieldAndMethod
357
358 // DuplicateMethod occurs when two methods on the same receiver type have
359 // the same name.
360 //
361 // Example:
362 // type T struct {}
363 // func (T) m() {}
364 // func (T) m(i int) int { return i }
365 DuplicateMethod
366
367 /* decls > special */
368
369 // InvalidBlank occurs when a blank identifier is used as a value or type.
370 //
371 // Per the spec:
372 // "The blank identifier may appear as an operand only on the left-hand side
373 // of an assignment."
374 //
375 // Example:
376 // var x = _
377 InvalidBlank
378
379 // InvalidIota occurs when the predeclared identifier iota is used outside
380 // of a constant declaration.
381 //
382 // Example:
383 // var x = iota
384 InvalidIota
385
386 // MissingInitBody occurs when an init function is missing its body.
387 //
388 // Example:
389 // func init()
390 MissingInitBody
391
392 // InvalidInitSig occurs when an init function declares parameters or
393 // results.
394 //
395 // Example:
396 // func init() int { return 1 }
397 InvalidInitSig
398
399 // InvalidInitDecl occurs when init is declared as anything other than a
400 // function.
401 //
402 // Example:
403 // var init = 1
404 InvalidInitDecl
405
406 // InvalidMainDecl occurs when main is declared as anything other than a
407 // function, in a main package.
408 InvalidMainDecl
409
410 /* exprs */
411
412 // TooManyValues occurs when a function returns too many values for the
413 // expression context in which it is used.
414 //
415 // Example:
416 // func ReturnTwo() (int, int) {
417 // return 1, 2
418 // }
419 //
420 // var x = ReturnTwo()
421 TooManyValues
422
423 // NotAnExpr occurs when a type expression is used where a value expression
424 // is expected.
425 //
426 // Example:
427 // type T struct {}
428 //
429 // func f() {
430 // T
431 // }
432 NotAnExpr
433
434 /* exprs > const */
435
436 // TruncatedFloat occurs when a float constant is truncated to an integer
437 // value.
438 //
439 // Example:
440 // var _ int = 98.6
441 TruncatedFloat
442
443 // NumericOverflow occurs when a numeric constant overflows its target type.
444 //
445 // Example:
446 // var x int8 = 1000
447 NumericOverflow
448
449 /* exprs > operation */
450
451 // UndefinedOp occurs when an operator is not defined for the type(s) used
452 // in an operation.
453 //
454 // Example:
455 // var c = "a" - "b"
456 UndefinedOp
457
458 // MismatchedTypes occurs when operand types are incompatible in a binary
459 // operation.
460 //
461 // Example:
462 // var a = "hello"
463 // var b = 1
464 // var c = a - b
465 MismatchedTypes
466
467 // DivByZero occurs when a division operation is provable at compile
468 // time to be a division by zero.
469 //
470 // Example:
471 // const divisor = 0
472 // var x int = 1/divisor
473 DivByZero
474
475 // NonNumericIncDec occurs when an increment or decrement operator is
476 // applied to a non-numeric value.
477 //
478 // Example:
479 // func f() {
480 // var c = "c"
481 // c++
482 // }
483 NonNumericIncDec
484
485 /* exprs > ptr */
486
487 // UnaddressableOperand occurs when the & operator is applied to an
488 // unaddressable expression.
489 //
490 // Example:
491 // var x = &1
492 UnaddressableOperand
493
494 // InvalidIndirection occurs when a non-pointer value is indirected via the
495 // '*' operator.
496 //
497 // Example:
498 // var x int
499 // var y = *x
500 InvalidIndirection
501
502 /* exprs > [] */
503
504 // NonIndexableOperand occurs when an index operation is applied to a value
505 // that cannot be indexed.
506 //
507 // Example:
508 // var x = 1
509 // var y = x[1]
510 NonIndexableOperand
511
512 // InvalidIndex occurs when an index argument is not of integer type,
513 // negative, or out-of-bounds.
514 //
515 // Example:
516 // var s = [...]int{1,2,3}
517 // var x = s[5]
518 //
519 // Example:
520 // var s = []int{1,2,3}
521 // var _ = s[-1]
522 //
523 // Example:
524 // var s = []int{1,2,3}
525 // var i string
526 // var _ = s[i]
527 InvalidIndex
528
529 // SwappedSliceIndices occurs when constant indices in a slice expression
530 // are decreasing in value.
531 //
532 // Example:
533 // var _ = []int{1,2,3}[2:1]
534 SwappedSliceIndices
535
536 /* operators > slice */
537
538 // NonSliceableOperand occurs when a slice operation is applied to a value
539 // whose type is not sliceable, or is unaddressable.
540 //
541 // Example:
542 // var x = [...]int{1, 2, 3}[:1]
543 //
544 // Example:
545 // var x = 1
546 // var y = 1[:1]
547 NonSliceableOperand
548
549 // InvalidSliceExpr occurs when a three-index slice expression (a[x:y:z]) is
550 // applied to a string.
551 //
552 // Example:
553 // var s = "hello"
554 // var x = s[1:2:3]
555 InvalidSliceExpr
556
557 /* exprs > shift */
558
559 // InvalidShiftCount occurs when the right-hand side of a shift operation is
560 // either non-integer, negative, or too large.
561 //
562 // Example:
563 // var (
564 // x string
565 // y int = 1 << x
566 // )
567 InvalidShiftCount
568
569 // InvalidShiftOperand occurs when the shifted operand is not an integer.
570 //
571 // Example:
572 // var s = "hello"
573 // var x = s << 2
574 InvalidShiftOperand
575
576 /* exprs > chan */
577
578 // InvalidReceive occurs when there is a channel receive from a value that
579 // is either not a channel, or is a send-only channel.
580 //
581 // Example:
582 // func f() {
583 // var x = 1
584 // <-x
585 // }
586 InvalidReceive
587
588 // InvalidSend occurs when there is a channel send to a value that is not a
589 // channel, or is a receive-only channel.
590 //
591 // Example:
592 // func f() {
593 // var x = 1
594 // x <- "hello!"
595 // }
596 InvalidSend
597
598 /* exprs > literal */
599
600 // DuplicateLitKey occurs when an index is duplicated in a slice, array, or
601 // map literal.
602 //
603 // Example:
604 // var _ = []int{0:1, 0:2}
605 //
606 // Example:
607 // var _ = map[string]int{"a": 1, "a": 2}
608 DuplicateLitKey
609
610 // MissingLitKey occurs when a map literal is missing a key expression.
611 //
612 // Example:
613 // var _ = map[string]int{1}
614 MissingLitKey
615
616 // InvalidLitIndex occurs when the key in a key-value element of a slice or
617 // array literal is not an integer constant.
618 //
619 // Example:
620 // var i = 0
621 // var x = []string{i: "world"}
622 InvalidLitIndex
623
624 // OversizeArrayLit occurs when an array literal exceeds its length.
625 //
626 // Example:
627 // var _ = [2]int{1,2,3}
628 OversizeArrayLit
629
630 // MixedStructLit occurs when a struct literal contains a mix of positional
631 // and named elements.
632 //
633 // Example:
634 // var _ = struct{i, j int}{i: 1, 2}
635 MixedStructLit
636
637 // InvalidStructLit occurs when a positional struct literal has an incorrect
638 // number of values.
639 //
640 // Example:
641 // var _ = struct{i, j int}{1,2,3}
642 InvalidStructLit
643
644 // MissingLitField occurs when a struct literal refers to a field that does
645 // not exist on the struct type.
646 //
647 // Example:
648 // var _ = struct{i int}{j: 2}
649 MissingLitField
650
651 // DuplicateLitField occurs when a struct literal contains duplicated
652 // fields.
653 //
654 // Example:
655 // var _ = struct{i int}{i: 1, i: 2}
656 DuplicateLitField
657
658 // UnexportedLitField occurs when a positional struct literal implicitly
659 // assigns an unexported field of an imported type.
660 UnexportedLitField
661
662 // InvalidLitField occurs when a field name is not a valid identifier.
663 //
664 // Example:
665 // var _ = struct{i int}{1: 1}
666 InvalidLitField
667
668 // UntypedLit occurs when a composite literal omits a required type
669 // identifier.
670 //
671 // Example:
672 // type outer struct{
673 // inner struct { i int }
674 // }
675 //
676 // var _ = outer{inner: {1}}
677 UntypedLit
678
679 // InvalidLit occurs when a composite literal expression does not match its
680 // type.
681 //
682 // Example:
683 // type P *struct{
684 // x int
685 // }
686 // var _ = P {}
687 InvalidLit
688
689 /* exprs > selector */
690
691 // AmbiguousSelector occurs when a selector is ambiguous.
692 //
693 // Example:
694 // type E1 struct { i int }
695 // type E2 struct { i int }
696 // type T struct { E1; E2 }
697 //
698 // var x T
699 // var _ = x.i
700 AmbiguousSelector
701
702 // UndeclaredImportedName occurs when a package-qualified identifier is
703 // undeclared by the imported package.
704 //
705 // Example:
706 // import "go/types"
707 //
708 // var _ = types.NotAnActualIdentifier
709 UndeclaredImportedName
710
711 // UnexportedName occurs when a selector refers to an unexported identifier
712 // of an imported package.
713 //
714 // Example:
715 // import "reflect"
716 //
717 // type _ reflect.flag
718 UnexportedName
719
720 // UndeclaredName occurs when an identifier is not declared in the current
721 // scope.
722 //
723 // Example:
724 // var x T
725 UndeclaredName
726
727 // MissingFieldOrMethod occurs when a selector references a field or method
728 // that does not exist.
729 //
730 // Example:
731 // type T struct {}
732 //
733 // var x = T{}.f
734 MissingFieldOrMethod
735
736 /* exprs > ... */
737
738 // BadDotDotDotSyntax occurs when a "..." occurs in a context where it is
739 // not valid.
740 //
741 // Example:
742 // var _ = map[int][...]int{0: {}}
743 BadDotDotDotSyntax
744
745 // NonVariadicDotDotDot occurs when a "..." is used on the final argument to
746 // a non-variadic function.
747 //
748 // Example:
749 // func printArgs(s []string) {
750 // for _, a := range s {
751 // println(a)
752 // }
753 // }
754 //
755 // func f() {
756 // s := []string{"a", "b", "c"}
757 // printArgs(s...)
758 // }
759 NonVariadicDotDotDot
760
761 // MisplacedDotDotDot occurs when a "..." is used somewhere other than the
762 // final argument to a function call.
763 //
764 // Example:
765 // func printArgs(args ...int) {
766 // for _, a := range args {
767 // println(a)
768 // }
769 // }
770 //
771 // func f() {
772 // a := []int{1,2,3}
773 // printArgs(0, a...)
774 // }
775 MisplacedDotDotDot
776
777 // InvalidDotDotDotOperand occurs when a "..." operator is applied to a
778 // single-valued operand.
779 //
780 // Example:
781 // func printArgs(args ...int) {
782 // for _, a := range args {
783 // println(a)
784 // }
785 // }
786 //
787 // func f() {
788 // a := 1
789 // printArgs(a...)
790 // }
791 //
792 // Example:
793 // func args() (int, int) {
794 // return 1, 2
795 // }
796 //
797 // func printArgs(args ...int) {
798 // for _, a := range args {
799 // println(a)
800 // }
801 // }
802 //
803 // func g() {
804 // printArgs(args()...)
805 // }
806 InvalidDotDotDotOperand
807
808 // InvalidDotDotDot occurs when a "..." is used in a non-variadic built-in
809 // function.
810 //
811 // Example:
812 // var s = []int{1, 2, 3}
813 // var l = len(s...)
814 InvalidDotDotDot
815
816 /* exprs > built-in */
817
818 // UncalledBuiltin occurs when a built-in function is used as a
819 // function-valued expression, instead of being called.
820 //
821 // Per the spec:
822 // "The built-in functions do not have standard Go types, so they can only
823 // appear in call expressions; they cannot be used as function values."
824 //
825 // Example:
826 // var _ = copy
827 UncalledBuiltin
828
829 // InvalidAppend occurs when append is called with a first argument that is
830 // not a slice.
831 //
832 // Example:
833 // var _ = append(1, 2)
834 InvalidAppend
835
836 // InvalidCap occurs when an argument to the cap built-in function is not of
837 // supported type.
838 //
839 // See https://golang.org/ref/spec#Length_and_capacity for information on
840 // which underlying types are supported as arguments to cap and len.
841 //
842 // Example:
843 // var s = 2
844 // var x = cap(s)
845 InvalidCap
846
847 // InvalidClose occurs when close(...) is called with an argument that is
848 // not of channel type, or that is a receive-only channel.
849 //
850 // Example:
851 // func f() {
852 // var x int
853 // close(x)
854 // }
855 InvalidClose
856
857 // InvalidCopy occurs when the arguments are not of slice type or do not
858 // have compatible type.
859 //
860 // See https://golang.org/ref/spec#Appending_and_copying_slices for more
861 // information on the type requirements for the copy built-in.
862 //
863 // Example:
864 // func f() {
865 // var x []int
866 // y := []int64{1,2,3}
867 // copy(x, y)
868 // }
869 InvalidCopy
870
871 // InvalidComplex occurs when the complex built-in function is called with
872 // arguments with incompatible types.
873 //
874 // Example:
875 // var _ = complex(float32(1), float64(2))
876 InvalidComplex
877
878 // InvalidDelete occurs when the delete built-in function is called with a
879 // first argument that is not a map.
880 //
881 // Example:
882 // func f() {
883 // m := "hello"
884 // delete(m, "e")
885 // }
886 InvalidDelete
887
888 // InvalidImag occurs when the imag built-in function is called with an
889 // argument that does not have complex type.
890 //
891 // Example:
892 // var _ = imag(int(1))
893 InvalidImag
894
895 // InvalidLen occurs when an argument to the len built-in function is not of
896 // supported type.
897 //
898 // See https://golang.org/ref/spec#Length_and_capacity for information on
899 // which underlying types are supported as arguments to cap and len.
900 //
901 // Example:
902 // var s = 2
903 // var x = len(s)
904 InvalidLen
905
906 // SwappedMakeArgs occurs when make is called with three arguments, and its
907 // length argument is larger than its capacity argument.
908 //
909 // Example:
910 // var x = make([]int, 3, 2)
911 SwappedMakeArgs
912
913 // InvalidMake occurs when make is called with an unsupported type argument.
914 //
915 // See https://golang.org/ref/spec#Making_slices_maps_and_channels for
916 // information on the types that may be created using make.
917 //
918 // Example:
919 // var x = make(int)
920 InvalidMake
921
922 // InvalidReal occurs when the real built-in function is called with an
923 // argument that does not have complex type.
924 //
925 // Example:
926 // var _ = real(int(1))
927 InvalidReal
928
929 /* exprs > assertion */
930
931 // InvalidAssert occurs when a type assertion is applied to a
932 // value that is not of interface type.
933 //
934 // Example:
935 // var x = 1
936 // var _ = x.(float64)
937 InvalidAssert
938
939 // ImpossibleAssert occurs for a type assertion x.(T) when the value x of
940 // interface cannot have dynamic type T, due to a missing or mismatching
941 // method on T.
942 //
943 // Example:
944 // type T int
945 //
946 // func (t *T) m() int { return int(*t) }
947 //
948 // type I interface { m() int }
949 //
950 // var x I
951 // var _ = x.(T)
952 ImpossibleAssert
953
954 /* exprs > conversion */
955
956 // InvalidConversion occurs when the argument type cannot be converted to the
957 // target.
958 //
959 // See https://golang.org/ref/spec#Conversions for the rules of
960 // convertibility.
961 //
962 // Example:
963 // var x float64
964 // var _ = string(x)
965 InvalidConversion
966
967 // InvalidUntypedConversion occurs when there is no valid implicit
968 // conversion from an untyped value satisfying the type constraints of the
969 // context in which it is used.
970 //
971 // Example:
972 // var _ = 1 + ""
973 InvalidUntypedConversion
974
975 /* offsetof */
976
977 // BadOffsetofSyntax occurs when unsafe.Offsetof is called with an argument
978 // that is not a selector expression.
979 //
980 // Example:
981 // import "unsafe"
982 //
983 // var x int
984 // var _ = unsafe.Offsetof(x)
985 BadOffsetofSyntax
986
987 // InvalidOffsetof occurs when unsafe.Offsetof is called with a method
988 // selector, rather than a field selector, or when the field is embedded via
989 // a pointer.
990 //
991 // Per the spec:
992 //
993 // "If f is an embedded field, it must be reachable without pointer
994 // indirections through fields of the struct. "
995 //
996 // Example:
997 // import "unsafe"
998 //
999 // type T struct { f int }
1000 // type S struct { *T }
1001 // var s S
1002 // var _ = unsafe.Offsetof(s.f)
1003 //
1004 // Example:
1005 // import "unsafe"
1006 //
1007 // type S struct{}
1008 //
1009 // func (S) m() {}
1010 //
1011 // var s S
1012 // var _ = unsafe.Offsetof(s.m)
1013 InvalidOffsetof
1014
1015 /* control flow > scope */
1016
1017 // UnusedExpr occurs when a side-effect free expression is used as a
1018 // statement. Such a statement has no effect.
1019 //
1020 // Example:
1021 // func f(i int) {
1022 // i*i
1023 // }
1024 UnusedExpr
1025
1026 // UnusedVar occurs when a variable is declared but unused.
1027 //
1028 // Example:
1029 // func f() {
1030 // x := 1
1031 // }
1032 UnusedVar
1033
1034 // MissingReturn occurs when a function with results is missing a return
1035 // statement.
1036 //
1037 // Example:
1038 // func f() int {}
1039 MissingReturn
1040
1041 // WrongResultCount occurs when a return statement returns an incorrect
1042 // number of values.
1043 //
1044 // Example:
1045 // func ReturnOne() int {
1046 // return 1, 2
1047 // }
1048 WrongResultCount
1049
1050 // OutOfScopeResult occurs when the name of a value implicitly returned by
1051 // an empty return statement is shadowed in a nested scope.
1052 //
1053 // Example:
1054 // func factor(n int) (i int) {
1055 // for i := 2; i < n; i++ {
1056 // if n%i == 0 {
1057 // return
1058 // }
1059 // }
1060 // return 0
1061 // }
1062 OutOfScopeResult
1063
1064 /* control flow > if */
1065
1066 // InvalidCond occurs when an if condition is not a boolean expression.
1067 //
1068 // Example:
1069 // func checkReturn(i int) {
1070 // if i {
1071 // panic("non-zero return")
1072 // }
1073 // }
1074 InvalidCond
1075
1076 /* control flow > for */
1077
1078 // InvalidPostDecl occurs when there is a declaration in a for-loop post
1079 // statement.
1080 //
1081 // Example:
1082 // func f() {
1083 // for i := 0; i < 10; j := 0 {}
1084 // }
1085 InvalidPostDecl
1086
1087 // InvalidChanRange occurs when a send-only channel used in a range
1088 // expression.
1089 //
1090 // Example:
1091 // func sum(c chan<- int) {
1092 // s := 0
1093 // for i := range c {
1094 // s += i
1095 // }
1096 // }
1097 InvalidChanRange
1098
1099 // InvalidIterVar occurs when two iteration variables are used while ranging
1100 // over a channel.
1101 //
1102 // Example:
1103 // func f(c chan int) {
1104 // for k, v := range c {
1105 // println(k, v)
1106 // }
1107 // }
1108 InvalidIterVar
1109
1110 // InvalidRangeExpr occurs when the type of a range expression is not array,
1111 // slice, string, map, or channel.
1112 //
1113 // Example:
1114 // func f(i int) {
1115 // for j := range i {
1116 // println(j)
1117 // }
1118 // }
1119 InvalidRangeExpr
1120
1121 /* control flow > switch */
1122
1123 // MisplacedBreak occurs when a break statement is not within a for, switch,
1124 // or select statement of the innermost function definition.
1125 //
1126 // Example:
1127 // func f() {
1128 // break
1129 // }
1130 MisplacedBreak
1131
1132 // MisplacedContinue occurs when a continue statement is not within a for
1133 // loop of the innermost function definition.
1134 //
1135 // Example:
1136 // func sumeven(n int) int {
1137 // proceed := func() {
1138 // continue
1139 // }
1140 // sum := 0
1141 // for i := 1; i <= n; i++ {
1142 // if i % 2 != 0 {
1143 // proceed()
1144 // }
1145 // sum += i
1146 // }
1147 // return sum
1148 // }
1149 MisplacedContinue
1150
1151 // MisplacedFallthrough occurs when a fallthrough statement is not within an
1152 // expression switch.
1153 //
1154 // Example:
1155 // func typename(i interface{}) string {
1156 // switch i.(type) {
1157 // case int64:
1158 // fallthrough
1159 // case int:
1160 // return "int"
1161 // }
1162 // return "unsupported"
1163 // }
1164 MisplacedFallthrough
1165
1166 // DuplicateCase occurs when a type or expression switch has duplicate
1167 // cases.
1168 //
1169 // Example:
1170 // func printInt(i int) {
1171 // switch i {
1172 // case 1:
1173 // println("one")
1174 // case 1:
1175 // println("One")
1176 // }
1177 // }
1178 DuplicateCase
1179
1180 // DuplicateDefault occurs when a type or expression switch has multiple
1181 // default clauses.
1182 //
1183 // Example:
1184 // func printInt(i int) {
1185 // switch i {
1186 // case 1:
1187 // println("one")
1188 // default:
1189 // println("One")
1190 // default:
1191 // println("1")
1192 // }
1193 // }
1194 DuplicateDefault
1195
1196 // BadTypeKeyword occurs when a .(type) expression is used anywhere other
1197 // than a type switch.
1198 //
1199 // Example:
1200 // type I interface {
1201 // m()
1202 // }
1203 // var t I
1204 // var _ = t.(type)
1205 BadTypeKeyword
1206
1207 // InvalidTypeSwitch occurs when .(type) is used on an expression that is
1208 // not of interface type.
1209 //
1210 // Example:
1211 // func f(i int) {
1212 // switch x := i.(type) {}
1213 // }
1214 InvalidTypeSwitch
1215
1216 // InvalidExprSwitch occurs when a switch expression is not comparable.
1217 //
1218 // Example:
1219 // func _() {
1220 // var a struct{ _ func() }
1221 // switch a /* ERROR cannot switch on a */ {
1222 // }
1223 // }
1224 InvalidExprSwitch
1225
1226 /* control flow > select */
1227
1228 // InvalidSelectCase occurs when a select case is not a channel send or
1229 // receive.
1230 //
1231 // Example:
1232 // func checkChan(c <-chan int) bool {
1233 // select {
1234 // case c:
1235 // return true
1236 // default:
1237 // return false
1238 // }
1239 // }
1240 InvalidSelectCase
1241
1242 /* control flow > labels and jumps */
1243
1244 // UndeclaredLabel occurs when an undeclared label is jumped to.
1245 //
1246 // Example:
1247 // func f() {
1248 // goto L
1249 // }
1250 UndeclaredLabel
1251
1252 // DuplicateLabel occurs when a label is declared more than once.
1253 //
1254 // Example:
1255 // func f() int {
1256 // L:
1257 // L:
1258 // return 1
1259 // }
1260 DuplicateLabel
1261
1262 // MisplacedLabel occurs when a break or continue label is not on a for,
1263 // switch, or select statement.
1264 //
1265 // Example:
1266 // func f() {
1267 // L:
1268 // a := []int{1,2,3}
1269 // for _, e := range a {
1270 // if e > 10 {
1271 // break L
1272 // }
1273 // println(a)
1274 // }
1275 // }
1276 MisplacedLabel
1277
1278 // UnusedLabel occurs when a label is declared but not used.
1279 //
1280 // Example:
1281 // func f() {
1282 // L:
1283 // }
1284 UnusedLabel
1285
1286 // JumpOverDecl occurs when a label jumps over a variable declaration.
1287 //
1288 // Example:
1289 // func f() int {
1290 // goto L
1291 // x := 2
1292 // L:
1293 // x++
1294 // return x
1295 // }
1296 JumpOverDecl
1297
1298 // JumpIntoBlock occurs when a forward jump goes to a label inside a nested
1299 // block.
1300 //
1301 // Example:
1302 // func f(x int) {
1303 // goto L
1304 // if x > 0 {
1305 // L:
1306 // print("inside block")
1307 // }
1308 // }
1309 JumpIntoBlock
1310
1311 /* control flow > calls */
1312
1313 // InvalidMethodExpr occurs when a pointer method is called but the argument
1314 // is not addressable.
1315 //
1316 // Example:
1317 // type T struct {}
1318 //
1319 // func (*T) m() int { return 1 }
1320 //
1321 // var _ = T.m(T{})
1322 InvalidMethodExpr
1323
1324 // WrongArgCount occurs when too few or too many arguments are passed by a
1325 // function call.
1326 //
1327 // Example:
1328 // func f(i int) {}
1329 // var x = f()
1330 WrongArgCount
1331
1332 // InvalidCall occurs when an expression is called that is not of function
1333 // type.
1334 //
1335 // Example:
1336 // var x = "x"
1337 // var y = x()
1338 InvalidCall
1339
1340 /* control flow > suspended */
1341
1342 // UnusedResults occurs when a restricted expression-only built-in function
1343 // is suspended via go or defer. Such a suspension discards the results of
1344 // these side-effect free built-in functions, and therefore is ineffectual.
1345 //
1346 // Example:
1347 // func f(a []int) int {
1348 // defer len(a)
1349 // return i
1350 // }
1351 UnusedResults
1352
1353 // InvalidDefer occurs when a deferred expression is not a function call,
1354 // for example if the expression is a type conversion.
1355 //
1356 // Example:
1357 // func f(i int) int {
1358 // defer int32(i)
1359 // return i
1360 // }
1361 InvalidDefer
1362
1363 // InvalidGo occurs when a go expression is not a function call, for example
1364 // if the expression is a type conversion.
1365 //
1366 // Example:
1367 // func f(i int) int {
1368 // go int32(i)
1369 // return i
1370 // }
1371 InvalidGo
1372
1373 // All codes below were added in Go 1.17.
1374
1375 /* decl */
1376
1377 // BadDecl occurs when a declaration has invalid syntax.
1378 BadDecl
1379
1380 // RepeatedDecl occurs when an identifier occurs more than once on the left
1381 // hand side of a short variable declaration.
1382 //
1383 // Example:
1384 // func _() {
1385 // x, y, y := 1, 2, 3
1386 // }
1387 RepeatedDecl
1388
1389 /* unsafe */
1390
1391 // InvalidUnsafeAdd occurs when unsafe.Add is called with a
1392 // length argument that is not of integer type.
1393 //
1394 // Example:
1395 // import "unsafe"
1396 //
1397 // var p unsafe.Pointer
1398 // var _ = unsafe.Add(p, float64(1))
1399 InvalidUnsafeAdd
1400
1401 // InvalidUnsafeSlice occurs when unsafe.Slice is called with a
1402 // pointer argument that is not of pointer type or a length argument
1403 // that is not of integer type, negative, or out of bounds.
1404 //
1405 // Example:
1406 // import "unsafe"
1407 //
1408 // var x int
1409 // var _ = unsafe.Slice(x, 1)
1410 //
1411 // Example:
1412 // import "unsafe"
1413 //
1414 // var x int
1415 // var _ = unsafe.Slice(&x, float64(1))
1416 //
1417 // Example:
1418 // import "unsafe"
1419 //
1420 // var x int
1421 // var _ = unsafe.Slice(&x, -1)
1422 //
1423 // Example:
1424 // import "unsafe"
1425 //
1426 // var x int
1427 // var _ = unsafe.Slice(&x, uint64(1) << 63)
1428 InvalidUnsafeSlice
1429
1430 // All codes below were added in Go 1.18.
1431
1432 /* features */
1433
1434 // UnsupportedFeature occurs when a language feature is used that is not
1435 // supported at this Go version.
1436 UnsupportedFeature
1437
1438 /* type params */
1439
1440 // NotAGenericType occurs when a non-generic type is used where a generic
1441 // type is expected: in type or function instantiation.
1442 //
1443 // Example:
1444 // type T int
1445 //
1446 // var _ T[int]
1447 NotAGenericType
1448
1449 // WrongTypeArgCount occurs when a type or function is instantiated with an
1450 // incorrect number of type arguments, including when a generic type or
1451 // function is used without instantiation.
1452 //
1453 // Errors involving failed type inference are assigned other error codes.
1454 //
1455 // Example:
1456 // type T[p any] int
1457 //
1458 // var _ T[int, string]
1459 //
1460 // Example:
1461 // func f[T any]() {}
1462 //
1463 // var x = f
1464 WrongTypeArgCount
1465
1466 // CannotInferTypeArgs occurs when type or function type argument inference
1467 // fails to infer all type arguments.
1468 //
1469 // Example:
1470 // func f[T any]() {}
1471 //
1472 // func _() {
1473 // f()
1474 // }
1475 //
1476 // Example:
1477 // type N[P, Q any] struct{}
1478 //
1479 // var _ N[int]
1480 CannotInferTypeArgs
1481
1482 // InvalidTypeArg occurs when a type argument does not satisfy its
1483 // corresponding type parameter constraints.
1484 //
1485 // Example:
1486 // type T[P ~int] struct{}
1487 //
1488 // var _ T[string]
1489 InvalidTypeArg // arguments? InferenceFailed
1490
1491 // InvalidInstanceCycle occurs when an invalid cycle is detected
1492 // within the instantiation graph.
1493 //
1494 // Example:
1495 // func f[T any]() { f[*T]() }
1496 InvalidInstanceCycle
1497
1498 // InvalidUnion occurs when an embedded union or approximation element is
1499 // not valid.
1500 //
1501 // Example:
1502 // type _ interface {
1503 // ~int | interface{ m() }
1504 // }
1505 InvalidUnion
1506
1507 // MisplacedConstraintIface occurs when a constraint-type interface is used
1508 // outside of constraint position.
1509 //
1510 // Example:
1511 // type I interface { ~int }
1512 //
1513 // var _ I
1514 MisplacedConstraintIface
1515
1516 // InvalidMethodTypeParams occurs when methods have type parameters.
1517 //
1518 // It cannot be encountered with an AST parsed using go/parser.
1519 InvalidMethodTypeParams
1520
1521 // MisplacedTypeParam occurs when a type parameter is used in a place where
1522 // it is not permitted.
1523 //
1524 // Example:
1525 // type T[P any] P
1526 //
1527 // Example:
1528 // type T[P any] struct{ *P }
1529 MisplacedTypeParam
1530
1531 // InvalidUnsafeSliceData occurs when unsafe.SliceData is called with
1532 // an argument that is not of slice type. It also occurs if it is used
1533 // in a package compiled for a language version before go1.20.
1534 //
1535 // Example:
1536 // import "unsafe"
1537 //
1538 // var x int
1539 // var _ = unsafe.SliceData(x)
1540 InvalidUnsafeSliceData
1541
1542 // InvalidUnsafeString occurs when unsafe.String is called with
1543 // a length argument that is not of integer type, negative, or
1544 // out of bounds. It also occurs if it is used in a package
1545 // compiled for a language version before go1.20.
1546 //
1547 // Example:
1548 // import "unsafe"
1549 //
1550 // var b [10]byte
1551 // var _ = unsafe.String(&b[0], -1)
1552 InvalidUnsafeString
1553
1554 // InvalidUnsafeStringData occurs if it is used in a package
1555 // compiled for a language version before go1.20.
1556 _ // not used anymore
1557
1558 )