...

Source file src/internal/types/errors/codes.go

Documentation: internal/types/errors

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package errors
     6  
     7  //go:generate go run golang.org/x/tools/cmd/stringer@latest -type Code codes.go
     8  
     9  type Code int
    10  
    11  // This file defines the error codes that can be produced during type-checking.
    12  // Collectively, these codes provide an identifier that may be used to
    13  // implement special handling for certain types of errors.
    14  //
    15  // Error code values should not be changed: add new codes at the end.
    16  //
    17  // Error codes should be fine-grained enough that the exact nature of the error
    18  // can be easily determined, but coarse enough that they are not an
    19  // implementation detail of the type checking algorithm. As a rule-of-thumb,
    20  // errors should be considered equivalent if there is a theoretical refactoring
    21  // of the type checker in which they are emitted in exactly one place. For
    22  // example, the type checker emits different error messages for "too many
    23  // arguments" and "too few arguments", but one can imagine an alternative type
    24  // checker where this check instead just emits a single "wrong number of
    25  // arguments", so these errors should have the same code.
    26  //
    27  // Error code names should be as brief as possible while retaining accuracy and
    28  // distinctiveness. In most cases names should start with an adjective
    29  // describing the nature of the error (e.g. "invalid", "unused", "misplaced"),
    30  // and end with a noun identifying the relevant language object. For example,
    31  // "_DuplicateDecl" or "_InvalidSliceExpr". For brevity, naming follows the
    32  // convention that "bad" implies a problem with syntax, and "invalid" implies a
    33  // problem with types.
    34  
    35  const (
    36  	// InvalidSyntaxTree occurs if an invalid syntax tree is provided
    37  	// to the type checker. It should never happen.
    38  	InvalidSyntaxTree Code = -1
    39  )
    40  
    41  const (
    42  	// The zero Code value indicates an unset (invalid) error code.
    43  	_ Code = iota
    44  
    45  	// Test is reserved for errors that only apply while in self-test mode.
    46  	Test
    47  
    48  	// BlankPkgName occurs when a package name is the blank identifier "_".
    49  	//
    50  	// Per the spec:
    51  	//  "The PackageName must not be the blank identifier."
    52  	//
    53  	// Example:
    54  	//  package _
    55  	BlankPkgName
    56  
    57  	// MismatchedPkgName occurs when a file's package name doesn't match the
    58  	// package name already established by other files.
    59  	MismatchedPkgName
    60  
    61  	// InvalidPkgUse occurs when a package identifier is used outside of a
    62  	// selector expression.
    63  	//
    64  	// Example:
    65  	//  import "fmt"
    66  	//
    67  	//  var _ = fmt
    68  	InvalidPkgUse
    69  
    70  	// BadImportPath occurs when an import path is not valid.
    71  	BadImportPath
    72  
    73  	// BrokenImport occurs when importing a package fails.
    74  	//
    75  	// Example:
    76  	//  import "amissingpackage"
    77  	BrokenImport
    78  
    79  	// ImportCRenamed occurs when the special import "C" is renamed. "C" is a
    80  	// pseudo-package, and must not be renamed.
    81  	//
    82  	// Example:
    83  	//  import _ "C"
    84  	ImportCRenamed
    85  
    86  	// UnusedImport occurs when an import is unused.
    87  	//
    88  	// Example:
    89  	//  import "fmt"
    90  	//
    91  	//  func main() {}
    92  	UnusedImport
    93  
    94  	// InvalidInitCycle occurs when an invalid cycle is detected within the
    95  	// initialization graph.
    96  	//
    97  	// Example:
    98  	//  var x int = f()
    99  	//
   100  	//  func f() int { return x }
   101  	InvalidInitCycle
   102  
   103  	// DuplicateDecl occurs when an identifier is declared multiple times.
   104  	//
   105  	// Example:
   106  	//  var x = 1
   107  	//  var x = 2
   108  	DuplicateDecl
   109  
   110  	// InvalidDeclCycle occurs when a declaration cycle is not valid.
   111  	//
   112  	// Example:
   113  	//  type S struct {
   114  	//  	S
   115  	//  }
   116  	//
   117  	InvalidDeclCycle
   118  
   119  	// InvalidTypeCycle occurs when a cycle in type definitions results in a
   120  	// type that is not well-defined.
   121  	//
   122  	// Example:
   123  	//  import "unsafe"
   124  	//
   125  	//  type T [unsafe.Sizeof(T{})]int
   126  	InvalidTypeCycle
   127  
   128  	// InvalidConstInit occurs when a const declaration has a non-constant
   129  	// initializer.
   130  	//
   131  	// Example:
   132  	//  var x int
   133  	//  const _ = x
   134  	InvalidConstInit
   135  
   136  	// InvalidConstVal occurs when a const value cannot be converted to its
   137  	// target type.
   138  	//
   139  	// TODO(findleyr): this error code and example are not very clear. Consider
   140  	// removing it.
   141  	//
   142  	// Example:
   143  	//  const _ = 1 << "hello"
   144  	InvalidConstVal
   145  
   146  	// InvalidConstType occurs when the underlying type in a const declaration
   147  	// is not a valid constant type.
   148  	//
   149  	// Example:
   150  	//  const c *int = 4
   151  	InvalidConstType
   152  
   153  	// UntypedNilUse occurs when the predeclared (untyped) value nil is used to
   154  	// initialize a variable declared without an explicit type.
   155  	//
   156  	// Example:
   157  	//  var x = nil
   158  	UntypedNilUse
   159  
   160  	// WrongAssignCount occurs when the number of values on the right-hand side
   161  	// of an assignment or initialization expression does not match the number
   162  	// of variables on the left-hand side.
   163  	//
   164  	// Example:
   165  	//  var x = 1, 2
   166  	WrongAssignCount
   167  
   168  	// UnassignableOperand occurs when the left-hand side of an assignment is
   169  	// not assignable.
   170  	//
   171  	// Example:
   172  	//  func f() {
   173  	//  	const c = 1
   174  	//  	c = 2
   175  	//  }
   176  	UnassignableOperand
   177  
   178  	// NoNewVar occurs when a short variable declaration (':=') does not declare
   179  	// new variables.
   180  	//
   181  	// Example:
   182  	//  func f() {
   183  	//  	x := 1
   184  	//  	x := 2
   185  	//  }
   186  	NoNewVar
   187  
   188  	// MultiValAssignOp occurs when an assignment operation (+=, *=, etc) does
   189  	// not have single-valued left-hand or right-hand side.
   190  	//
   191  	// Per the spec:
   192  	//  "In assignment operations, both the left- and right-hand expression lists
   193  	//  must contain exactly one single-valued expression"
   194  	//
   195  	// Example:
   196  	//  func f() int {
   197  	//  	x, y := 1, 2
   198  	//  	x, y += 1
   199  	//  	return x + y
   200  	//  }
   201  	MultiValAssignOp
   202  
   203  	// InvalidIfaceAssign occurs when a value of type T is used as an
   204  	// interface, but T does not implement a method of the expected interface.
   205  	//
   206  	// Example:
   207  	//  type I interface {
   208  	//  	f()
   209  	//  }
   210  	//
   211  	//  type T int
   212  	//
   213  	//  var x I = T(1)
   214  	InvalidIfaceAssign
   215  
   216  	// InvalidChanAssign occurs when a chan assignment is invalid.
   217  	//
   218  	// Per the spec, a value x is assignable to a channel type T if:
   219  	//  "x is a bidirectional channel value, T is a channel type, x's type V and
   220  	//  T have identical element types, and at least one of V or T is not a
   221  	//  defined type."
   222  	//
   223  	// Example:
   224  	//  type T1 chan int
   225  	//  type T2 chan int
   226  	//
   227  	//  var x T1
   228  	//  // Invalid assignment because both types are named
   229  	//  var _ T2 = x
   230  	InvalidChanAssign
   231  
   232  	// IncompatibleAssign occurs when the type of the right-hand side expression
   233  	// in an assignment cannot be assigned to the type of the variable being
   234  	// assigned.
   235  	//
   236  	// Example:
   237  	//  var x []int
   238  	//  var _ int = x
   239  	IncompatibleAssign
   240  
   241  	// UnaddressableFieldAssign occurs when trying to assign to a struct field
   242  	// in a map value.
   243  	//
   244  	// Example:
   245  	//  func f() {
   246  	//  	m := make(map[string]struct{i int})
   247  	//  	m["foo"].i = 42
   248  	//  }
   249  	UnaddressableFieldAssign
   250  
   251  	// NotAType occurs when the identifier used as the underlying type in a type
   252  	// declaration or the right-hand side of a type alias does not denote a type.
   253  	//
   254  	// Example:
   255  	//  var S = 2
   256  	//
   257  	//  type T S
   258  	NotAType
   259  
   260  	// InvalidArrayLen occurs when an array length is not a constant value.
   261  	//
   262  	// Example:
   263  	//  var n = 3
   264  	//  var _ = [n]int{}
   265  	InvalidArrayLen
   266  
   267  	// BlankIfaceMethod occurs when a method name is '_'.
   268  	//
   269  	// Per the spec:
   270  	//  "The name of each explicitly specified method must be unique and not
   271  	//  blank."
   272  	//
   273  	// Example:
   274  	//  type T interface {
   275  	//  	_(int)
   276  	//  }
   277  	BlankIfaceMethod
   278  
   279  	// IncomparableMapKey occurs when a map key type does not support the == and
   280  	// != operators.
   281  	//
   282  	// Per the spec:
   283  	//  "The comparison operators == and != must be fully defined for operands of
   284  	//  the key type; thus the key type must not be a function, map, or slice."
   285  	//
   286  	// Example:
   287  	//  var x map[T]int
   288  	//
   289  	//  type T []int
   290  	IncomparableMapKey
   291  
   292  	// InvalidIfaceEmbed occurs when a non-interface type is embedded in an
   293  	// interface (for go 1.17 or earlier).
   294  	_ // not used anymore
   295  
   296  	// InvalidPtrEmbed occurs when an embedded field is of the pointer form *T,
   297  	// and T itself is itself a pointer, an unsafe.Pointer, or an interface.
   298  	//
   299  	// Per the spec:
   300  	//  "An embedded field must be specified as a type name T or as a pointer to
   301  	//  a non-interface type name *T, and T itself may not be a pointer type."
   302  	//
   303  	// Example:
   304  	//  type T *int
   305  	//
   306  	//  type S struct {
   307  	//  	*T
   308  	//  }
   309  	InvalidPtrEmbed
   310  
   311  	// BadRecv occurs when a method declaration does not have exactly one
   312  	// receiver parameter.
   313  	//
   314  	// Example:
   315  	//  func () _() {}
   316  	BadRecv
   317  
   318  	// InvalidRecv occurs when a receiver type expression is not of the form T
   319  	// or *T, or T is a pointer type.
   320  	//
   321  	// Example:
   322  	//  type T struct {}
   323  	//
   324  	//  func (**T) m() {}
   325  	InvalidRecv
   326  
   327  	// DuplicateFieldAndMethod occurs when an identifier appears as both a field
   328  	// and method name.
   329  	//
   330  	// Example:
   331  	//  type T struct {
   332  	//  	m int
   333  	//  }
   334  	//
   335  	//  func (T) m() {}
   336  	DuplicateFieldAndMethod
   337  
   338  	// DuplicateMethod occurs when two methods on the same receiver type have
   339  	// the same name.
   340  	//
   341  	// Example:
   342  	//  type T struct {}
   343  	//  func (T) m() {}
   344  	//  func (T) m(i int) int { return i }
   345  	DuplicateMethod
   346  
   347  	// InvalidBlank occurs when a blank identifier is used as a value or type.
   348  	//
   349  	// Per the spec:
   350  	//  "The blank identifier may appear as an operand only on the left-hand side
   351  	//  of an assignment."
   352  	//
   353  	// Example:
   354  	//  var x = _
   355  	InvalidBlank
   356  
   357  	// InvalidIota occurs when the predeclared identifier iota is used outside
   358  	// of a constant declaration.
   359  	//
   360  	// Example:
   361  	//  var x = iota
   362  	InvalidIota
   363  
   364  	// MissingInitBody occurs when an init function is missing its body.
   365  	//
   366  	// Example:
   367  	//  func init()
   368  	MissingInitBody
   369  
   370  	// InvalidInitSig occurs when an init function declares parameters or
   371  	// results.
   372  	//
   373  	// Deprecated: no longer emitted by the type checker. _InvalidInitDecl is
   374  	// used instead.
   375  	InvalidInitSig
   376  
   377  	// InvalidInitDecl occurs when init is declared as anything other than a
   378  	// function.
   379  	//
   380  	// Example:
   381  	//  var init = 1
   382  	//
   383  	// Example:
   384  	//  func init() int { return 1 }
   385  	InvalidInitDecl
   386  
   387  	// InvalidMainDecl occurs when main is declared as anything other than a
   388  	// function, in a main package.
   389  	InvalidMainDecl
   390  
   391  	// TooManyValues occurs when a function returns too many values for the
   392  	// expression context in which it is used.
   393  	//
   394  	// Example:
   395  	//  func ReturnTwo() (int, int) {
   396  	//  	return 1, 2
   397  	//  }
   398  	//
   399  	//  var x = ReturnTwo()
   400  	TooManyValues
   401  
   402  	// NotAnExpr occurs when a type expression is used where a value expression
   403  	// is expected.
   404  	//
   405  	// Example:
   406  	//  type T struct {}
   407  	//
   408  	//  func f() {
   409  	//  	T
   410  	//  }
   411  	NotAnExpr
   412  
   413  	// TruncatedFloat occurs when a float constant is truncated to an integer
   414  	// value.
   415  	//
   416  	// Example:
   417  	//  var _ int = 98.6
   418  	TruncatedFloat
   419  
   420  	// NumericOverflow occurs when a numeric constant overflows its target type.
   421  	//
   422  	// Example:
   423  	//  var x int8 = 1000
   424  	NumericOverflow
   425  
   426  	// UndefinedOp occurs when an operator is not defined for the type(s) used
   427  	// in an operation.
   428  	//
   429  	// Example:
   430  	//  var c = "a" - "b"
   431  	UndefinedOp
   432  
   433  	// MismatchedTypes occurs when operand types are incompatible in a binary
   434  	// operation.
   435  	//
   436  	// Example:
   437  	//  var a = "hello"
   438  	//  var b = 1
   439  	//  var c = a - b
   440  	MismatchedTypes
   441  
   442  	// DivByZero occurs when a division operation is provable at compile
   443  	// time to be a division by zero.
   444  	//
   445  	// Example:
   446  	//  const divisor = 0
   447  	//  var x int = 1/divisor
   448  	DivByZero
   449  
   450  	// NonNumericIncDec occurs when an increment or decrement operator is
   451  	// applied to a non-numeric value.
   452  	//
   453  	// Example:
   454  	//  func f() {
   455  	//  	var c = "c"
   456  	//  	c++
   457  	//  }
   458  	NonNumericIncDec
   459  
   460  	// UnaddressableOperand occurs when the & operator is applied to an
   461  	// unaddressable expression.
   462  	//
   463  	// Example:
   464  	//  var x = &1
   465  	UnaddressableOperand
   466  
   467  	// InvalidIndirection occurs when a non-pointer value is indirected via the
   468  	// '*' operator.
   469  	//
   470  	// Example:
   471  	//  var x int
   472  	//  var y = *x
   473  	InvalidIndirection
   474  
   475  	// NonIndexableOperand occurs when an index operation is applied to a value
   476  	// that cannot be indexed.
   477  	//
   478  	// Example:
   479  	//  var x = 1
   480  	//  var y = x[1]
   481  	NonIndexableOperand
   482  
   483  	// InvalidIndex occurs when an index argument is not of integer type,
   484  	// negative, or out-of-bounds.
   485  	//
   486  	// Example:
   487  	//  var s = [...]int{1,2,3}
   488  	//  var x = s[5]
   489  	//
   490  	// Example:
   491  	//  var s = []int{1,2,3}
   492  	//  var _ = s[-1]
   493  	//
   494  	// Example:
   495  	//  var s = []int{1,2,3}
   496  	//  var i string
   497  	//  var _ = s[i]
   498  	InvalidIndex
   499  
   500  	// SwappedSliceIndices occurs when constant indices in a slice expression
   501  	// are decreasing in value.
   502  	//
   503  	// Example:
   504  	//  var _ = []int{1,2,3}[2:1]
   505  	SwappedSliceIndices
   506  
   507  	// NonSliceableOperand occurs when a slice operation is applied to a value
   508  	// whose type is not sliceable, or is unaddressable.
   509  	//
   510  	// Example:
   511  	//  var x = [...]int{1, 2, 3}[:1]
   512  	//
   513  	// Example:
   514  	//  var x = 1
   515  	//  var y = 1[:1]
   516  	NonSliceableOperand
   517  
   518  	// InvalidSliceExpr occurs when a three-index slice expression (a[x:y:z]) is
   519  	// applied to a string.
   520  	//
   521  	// Example:
   522  	//  var s = "hello"
   523  	//  var x = s[1:2:3]
   524  	InvalidSliceExpr
   525  
   526  	// InvalidShiftCount occurs when the right-hand side of a shift operation is
   527  	// either non-integer, negative, or too large.
   528  	//
   529  	// Example:
   530  	//  var (
   531  	//  	x string
   532  	//  	y int = 1 << x
   533  	//  )
   534  	InvalidShiftCount
   535  
   536  	// InvalidShiftOperand occurs when the shifted operand is not an integer.
   537  	//
   538  	// Example:
   539  	//  var s = "hello"
   540  	//  var x = s << 2
   541  	InvalidShiftOperand
   542  
   543  	// InvalidReceive occurs when there is a channel receive from a value that
   544  	// is either not a channel, or is a send-only channel.
   545  	//
   546  	// Example:
   547  	//  func f() {
   548  	//  	var x = 1
   549  	//  	<-x
   550  	//  }
   551  	InvalidReceive
   552  
   553  	// InvalidSend occurs when there is a channel send to a value that is not a
   554  	// channel, or is a receive-only channel.
   555  	//
   556  	// Example:
   557  	//  func f() {
   558  	//  	var x = 1
   559  	//  	x <- "hello!"
   560  	//  }
   561  	InvalidSend
   562  
   563  	// DuplicateLitKey occurs when an index is duplicated in a slice, array, or
   564  	// map literal.
   565  	//
   566  	// Example:
   567  	//  var _ = []int{0:1, 0:2}
   568  	//
   569  	// Example:
   570  	//  var _ = map[string]int{"a": 1, "a": 2}
   571  	DuplicateLitKey
   572  
   573  	// MissingLitKey occurs when a map literal is missing a key expression.
   574  	//
   575  	// Example:
   576  	//  var _ = map[string]int{1}
   577  	MissingLitKey
   578  
   579  	// InvalidLitIndex occurs when the key in a key-value element of a slice or
   580  	// array literal is not an integer constant.
   581  	//
   582  	// Example:
   583  	//  var i = 0
   584  	//  var x = []string{i: "world"}
   585  	InvalidLitIndex
   586  
   587  	// OversizeArrayLit occurs when an array literal exceeds its length.
   588  	//
   589  	// Example:
   590  	//  var _ = [2]int{1,2,3}
   591  	OversizeArrayLit
   592  
   593  	// MixedStructLit occurs when a struct literal contains a mix of positional
   594  	// and named elements.
   595  	//
   596  	// Example:
   597  	//  var _ = struct{i, j int}{i: 1, 2}
   598  	MixedStructLit
   599  
   600  	// InvalidStructLit occurs when a positional struct literal has an incorrect
   601  	// number of values.
   602  	//
   603  	// Example:
   604  	//  var _ = struct{i, j int}{1,2,3}
   605  	InvalidStructLit
   606  
   607  	// MissingLitField occurs when a struct literal refers to a field that does
   608  	// not exist on the struct type.
   609  	//
   610  	// Example:
   611  	//  var _ = struct{i int}{j: 2}
   612  	MissingLitField
   613  
   614  	// DuplicateLitField occurs when a struct literal contains duplicated
   615  	// fields.
   616  	//
   617  	// Example:
   618  	//  var _ = struct{i int}{i: 1, i: 2}
   619  	DuplicateLitField
   620  
   621  	// UnexportedLitField occurs when a positional struct literal implicitly
   622  	// assigns an unexported field of an imported type.
   623  	UnexportedLitField
   624  
   625  	// InvalidLitField occurs when a field name is not a valid identifier.
   626  	//
   627  	// Example:
   628  	//  var _ = struct{i int}{1: 1}
   629  	InvalidLitField
   630  
   631  	// UntypedLit occurs when a composite literal omits a required type
   632  	// identifier.
   633  	//
   634  	// Example:
   635  	//  type outer struct{
   636  	//  	inner struct { i int }
   637  	//  }
   638  	//
   639  	//  var _ = outer{inner: {1}}
   640  	UntypedLit
   641  
   642  	// InvalidLit occurs when a composite literal expression does not match its
   643  	// type.
   644  	//
   645  	// Example:
   646  	//  type P *struct{
   647  	//  	x int
   648  	//  }
   649  	//  var _ = P {}
   650  	InvalidLit
   651  
   652  	// AmbiguousSelector occurs when a selector is ambiguous.
   653  	//
   654  	// Example:
   655  	//  type E1 struct { i int }
   656  	//  type E2 struct { i int }
   657  	//  type T struct { E1; E2 }
   658  	//
   659  	//  var x T
   660  	//  var _ = x.i
   661  	AmbiguousSelector
   662  
   663  	// UndeclaredImportedName occurs when a package-qualified identifier is
   664  	// undeclared by the imported package.
   665  	//
   666  	// Example:
   667  	//  import "go/types"
   668  	//
   669  	//  var _ = types.NotAnActualIdentifier
   670  	UndeclaredImportedName
   671  
   672  	// UnexportedName occurs when a selector refers to an unexported identifier
   673  	// of an imported package.
   674  	//
   675  	// Example:
   676  	//  import "reflect"
   677  	//
   678  	//  type _ reflect.flag
   679  	UnexportedName
   680  
   681  	// UndeclaredName occurs when an identifier is not declared in the current
   682  	// scope.
   683  	//
   684  	// Example:
   685  	//  var x T
   686  	UndeclaredName
   687  
   688  	// MissingFieldOrMethod occurs when a selector references a field or method
   689  	// that does not exist.
   690  	//
   691  	// Example:
   692  	//  type T struct {}
   693  	//
   694  	//  var x = T{}.f
   695  	MissingFieldOrMethod
   696  
   697  	// BadDotDotDotSyntax occurs when a "..." occurs in a context where it is
   698  	// not valid.
   699  	//
   700  	// Example:
   701  	//  var _ = map[int][...]int{0: {}}
   702  	BadDotDotDotSyntax
   703  
   704  	// NonVariadicDotDotDot occurs when a "..." is used on the final argument to
   705  	// a non-variadic function.
   706  	//
   707  	// Example:
   708  	//  func printArgs(s []string) {
   709  	//  	for _, a := range s {
   710  	//  		println(a)
   711  	//  	}
   712  	//  }
   713  	//
   714  	//  func f() {
   715  	//  	s := []string{"a", "b", "c"}
   716  	//  	printArgs(s...)
   717  	//  }
   718  	NonVariadicDotDotDot
   719  
   720  	// MisplacedDotDotDot occurs when a "..." is used somewhere other than the
   721  	// final argument in a function declaration.
   722  	//
   723  	// Example:
   724  	// 	func f(...int, int)
   725  	MisplacedDotDotDot
   726  
   727  	_ // InvalidDotDotDotOperand was removed.
   728  
   729  	// InvalidDotDotDot occurs when a "..." is used in a non-variadic built-in
   730  	// function.
   731  	//
   732  	// Example:
   733  	//  var s = []int{1, 2, 3}
   734  	//  var l = len(s...)
   735  	InvalidDotDotDot
   736  
   737  	// UncalledBuiltin occurs when a built-in function is used as a
   738  	// function-valued expression, instead of being called.
   739  	//
   740  	// Per the spec:
   741  	//  "The built-in functions do not have standard Go types, so they can only
   742  	//  appear in call expressions; they cannot be used as function values."
   743  	//
   744  	// Example:
   745  	//  var _ = copy
   746  	UncalledBuiltin
   747  
   748  	// InvalidAppend occurs when append is called with a first argument that is
   749  	// not a slice.
   750  	//
   751  	// Example:
   752  	//  var _ = append(1, 2)
   753  	InvalidAppend
   754  
   755  	// InvalidCap occurs when an argument to the cap built-in function is not of
   756  	// supported type.
   757  	//
   758  	// See https://golang.org/ref/spec#Length_and_capacity for information on
   759  	// which underlying types are supported as arguments to cap and len.
   760  	//
   761  	// Example:
   762  	//  var s = 2
   763  	//  var x = cap(s)
   764  	InvalidCap
   765  
   766  	// InvalidClose occurs when close(...) is called with an argument that is
   767  	// not of channel type, or that is a receive-only channel.
   768  	//
   769  	// Example:
   770  	//  func f() {
   771  	//  	var x int
   772  	//  	close(x)
   773  	//  }
   774  	InvalidClose
   775  
   776  	// InvalidCopy occurs when the arguments are not of slice type or do not
   777  	// have compatible type.
   778  	//
   779  	// See https://golang.org/ref/spec#Appending_and_copying_slices for more
   780  	// information on the type requirements for the copy built-in.
   781  	//
   782  	// Example:
   783  	//  func f() {
   784  	//  	var x []int
   785  	//  	y := []int64{1,2,3}
   786  	//  	copy(x, y)
   787  	//  }
   788  	InvalidCopy
   789  
   790  	// InvalidComplex occurs when the complex built-in function is called with
   791  	// arguments with incompatible types.
   792  	//
   793  	// Example:
   794  	//  var _ = complex(float32(1), float64(2))
   795  	InvalidComplex
   796  
   797  	// InvalidDelete occurs when the delete built-in function is called with a
   798  	// first argument that is not a map.
   799  	//
   800  	// Example:
   801  	//  func f() {
   802  	//  	m := "hello"
   803  	//  	delete(m, "e")
   804  	//  }
   805  	InvalidDelete
   806  
   807  	// InvalidImag occurs when the imag built-in function is called with an
   808  	// argument that does not have complex type.
   809  	//
   810  	// Example:
   811  	//  var _ = imag(int(1))
   812  	InvalidImag
   813  
   814  	// InvalidLen occurs when an argument to the len built-in function is not of
   815  	// supported type.
   816  	//
   817  	// See https://golang.org/ref/spec#Length_and_capacity for information on
   818  	// which underlying types are supported as arguments to cap and len.
   819  	//
   820  	// Example:
   821  	//  var s = 2
   822  	//  var x = len(s)
   823  	InvalidLen
   824  
   825  	// SwappedMakeArgs occurs when make is called with three arguments, and its
   826  	// length argument is larger than its capacity argument.
   827  	//
   828  	// Example:
   829  	//  var x = make([]int, 3, 2)
   830  	SwappedMakeArgs
   831  
   832  	// InvalidMake occurs when make is called with an unsupported type argument.
   833  	//
   834  	// See https://golang.org/ref/spec#Making_slices_maps_and_channels for
   835  	// information on the types that may be created using make.
   836  	//
   837  	// Example:
   838  	//  var x = make(int)
   839  	InvalidMake
   840  
   841  	// InvalidReal occurs when the real built-in function is called with an
   842  	// argument that does not have complex type.
   843  	//
   844  	// Example:
   845  	//  var _ = real(int(1))
   846  	InvalidReal
   847  
   848  	// InvalidAssert occurs when a type assertion is applied to a
   849  	// value that is not of interface type.
   850  	//
   851  	// Example:
   852  	//  var x = 1
   853  	//  var _ = x.(float64)
   854  	InvalidAssert
   855  
   856  	// ImpossibleAssert occurs for a type assertion x.(T) when the value x of
   857  	// interface cannot have dynamic type T, due to a missing or mismatching
   858  	// method on T.
   859  	//
   860  	// Example:
   861  	//  type T int
   862  	//
   863  	//  func (t *T) m() int { return int(*t) }
   864  	//
   865  	//  type I interface { m() int }
   866  	//
   867  	//  var x I
   868  	//  var _ = x.(T)
   869  	ImpossibleAssert
   870  
   871  	// InvalidConversion occurs when the argument type cannot be converted to the
   872  	// target.
   873  	//
   874  	// See https://golang.org/ref/spec#Conversions for the rules of
   875  	// convertibility.
   876  	//
   877  	// Example:
   878  	//  var x float64
   879  	//  var _ = string(x)
   880  	InvalidConversion
   881  
   882  	// InvalidUntypedConversion occurs when there is no valid implicit
   883  	// conversion from an untyped value satisfying the type constraints of the
   884  	// context in which it is used.
   885  	//
   886  	// Example:
   887  	//  var _ = 1 + []int{}
   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  

View as plain text