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