Source file src/go/types/errorcodes.go

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types
     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 code values should not be changed: add new codes at the end.
    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  	_ errorCode = iota
    35  
    36  	// _Test is reserved for errors that only apply while in self-test mode.
    37  	_Test
    38  
    39  	// _BlankPkgName occurs when a package name is the blank identifier "_".
    40  	//
    41  	// Per the spec:
    42  	//  "The PackageName must not be the blank identifier."
    43  	_BlankPkgName
    44  
    45  	// _MismatchedPkgName occurs when a file's package name doesn't match the
    46  	// package name already established by other files.
    47  	_MismatchedPkgName
    48  
    49  	// _InvalidPkgUse occurs when a package identifier is used outside of a
    50  	// selector expression.
    51  	//
    52  	// Example:
    53  	//  import "fmt"
    54  	//
    55  	//  var _ = fmt
    56  	_InvalidPkgUse
    57  
    58  	// _BadImportPath occurs when an import path is not valid.
    59  	_BadImportPath
    60  
    61  	// _BrokenImport occurs when importing a package fails.
    62  	//
    63  	// Example:
    64  	//  import "amissingpackage"
    65  	_BrokenImport
    66  
    67  	// _ImportCRenamed occurs when the special import "C" is renamed. "C" is a
    68  	// pseudo-package, and must not be renamed.
    69  	//
    70  	// Example:
    71  	//  import _ "C"
    72  	_ImportCRenamed
    73  
    74  	// _UnusedImport occurs when an import is unused.
    75  	//
    76  	// Example:
    77  	//  import "fmt"
    78  	//
    79  	//  func main() {}
    80  	_UnusedImport
    81  
    82  	// _InvalidInitCycle occurs when an invalid cycle is detected within the
    83  	// initialization graph.
    84  	//
    85  	// Example:
    86  	//  var x int = f()
    87  	//
    88  	//  func f() int { return x }
    89  	_InvalidInitCycle
    90  
    91  	// _DuplicateDecl occurs when an identifier is declared multiple times.
    92  	//
    93  	// Example:
    94  	//  var x = 1
    95  	//  var x = 2
    96  	_DuplicateDecl
    97  
    98  	// _InvalidDeclCycle occurs when a declaration cycle is not valid.
    99  	//
   100  	// Example:
   101  	//  type S struct {
   102  	//  	S
   103  	//  }
   104  	//
   105  	_InvalidDeclCycle
   106  
   107  	// _InvalidTypeCycle occurs when a cycle in type definitions results in a
   108  	// type that is not well-defined.
   109  	//
   110  	// Example:
   111  	//  import "unsafe"
   112  	//
   113  	//  type T [unsafe.Sizeof(T{})]int
   114  	_InvalidTypeCycle
   115  
   116  	// _InvalidConstInit occurs when a const declaration has a non-constant
   117  	// initializer.
   118  	//
   119  	// Example:
   120  	//  var x int
   121  	//  const _ = x
   122  	_InvalidConstInit
   123  
   124  	// _InvalidConstVal occurs when a const value cannot be converted to its
   125  	// target type.
   126  	//
   127  	// TODO(findleyr): this error code and example are not very clear. Consider
   128  	// removing it.
   129  	//
   130  	// Example:
   131  	//  const _ = 1 << "hello"
   132  	_InvalidConstVal
   133  
   134  	// _InvalidConstType occurs when the underlying type in a const declaration
   135  	// is not a valid constant type.
   136  	//
   137  	// Example:
   138  	//  const c *int = 4
   139  	_InvalidConstType
   140  
   141  	// _UntypedNil occurs when the predeclared (untyped) value nil is used to
   142  	// initialize a variable declared without an explicit type.
   143  	//
   144  	// Example:
   145  	//  var x = nil
   146  	_UntypedNil
   147  
   148  	// _WrongAssignCount occurs when the number of values on the right-hand side
   149  	// of an assignment or initialization expression does not match the number
   150  	// of variables on the left-hand side.
   151  	//
   152  	// Example:
   153  	//  var x = 1, 2
   154  	_WrongAssignCount
   155  
   156  	// _UnassignableOperand occurs when the left-hand side of an assignment is
   157  	// not assignable.
   158  	//
   159  	// Example:
   160  	//  func f() {
   161  	//  	const c = 1
   162  	//  	c = 2
   163  	//  }
   164  	_UnassignableOperand
   165  
   166  	// _NoNewVar occurs when a short variable declaration (':=') does not declare
   167  	// new variables.
   168  	//
   169  	// Example:
   170  	//  func f() {
   171  	//  	x := 1
   172  	//  	x := 2
   173  	//  }
   174  	_NoNewVar
   175  
   176  	// _MultiValAssignOp occurs when an assignment operation (+=, *=, etc) does
   177  	// not have single-valued left-hand or right-hand side.
   178  	//
   179  	// Per the spec:
   180  	//  "In assignment operations, both the left- and right-hand expression lists
   181  	//  must contain exactly one single-valued expression"
   182  	//
   183  	// Example:
   184  	//  func f() int {
   185  	//  	x, y := 1, 2
   186  	//  	x, y += 1
   187  	//  	return x + y
   188  	//  }
   189  	_MultiValAssignOp
   190  
   191  	// _InvalidIfaceAssign occurs when a value of type T is used as an
   192  	// interface, but T does not implement a method of the expected interface.
   193  	//
   194  	// Example:
   195  	//  type I interface {
   196  	//  	f()
   197  	//  }
   198  	//
   199  	//  type T int
   200  	//
   201  	//  var x I = T(1)
   202  	_InvalidIfaceAssign
   203  
   204  	// _InvalidChanAssign occurs when a chan assignment is invalid.
   205  	//
   206  	// Per the spec, a value x is assignable to a channel type T if:
   207  	//  "x is a bidirectional channel value, T is a channel type, x's type V and
   208  	//  T have identical element types, and at least one of V or T is not a
   209  	//  defined type."
   210  	//
   211  	// Example:
   212  	//  type T1 chan int
   213  	//  type T2 chan int
   214  	//
   215  	//  var x T1
   216  	//  // Invalid assignment because both types are named
   217  	//  var _ T2 = x
   218  	_InvalidChanAssign
   219  
   220  	// _IncompatibleAssign occurs when the type of the right-hand side expression
   221  	// in an assignment cannot be assigned to the type of the variable being
   222  	// assigned.
   223  	//
   224  	// Example:
   225  	//  var x []int
   226  	//  var _ int = x
   227  	_IncompatibleAssign
   228  
   229  	// _UnaddressableFieldAssign occurs when trying to assign to a struct field
   230  	// in a map value.
   231  	//
   232  	// Example:
   233  	//  func f() {
   234  	//  	m := make(map[string]struct{i int})
   235  	//  	m["foo"].i = 42
   236  	//  }
   237  	_UnaddressableFieldAssign
   238  
   239  	// _NotAType occurs when the identifier used as the underlying type in a type
   240  	// declaration or the right-hand side of a type alias does not denote a type.
   241  	//
   242  	// Example:
   243  	//  var S = 2
   244  	//
   245  	//  type T S
   246  	_NotAType
   247  
   248  	// _InvalidArrayLen occurs when an array length is not a constant value.
   249  	//
   250  	// Example:
   251  	//  var n = 3
   252  	//  var _ = [n]int{}
   253  	_InvalidArrayLen
   254  
   255  	// _BlankIfaceMethod occurs when a method name is '_'.
   256  	//
   257  	// Per the spec:
   258  	//  "The name of each explicitly specified method must be unique and not
   259  	//  blank."
   260  	//
   261  	// Example:
   262  	//  type T interface {
   263  	//  	_(int)
   264  	//  }
   265  	_BlankIfaceMethod
   266  
   267  	// _IncomparableMapKey occurs when a map key type does not support the == and
   268  	// != operators.
   269  	//
   270  	// Per the spec:
   271  	//  "The comparison operators == and != must be fully defined for operands of
   272  	//  the key type; thus the key type must not be a function, map, or slice."
   273  	//
   274  	// Example:
   275  	//  var x map[T]int
   276  	//
   277  	//  type T []int
   278  	_IncomparableMapKey
   279  
   280  	// _InvalidIfaceEmbed occurs when a non-interface type is embedded in an
   281  	// interface (for go 1.17 or earlier).
   282  	_InvalidIfaceEmbed
   283  
   284  	// _InvalidPtrEmbed occurs when an embedded field is of the pointer form *T,
   285  	// and T itself is itself a pointer, an unsafe.Pointer, or an interface.
   286  	//
   287  	// Per the spec:
   288  	//  "An embedded field must be specified as a type name T or as a pointer to
   289  	//  a non-interface type name *T, and T itself may not be a pointer type."
   290  	//
   291  	// Example:
   292  	//  type T *int
   293  	//
   294  	//  type S struct {
   295  	//  	*T
   296  	//  }
   297  	_InvalidPtrEmbed
   298  
   299  	// _BadRecv occurs when a method declaration does not have exactly one
   300  	// receiver parameter.
   301  	//
   302  	// Example:
   303  	//  func () _() {}
   304  	_BadRecv
   305  
   306  	// _InvalidRecv occurs when a receiver type expression is not of the form T
   307  	// or *T, or T is a pointer type.
   308  	//
   309  	// Example:
   310  	//  type T struct {}
   311  	//
   312  	//  func (**T) m() {}
   313  	_InvalidRecv
   314  
   315  	// _DuplicateFieldAndMethod occurs when an identifier appears as both a field
   316  	// and method name.
   317  	//
   318  	// Example:
   319  	//  type T struct {
   320  	//  	m int
   321  	//  }
   322  	//
   323  	//  func (T) m() {}
   324  	_DuplicateFieldAndMethod
   325  
   326  	// _DuplicateMethod occurs when two methods on the same receiver type have
   327  	// the same name.
   328  	//
   329  	// Example:
   330  	//  type T struct {}
   331  	//  func (T) m() {}
   332  	//  func (T) m(i int) int { return i }
   333  	_DuplicateMethod
   334  
   335  	// _InvalidBlank occurs when a blank identifier is used as a value or type.
   336  	//
   337  	// Per the spec:
   338  	//  "The blank identifier may appear as an operand only on the left-hand side
   339  	//  of an assignment."
   340  	//
   341  	// Example:
   342  	//  var x = _
   343  	_InvalidBlank
   344  
   345  	// _InvalidIota occurs when the predeclared identifier iota is used outside
   346  	// of a constant declaration.
   347  	//
   348  	// Example:
   349  	//  var x = iota
   350  	_InvalidIota
   351  
   352  	// _MissingInitBody occurs when an init function is missing its body.
   353  	//
   354  	// Example:
   355  	//  func init()
   356  	_MissingInitBody
   357  
   358  	// _InvalidInitSig occurs when an init function declares parameters or
   359  	// results.
   360  	//
   361  	// Deprecated: no longer emitted by the type checker. _InvalidInitDecl is
   362  	// used instead.
   363  	_InvalidInitSig
   364  
   365  	// _InvalidInitDecl occurs when init is declared as anything other than a
   366  	// function.
   367  	//
   368  	// Example:
   369  	//  var init = 1
   370  	//
   371  	// Example:
   372  	//  func init() int { return 1 }
   373  	_InvalidInitDecl
   374  
   375  	// _InvalidMainDecl occurs when main is declared as anything other than a
   376  	// function, in a main package.
   377  	_InvalidMainDecl
   378  
   379  	// _TooManyValues occurs when a function returns too many values for the
   380  	// expression context in which it is used.
   381  	//
   382  	// Example:
   383  	//  func ReturnTwo() (int, int) {
   384  	//  	return 1, 2
   385  	//  }
   386  	//
   387  	//  var x = ReturnTwo()
   388  	_TooManyValues
   389  
   390  	// _NotAnExpr occurs when a type expression is used where a value expression
   391  	// is expected.
   392  	//
   393  	// Example:
   394  	//  type T struct {}
   395  	//
   396  	//  func f() {
   397  	//  	T
   398  	//  }
   399  	_NotAnExpr
   400  
   401  	// _TruncatedFloat occurs when a float constant is truncated to an integer
   402  	// value.
   403  	//
   404  	// Example:
   405  	//  var _ int = 98.6
   406  	_TruncatedFloat
   407  
   408  	// _NumericOverflow occurs when a numeric constant overflows its target type.
   409  	//
   410  	// Example:
   411  	//  var x int8 = 1000
   412  	_NumericOverflow
   413  
   414  	// _UndefinedOp occurs when an operator is not defined for the type(s) used
   415  	// in an operation.
   416  	//
   417  	// Example:
   418  	//  var c = "a" - "b"
   419  	_UndefinedOp
   420  
   421  	// _MismatchedTypes occurs when operand types are incompatible in a binary
   422  	// operation.
   423  	//
   424  	// Example:
   425  	//  var a = "hello"
   426  	//  var b = 1
   427  	//  var c = a - b
   428  	_MismatchedTypes
   429  
   430  	// _DivByZero occurs when a division operation is provable at compile
   431  	// time to be a division by zero.
   432  	//
   433  	// Example:
   434  	//  const divisor = 0
   435  	//  var x int = 1/divisor
   436  	_DivByZero
   437  
   438  	// _NonNumericIncDec occurs when an increment or decrement operator is
   439  	// applied to a non-numeric value.
   440  	//
   441  	// Example:
   442  	//  func f() {
   443  	//  	var c = "c"
   444  	//  	c++
   445  	//  }
   446  	_NonNumericIncDec
   447  
   448  	// _UnaddressableOperand occurs when the & operator is applied to an
   449  	// unaddressable expression.
   450  	//
   451  	// Example:
   452  	//  var x = &1
   453  	_UnaddressableOperand
   454  
   455  	// _InvalidIndirection occurs when a non-pointer value is indirected via the
   456  	// '*' operator.
   457  	//
   458  	// Example:
   459  	//  var x int
   460  	//  var y = *x
   461  	_InvalidIndirection
   462  
   463  	// _NonIndexableOperand occurs when an index operation is applied to a value
   464  	// that cannot be indexed.
   465  	//
   466  	// Example:
   467  	//  var x = 1
   468  	//  var y = x[1]
   469  	_NonIndexableOperand
   470  
   471  	// _InvalidIndex occurs when an index argument is not of integer type,
   472  	// negative, or out-of-bounds.
   473  	//
   474  	// Example:
   475  	//  var s = [...]int{1,2,3}
   476  	//  var x = s[5]
   477  	//
   478  	// Example:
   479  	//  var s = []int{1,2,3}
   480  	//  var _ = s[-1]
   481  	//
   482  	// Example:
   483  	//  var s = []int{1,2,3}
   484  	//  var i string
   485  	//  var _ = s[i]
   486  	_InvalidIndex
   487  
   488  	// _SwappedSliceIndices occurs when constant indices in a slice expression
   489  	// are decreasing in value.
   490  	//
   491  	// Example:
   492  	//  var _ = []int{1,2,3}[2:1]
   493  	_SwappedSliceIndices
   494  
   495  	// _NonSliceableOperand occurs when a slice operation is applied to a value
   496  	// whose type is not sliceable, or is unaddressable.
   497  	//
   498  	// Example:
   499  	//  var x = [...]int{1, 2, 3}[:1]
   500  	//
   501  	// Example:
   502  	//  var x = 1
   503  	//  var y = 1[:1]
   504  	_NonSliceableOperand
   505  
   506  	// _InvalidSliceExpr occurs when a three-index slice expression (a[x:y:z]) is
   507  	// applied to a string.
   508  	//
   509  	// Example:
   510  	//  var s = "hello"
   511  	//  var x = s[1:2:3]
   512  	_InvalidSliceExpr
   513  
   514  	// _InvalidShiftCount occurs when the right-hand side of a shift operation is
   515  	// either non-integer, negative, or too large.
   516  	//
   517  	// Example:
   518  	//  var (
   519  	//  	x string
   520  	//  	y int = 1 << x
   521  	//  )
   522  	_InvalidShiftCount
   523  
   524  	// _InvalidShiftOperand occurs when the shifted operand is not an integer.
   525  	//
   526  	// Example:
   527  	//  var s = "hello"
   528  	//  var x = s << 2
   529  	_InvalidShiftOperand
   530  
   531  	// _InvalidReceive occurs when there is a channel receive from a value that
   532  	// is either not a channel, or is a send-only channel.
   533  	//
   534  	// Example:
   535  	//  func f() {
   536  	//  	var x = 1
   537  	//  	<-x
   538  	//  }
   539  	_InvalidReceive
   540  
   541  	// _InvalidSend occurs when there is a channel send to a value that is not a
   542  	// channel, or is a receive-only channel.
   543  	//
   544  	// Example:
   545  	//  func f() {
   546  	//  	var x = 1
   547  	//  	x <- "hello!"
   548  	//  }
   549  	_InvalidSend
   550  
   551  	// _DuplicateLitKey occurs when an index is duplicated in a slice, array, or
   552  	// map literal.
   553  	//
   554  	// Example:
   555  	//  var _ = []int{0:1, 0:2}
   556  	//
   557  	// Example:
   558  	//  var _ = map[string]int{"a": 1, "a": 2}
   559  	_DuplicateLitKey
   560  
   561  	// _MissingLitKey occurs when a map literal is missing a key expression.
   562  	//
   563  	// Example:
   564  	//  var _ = map[string]int{1}
   565  	_MissingLitKey
   566  
   567  	// _InvalidLitIndex occurs when the key in a key-value element of a slice or
   568  	// array literal is not an integer constant.
   569  	//
   570  	// Example:
   571  	//  var i = 0
   572  	//  var x = []string{i: "world"}
   573  	_InvalidLitIndex
   574  
   575  	// _OversizeArrayLit occurs when an array literal exceeds its length.
   576  	//
   577  	// Example:
   578  	//  var _ = [2]int{1,2,3}
   579  	_OversizeArrayLit
   580  
   581  	// _MixedStructLit occurs when a struct literal contains a mix of positional
   582  	// and named elements.
   583  	//
   584  	// Example:
   585  	//  var _ = struct{i, j int}{i: 1, 2}
   586  	_MixedStructLit
   587  
   588  	// _InvalidStructLit occurs when a positional struct literal has an incorrect
   589  	// number of values.
   590  	//
   591  	// Example:
   592  	//  var _ = struct{i, j int}{1,2,3}
   593  	_InvalidStructLit
   594  
   595  	// _MissingLitField occurs when a struct literal refers to a field that does
   596  	// not exist on the struct type.
   597  	//
   598  	// Example:
   599  	//  var _ = struct{i int}{j: 2}
   600  	_MissingLitField
   601  
   602  	// _DuplicateLitField occurs when a struct literal contains duplicated
   603  	// fields.
   604  	//
   605  	// Example:
   606  	//  var _ = struct{i int}{i: 1, i: 2}
   607  	_DuplicateLitField
   608  
   609  	// _UnexportedLitField occurs when a positional struct literal implicitly
   610  	// assigns an unexported field of an imported type.
   611  	_UnexportedLitField
   612  
   613  	// _InvalidLitField occurs when a field name is not a valid identifier.
   614  	//
   615  	// Example:
   616  	//  var _ = struct{i int}{1: 1}
   617  	_InvalidLitField
   618  
   619  	// _UntypedLit occurs when a composite literal omits a required type
   620  	// identifier.
   621  	//
   622  	// Example:
   623  	//  type outer struct{
   624  	//  	inner struct { i int }
   625  	//  }
   626  	//
   627  	//  var _ = outer{inner: {1}}
   628  	_UntypedLit
   629  
   630  	// _InvalidLit occurs when a composite literal expression does not match its
   631  	// type.
   632  	//
   633  	// Example:
   634  	//  type P *struct{
   635  	//  	x int
   636  	//  }
   637  	//  var _ = P {}
   638  	_InvalidLit
   639  
   640  	// _AmbiguousSelector occurs when a selector is ambiguous.
   641  	//
   642  	// Example:
   643  	//  type E1 struct { i int }
   644  	//  type E2 struct { i int }
   645  	//  type T struct { E1; E2 }
   646  	//
   647  	//  var x T
   648  	//  var _ = x.i
   649  	_AmbiguousSelector
   650  
   651  	// _UndeclaredImportedName occurs when a package-qualified identifier is
   652  	// undeclared by the imported package.
   653  	//
   654  	// Example:
   655  	//  import "go/types"
   656  	//
   657  	//  var _ = types.NotAnActualIdentifier
   658  	_UndeclaredImportedName
   659  
   660  	// _UnexportedName occurs when a selector refers to an unexported identifier
   661  	// of an imported package.
   662  	//
   663  	// Example:
   664  	//  import "reflect"
   665  	//
   666  	//  type _ reflect.flag
   667  	_UnexportedName
   668  
   669  	// _UndeclaredName occurs when an identifier is not declared in the current
   670  	// scope.
   671  	//
   672  	// Example:
   673  	//  var x T
   674  	_UndeclaredName
   675  
   676  	// _MissingFieldOrMethod occurs when a selector references a field or method
   677  	// that does not exist.
   678  	//
   679  	// Example:
   680  	//  type T struct {}
   681  	//
   682  	//  var x = T{}.f
   683  	_MissingFieldOrMethod
   684  
   685  	// _BadDotDotDotSyntax occurs when a "..." occurs in a context where it is
   686  	// not valid.
   687  	//
   688  	// Example:
   689  	//  var _ = map[int][...]int{0: {}}
   690  	_BadDotDotDotSyntax
   691  
   692  	// _NonVariadicDotDotDot occurs when a "..." is used on the final argument to
   693  	// a non-variadic function.
   694  	//
   695  	// Example:
   696  	//  func printArgs(s []string) {
   697  	//  	for _, a := range s {
   698  	//  		println(a)
   699  	//  	}
   700  	//  }
   701  	//
   702  	//  func f() {
   703  	//  	s := []string{"a", "b", "c"}
   704  	//  	printArgs(s...)
   705  	//  }
   706  	_NonVariadicDotDotDot
   707  
   708  	// _MisplacedDotDotDot occurs when a "..." is used somewhere other than the
   709  	// final argument in a function declaration.
   710  	//
   711  	// Example:
   712  	// 	func f(...int, int)
   713  	_MisplacedDotDotDot
   714  
   715  	_ // _InvalidDotDotDotOperand was removed.
   716  
   717  	// _InvalidDotDotDot occurs when a "..." is used in a non-variadic built-in
   718  	// function.
   719  	//
   720  	// Example:
   721  	//  var s = []int{1, 2, 3}
   722  	//  var l = len(s...)
   723  	_InvalidDotDotDot
   724  
   725  	// _UncalledBuiltin occurs when a built-in function is used as a
   726  	// function-valued expression, instead of being called.
   727  	//
   728  	// Per the spec:
   729  	//  "The built-in functions do not have standard Go types, so they can only
   730  	//  appear in call expressions; they cannot be used as function values."
   731  	//
   732  	// Example:
   733  	//  var _ = copy
   734  	_UncalledBuiltin
   735  
   736  	// _InvalidAppend occurs when append is called with a first argument that is
   737  	// not a slice.
   738  	//
   739  	// Example:
   740  	//  var _ = append(1, 2)
   741  	_InvalidAppend
   742  
   743  	// _InvalidCap occurs when an argument to the cap built-in function is not of
   744  	// supported type.
   745  	//
   746  	// See https://golang.org/ref/spec#Length_and_capacity for information on
   747  	// which underlying types are supported as arguments to cap and len.
   748  	//
   749  	// Example:
   750  	//  var s = 2
   751  	//  var x = cap(s)
   752  	_InvalidCap
   753  
   754  	// _InvalidClose occurs when close(...) is called with an argument that is
   755  	// not of channel type, or that is a receive-only channel.
   756  	//
   757  	// Example:
   758  	//  func f() {
   759  	//  	var x int
   760  	//  	close(x)
   761  	//  }
   762  	_InvalidClose
   763  
   764  	// _InvalidCopy occurs when the arguments are not of slice type or do not
   765  	// have compatible type.
   766  	//
   767  	// See https://golang.org/ref/spec#Appending_and_copying_slices for more
   768  	// information on the type requirements for the copy built-in.
   769  	//
   770  	// Example:
   771  	//  func f() {
   772  	//  	var x []int
   773  	//  	y := []int64{1,2,3}
   774  	//  	copy(x, y)
   775  	//  }
   776  	_InvalidCopy
   777  
   778  	// _InvalidComplex occurs when the complex built-in function is called with
   779  	// arguments with incompatible types.
   780  	//
   781  	// Example:
   782  	//  var _ = complex(float32(1), float64(2))
   783  	_InvalidComplex
   784  
   785  	// _InvalidDelete occurs when the delete built-in function is called with a
   786  	// first argument that is not a map.
   787  	//
   788  	// Example:
   789  	//  func f() {
   790  	//  	m := "hello"
   791  	//  	delete(m, "e")
   792  	//  }
   793  	_InvalidDelete
   794  
   795  	// _InvalidImag occurs when the imag built-in function is called with an
   796  	// argument that does not have complex type.
   797  	//
   798  	// Example:
   799  	//  var _ = imag(int(1))
   800  	_InvalidImag
   801  
   802  	// _InvalidLen occurs when an argument to the len built-in function is not of
   803  	// supported type.
   804  	//
   805  	// See https://golang.org/ref/spec#Length_and_capacity for information on
   806  	// which underlying types are supported as arguments to cap and len.
   807  	//
   808  	// Example:
   809  	//  var s = 2
   810  	//  var x = len(s)
   811  	_InvalidLen
   812  
   813  	// _SwappedMakeArgs occurs when make is called with three arguments, and its
   814  	// length argument is larger than its capacity argument.
   815  	//
   816  	// Example:
   817  	//  var x = make([]int, 3, 2)
   818  	_SwappedMakeArgs
   819  
   820  	// _InvalidMake occurs when make is called with an unsupported type argument.
   821  	//
   822  	// See https://golang.org/ref/spec#Making_slices_maps_and_channels for
   823  	// information on the types that may be created using make.
   824  	//
   825  	// Example:
   826  	//  var x = make(int)
   827  	_InvalidMake
   828  
   829  	// _InvalidReal occurs when the real built-in function is called with an
   830  	// argument that does not have complex type.
   831  	//
   832  	// Example:
   833  	//  var _ = real(int(1))
   834  	_InvalidReal
   835  
   836  	// _InvalidAssert occurs when a type assertion is applied to a
   837  	// value that is not of interface type.
   838  	//
   839  	// Example:
   840  	//  var x = 1
   841  	//  var _ = x.(float64)
   842  	_InvalidAssert
   843  
   844  	// _ImpossibleAssert occurs for a type assertion x.(T) when the value x of
   845  	// interface cannot have dynamic type T, due to a missing or mismatching
   846  	// method on T.
   847  	//
   848  	// Example:
   849  	//  type T int
   850  	//
   851  	//  func (t *T) m() int { return int(*t) }
   852  	//
   853  	//  type I interface { m() int }
   854  	//
   855  	//  var x I
   856  	//  var _ = x.(T)
   857  	_ImpossibleAssert
   858  
   859  	// _InvalidConversion occurs when the argument type cannot be converted to the
   860  	// target.
   861  	//
   862  	// See https://golang.org/ref/spec#Conversions for the rules of
   863  	// convertibility.
   864  	//
   865  	// Example:
   866  	//  var x float64
   867  	//  var _ = string(x)
   868  	_InvalidConversion
   869  
   870  	// _InvalidUntypedConversion occurs when an there is no valid implicit
   871  	// conversion from an untyped value satisfying the type constraints of the
   872  	// context in which it is used.
   873  	//
   874  	// Example:
   875  	//  var _ = 1 + new(int)
   876  	_InvalidUntypedConversion
   877  
   878  	// _BadOffsetofSyntax occurs when unsafe.Offsetof is called with an argument
   879  	// that is not a selector expression.
   880  	//
   881  	// Example:
   882  	//  import "unsafe"
   883  	//
   884  	//  var x int
   885  	//  var _ = unsafe.Offsetof(x)
   886  	_BadOffsetofSyntax
   887  
   888  	// _InvalidOffsetof occurs when unsafe.Offsetof is called with a method
   889  	// selector, rather than a field selector, or when the field is embedded via
   890  	// a pointer.
   891  	//
   892  	// Per the spec:
   893  	//
   894  	//  "If f is an embedded field, it must be reachable without pointer
   895  	//  indirections through fields of the struct. "
   896  	//
   897  	// Example:
   898  	//  import "unsafe"
   899  	//
   900  	//  type T struct { f int }
   901  	//  type S struct { *T }
   902  	//  var s S
   903  	//  var _ = unsafe.Offsetof(s.f)
   904  	//
   905  	// Example:
   906  	//  import "unsafe"
   907  	//
   908  	//  type S struct{}
   909  	//
   910  	//  func (S) m() {}
   911  	//
   912  	//  var s S
   913  	//  var _ = unsafe.Offsetof(s.m)
   914  	_InvalidOffsetof
   915  
   916  	// _UnusedExpr occurs when a side-effect free expression is used as a
   917  	// statement. Such a statement has no effect.
   918  	//
   919  	// Example:
   920  	//  func f(i int) {
   921  	//  	i*i
   922  	//  }
   923  	_UnusedExpr
   924  
   925  	// _UnusedVar occurs when a variable is declared but unused.
   926  	//
   927  	// Example:
   928  	//  func f() {
   929  	//  	x := 1
   930  	//  }
   931  	_UnusedVar
   932  
   933  	// _MissingReturn occurs when a function with results is missing a return
   934  	// statement.
   935  	//
   936  	// Example:
   937  	//  func f() int {}
   938  	_MissingReturn
   939  
   940  	// _WrongResultCount occurs when a return statement returns an incorrect
   941  	// number of values.
   942  	//
   943  	// Example:
   944  	//  func ReturnOne() int {
   945  	//  	return 1, 2
   946  	//  }
   947  	_WrongResultCount
   948  
   949  	// _OutOfScopeResult occurs when the name of a value implicitly returned by
   950  	// an empty return statement is shadowed in a nested scope.
   951  	//
   952  	// Example:
   953  	//  func factor(n int) (i int) {
   954  	//  	for i := 2; i < n; i++ {
   955  	//  		if n%i == 0 {
   956  	//  			return
   957  	//  		}
   958  	//  	}
   959  	//  	return 0
   960  	//  }
   961  	_OutOfScopeResult
   962  
   963  	// _InvalidCond occurs when an if condition is not a boolean expression.
   964  	//
   965  	// Example:
   966  	//  func checkReturn(i int) {
   967  	//  	if i {
   968  	//  		panic("non-zero return")
   969  	//  	}
   970  	//  }
   971  	_InvalidCond
   972  
   973  	// _InvalidPostDecl occurs when there is a declaration in a for-loop post
   974  	// statement.
   975  	//
   976  	// Example:
   977  	//  func f() {
   978  	//  	for i := 0; i < 10; j := 0 {}
   979  	//  }
   980  	_InvalidPostDecl
   981  
   982  	_ // _InvalidChanRange was removed.
   983  
   984  	// _InvalidIterVar occurs when two iteration variables are used while ranging
   985  	// over a channel.
   986  	//
   987  	// Example:
   988  	//  func f(c chan int) {
   989  	//  	for k, v := range c {
   990  	//  		println(k, v)
   991  	//  	}
   992  	//  }
   993  	_InvalidIterVar
   994  
   995  	// _InvalidRangeExpr occurs when the type of a range expression is not array,
   996  	// slice, string, map, or channel.
   997  	//
   998  	// Example:
   999  	//  func f(i int) {
  1000  	//  	for j := range i {
  1001  	//  		println(j)
  1002  	//  	}
  1003  	//  }
  1004  	_InvalidRangeExpr
  1005  
  1006  	// _MisplacedBreak occurs when a break statement is not within a for, switch,
  1007  	// or select statement of the innermost function definition.
  1008  	//
  1009  	// Example:
  1010  	//  func f() {
  1011  	//  	break
  1012  	//  }
  1013  	_MisplacedBreak
  1014  
  1015  	// _MisplacedContinue occurs when a continue statement is not within a for
  1016  	// loop of the innermost function definition.
  1017  	//
  1018  	// Example:
  1019  	//  func sumeven(n int) int {
  1020  	//  	proceed := func() {
  1021  	//  		continue
  1022  	//  	}
  1023  	//  	sum := 0
  1024  	//  	for i := 1; i <= n; i++ {
  1025  	//  		if i % 2 != 0 {
  1026  	//  			proceed()
  1027  	//  		}
  1028  	//  		sum += i
  1029  	//  	}
  1030  	//  	return sum
  1031  	//  }
  1032  	_MisplacedContinue
  1033  
  1034  	// _MisplacedFallthrough occurs when a fallthrough statement is not within an
  1035  	// expression switch.
  1036  	//
  1037  	// Example:
  1038  	//  func typename(i interface{}) string {
  1039  	//  	switch i.(type) {
  1040  	//  	case int64:
  1041  	//  		fallthrough
  1042  	//  	case int:
  1043  	//  		return "int"
  1044  	//  	}
  1045  	//  	return "unsupported"
  1046  	//  }
  1047  	_MisplacedFallthrough
  1048  
  1049  	// _DuplicateCase occurs when a type or expression switch has duplicate
  1050  	// cases.
  1051  	//
  1052  	// Example:
  1053  	//  func printInt(i int) {
  1054  	//  	switch i {
  1055  	//  	case 1:
  1056  	//  		println("one")
  1057  	//  	case 1:
  1058  	//  		println("One")
  1059  	//  	}
  1060  	//  }
  1061  	_DuplicateCase
  1062  
  1063  	// _DuplicateDefault occurs when a type or expression switch has multiple
  1064  	// default clauses.
  1065  	//
  1066  	// Example:
  1067  	//  func printInt(i int) {
  1068  	//  	switch i {
  1069  	//  	case 1:
  1070  	//  		println("one")
  1071  	//  	default:
  1072  	//  		println("One")
  1073  	//  	default:
  1074  	//  		println("1")
  1075  	//  	}
  1076  	//  }
  1077  	_DuplicateDefault
  1078  
  1079  	// _BadTypeKeyword occurs when a .(type) expression is used anywhere other
  1080  	// than a type switch.
  1081  	//
  1082  	// Example:
  1083  	//  type I interface {
  1084  	//  	m()
  1085  	//  }
  1086  	//  var t I
  1087  	//  var _ = t.(type)
  1088  	_BadTypeKeyword
  1089  
  1090  	// _InvalidTypeSwitch occurs when .(type) is used on an expression that is
  1091  	// not of interface type.
  1092  	//
  1093  	// Example:
  1094  	//  func f(i int) {
  1095  	//  	switch x := i.(type) {}
  1096  	//  }
  1097  	_InvalidTypeSwitch
  1098  
  1099  	// _InvalidExprSwitch occurs when a switch expression is not comparable.
  1100  	//
  1101  	// Example:
  1102  	//  func _() {
  1103  	//  	var a struct{ _ func() }
  1104  	//  	switch a /* ERROR cannot switch on a */ {
  1105  	//  	}
  1106  	//  }
  1107  	_InvalidExprSwitch
  1108  
  1109  	// _InvalidSelectCase occurs when a select case is not a channel send or
  1110  	// receive.
  1111  	//
  1112  	// Example:
  1113  	//  func checkChan(c <-chan int) bool {
  1114  	//  	select {
  1115  	//  	case c:
  1116  	//  		return true
  1117  	//  	default:
  1118  	//  		return false
  1119  	//  	}
  1120  	//  }
  1121  	_InvalidSelectCase
  1122  
  1123  	// _UndeclaredLabel occurs when an undeclared label is jumped to.
  1124  	//
  1125  	// Example:
  1126  	//  func f() {
  1127  	//  	goto L
  1128  	//  }
  1129  	_UndeclaredLabel
  1130  
  1131  	// _DuplicateLabel occurs when a label is declared more than once.
  1132  	//
  1133  	// Example:
  1134  	//  func f() int {
  1135  	//  L:
  1136  	//  L:
  1137  	//  	return 1
  1138  	//  }
  1139  	_DuplicateLabel
  1140  
  1141  	// _MisplacedLabel occurs when a break or continue label is not on a for,
  1142  	// switch, or select statement.
  1143  	//
  1144  	// Example:
  1145  	//  func f() {
  1146  	//  L:
  1147  	//  	a := []int{1,2,3}
  1148  	//  	for _, e := range a {
  1149  	//  		if e > 10 {
  1150  	//  			break L
  1151  	//  		}
  1152  	//  		println(a)
  1153  	//  	}
  1154  	//  }
  1155  	_MisplacedLabel
  1156  
  1157  	// _UnusedLabel occurs when a label is declared but not used.
  1158  	//
  1159  	// Example:
  1160  	//  func f() {
  1161  	//  L:
  1162  	//  }
  1163  	_UnusedLabel
  1164  
  1165  	// _JumpOverDecl occurs when a label jumps over a variable declaration.
  1166  	//
  1167  	// Example:
  1168  	//  func f() int {
  1169  	//  	goto L
  1170  	//  	x := 2
  1171  	//  L:
  1172  	//  	x++
  1173  	//  	return x
  1174  	//  }
  1175  	_JumpOverDecl
  1176  
  1177  	// _JumpIntoBlock occurs when a forward jump goes to a label inside a nested
  1178  	// block.
  1179  	//
  1180  	// Example:
  1181  	//  func f(x int) {
  1182  	//  	goto L
  1183  	//  	if x > 0 {
  1184  	//  	L:
  1185  	//  		print("inside block")
  1186  	//  	}
  1187  	// }
  1188  	_JumpIntoBlock
  1189  
  1190  	// _InvalidMethodExpr occurs when a pointer method is called but the argument
  1191  	// is not addressable.
  1192  	//
  1193  	// Example:
  1194  	//  type T struct {}
  1195  	//
  1196  	//  func (*T) m() int { return 1 }
  1197  	//
  1198  	//  var _ = T.m(T{})
  1199  	_InvalidMethodExpr
  1200  
  1201  	// _WrongArgCount occurs when too few or too many arguments are passed by a
  1202  	// function call.
  1203  	//
  1204  	// Example:
  1205  	//  func f(i int) {}
  1206  	//  var x = f()
  1207  	_WrongArgCount
  1208  
  1209  	// _InvalidCall occurs when an expression is called that is not of function
  1210  	// type.
  1211  	//
  1212  	// Example:
  1213  	//  var x = "x"
  1214  	//  var y = x()
  1215  	_InvalidCall
  1216  
  1217  	// _UnusedResults occurs when a restricted expression-only built-in function
  1218  	// is suspended via go or defer. Such a suspension discards the results of
  1219  	// these side-effect free built-in functions, and therefore is ineffectual.
  1220  	//
  1221  	// Example:
  1222  	//  func f(a []int) int {
  1223  	//  	defer len(a)
  1224  	//  	return i
  1225  	//  }
  1226  	_UnusedResults
  1227  
  1228  	// _InvalidDefer occurs when a deferred expression is not a function call,
  1229  	// for example if the expression is a type conversion.
  1230  	//
  1231  	// Example:
  1232  	//  func f(i int) int {
  1233  	//  	defer int32(i)
  1234  	//  	return i
  1235  	//  }
  1236  	_InvalidDefer
  1237  
  1238  	// _InvalidGo occurs when a go expression is not a function call, for example
  1239  	// if the expression is a type conversion.
  1240  	//
  1241  	// Example:
  1242  	//  func f(i int) int {
  1243  	//  	go int32(i)
  1244  	//  	return i
  1245  	//  }
  1246  	_InvalidGo
  1247  
  1248  	// All codes below were added in Go 1.17.
  1249  
  1250  	// _BadDecl occurs when a declaration has invalid syntax.
  1251  	_BadDecl
  1252  
  1253  	// _RepeatedDecl occurs when an identifier occurs more than once on the left
  1254  	// hand side of a short variable declaration.
  1255  	//
  1256  	// Example:
  1257  	//  func _() {
  1258  	//  	x, y, y := 1, 2, 3
  1259  	//  }
  1260  	_RepeatedDecl
  1261  
  1262  	// _InvalidUnsafeAdd occurs when unsafe.Add is called with a
  1263  	// length argument that is not of integer type.
  1264  	//
  1265  	// Example:
  1266  	//  import "unsafe"
  1267  	//
  1268  	//  var p unsafe.Pointer
  1269  	//  var _ = unsafe.Add(p, float64(1))
  1270  	_InvalidUnsafeAdd
  1271  
  1272  	// _InvalidUnsafeSlice occurs when unsafe.Slice is called with a
  1273  	// pointer argument that is not of pointer type or a length argument
  1274  	// that is not of integer type, negative, or out of bounds.
  1275  	//
  1276  	// Example:
  1277  	//  import "unsafe"
  1278  	//
  1279  	//  var x int
  1280  	//  var _ = unsafe.Slice(x, 1)
  1281  	//
  1282  	// Example:
  1283  	//  import "unsafe"
  1284  	//
  1285  	//  var x int
  1286  	//  var _ = unsafe.Slice(&x, float64(1))
  1287  	//
  1288  	// Example:
  1289  	//  import "unsafe"
  1290  	//
  1291  	//  var x int
  1292  	//  var _ = unsafe.Slice(&x, -1)
  1293  	//
  1294  	// Example:
  1295  	//  import "unsafe"
  1296  	//
  1297  	//  var x int
  1298  	//  var _ = unsafe.Slice(&x, uint64(1) << 63)
  1299  	_InvalidUnsafeSlice
  1300  
  1301  	// All codes below were added in Go 1.18.
  1302  
  1303  	// _UnsupportedFeature occurs when a language feature is used that is not
  1304  	// supported at this Go version.
  1305  	_UnsupportedFeature
  1306  
  1307  	// _NotAGenericType occurs when a non-generic type is used where a generic
  1308  	// type is expected: in type or function instantiation.
  1309  	//
  1310  	// Example:
  1311  	//  type T int
  1312  	//
  1313  	//  var _ T[int]
  1314  	_NotAGenericType
  1315  
  1316  	// _WrongTypeArgCount occurs when a type or function is instantiated with an
  1317  	// incorrent number of type arguments, including when a generic type or
  1318  	// function is used without instantiation.
  1319  	//
  1320  	// Errors inolving failed type inference are assigned other error codes.
  1321  	//
  1322  	// Example:
  1323  	//  type T[p any] int
  1324  	//
  1325  	//  var _ T[int, string]
  1326  	//
  1327  	// Example:
  1328  	//  func f[T any]() {}
  1329  	//
  1330  	//  var x = f
  1331  	_WrongTypeArgCount
  1332  
  1333  	// _CannotInferTypeArgs occurs when type or function type argument inference
  1334  	// fails to infer all type arguments.
  1335  	//
  1336  	// Example:
  1337  	//  func f[T any]() {}
  1338  	//
  1339  	//  func _() {
  1340  	//  	f()
  1341  	//  }
  1342  	_CannotInferTypeArgs
  1343  
  1344  	// _InvalidTypeArg occurs when a type argument does not satisfy its
  1345  	// corresponding type parameter constraints.
  1346  	//
  1347  	// Example:
  1348  	//  type T[P ~int] struct{}
  1349  	//
  1350  	//  var _ T[string]
  1351  	_InvalidTypeArg // arguments? InferenceFailed
  1352  
  1353  	// _InvalidInstanceCycle occurs when an invalid cycle is detected
  1354  	// within the instantiation graph.
  1355  	//
  1356  	// Example:
  1357  	//  func f[T any]() { f[*T]() }
  1358  	_InvalidInstanceCycle
  1359  
  1360  	// _InvalidUnion occurs when an embedded union or approximation element is
  1361  	// not valid.
  1362  	//
  1363  	// Example:
  1364  	//  type _ interface {
  1365  	//   	~int | interface{ m() }
  1366  	//  }
  1367  	_InvalidUnion
  1368  
  1369  	// _MisplacedConstraintIface occurs when a constraint-type interface is used
  1370  	// outside of constraint position.
  1371  	//
  1372  	// Example:
  1373  	//   type I interface { ~int }
  1374  	//
  1375  	//   var _ I
  1376  	_MisplacedConstraintIface
  1377  
  1378  	// _InvalidMethodTypeParams occurs when methods have type parameters.
  1379  	//
  1380  	// It cannot be encountered with an AST parsed using go/parser.
  1381  	_InvalidMethodTypeParams
  1382  
  1383  	// _MisplacedTypeParam occurs when a type parameter is used in a place where
  1384  	// it is not permitted.
  1385  	//
  1386  	// Example:
  1387  	//  type T[P any] P
  1388  	//
  1389  	// Example:
  1390  	//  type T[P any] struct{ *P }
  1391  	_MisplacedTypeParam
  1392  )
  1393  

View as plain text