Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/error.go

Documentation: runtime

     1  // Copyright 2010 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 runtime
     6  
     7  import "internal/bytealg"
     8  
     9  // The Error interface identifies a run time error.
    10  type Error interface {
    11  	error
    12  
    13  	// RuntimeError is a no-op function but
    14  	// serves to distinguish types that are run time
    15  	// errors from ordinary errors: a type is a
    16  	// run time error if it has a RuntimeError method.
    17  	RuntimeError()
    18  }
    19  
    20  // A TypeAssertionError explains a failed type assertion.
    21  type TypeAssertionError struct {
    22  	_interface    *_type
    23  	concrete      *_type
    24  	asserted      *_type
    25  	missingMethod string // one method needed by Interface, missing from Concrete
    26  }
    27  
    28  func (*TypeAssertionError) RuntimeError() {}
    29  
    30  func (e *TypeAssertionError) Error() string {
    31  	inter := "interface"
    32  	if e._interface != nil {
    33  		inter = e._interface.string()
    34  	}
    35  	as := e.asserted.string()
    36  	if e.concrete == nil {
    37  		return "interface conversion: " + inter + " is nil, not " + as
    38  	}
    39  	cs := e.concrete.string()
    40  	if e.missingMethod == "" {
    41  		msg := "interface conversion: " + inter + " is " + cs + ", not " + as
    42  		if cs == as {
    43  			// provide slightly clearer error message
    44  			if e.concrete.pkgpath() != e.asserted.pkgpath() {
    45  				msg += " (types from different packages)"
    46  			} else {
    47  				msg += " (types from different scopes)"
    48  			}
    49  		}
    50  		return msg
    51  	}
    52  	return "interface conversion: " + cs + " is not " + as +
    53  		": missing method " + e.missingMethod
    54  }
    55  
    56  //go:nosplit
    57  // itoa converts val to a decimal representation. The result is
    58  // written somewhere within buf and the location of the result is returned.
    59  // buf must be at least 20 bytes.
    60  func itoa(buf []byte, val uint64) []byte {
    61  	i := len(buf) - 1
    62  	for val >= 10 {
    63  		buf[i] = byte(val%10 + '0')
    64  		i--
    65  		val /= 10
    66  	}
    67  	buf[i] = byte(val + '0')
    68  	return buf[i:]
    69  }
    70  
    71  // An errorString represents a runtime error described by a single string.
    72  type errorString string
    73  
    74  func (e errorString) RuntimeError() {}
    75  
    76  func (e errorString) Error() string {
    77  	return "runtime error: " + string(e)
    78  }
    79  
    80  // plainError represents a runtime error described a string without
    81  // the prefix "runtime error: " after invoking errorString.Error().
    82  // See Issue #14965.
    83  type plainError string
    84  
    85  func (e plainError) RuntimeError() {}
    86  
    87  func (e plainError) Error() string {
    88  	return string(e)
    89  }
    90  
    91  // A boundsError represents an indexing or slicing operation gone wrong.
    92  type boundsError struct {
    93  	x int64
    94  	y int
    95  	// Values in an index or slice expression can be signed or unsigned.
    96  	// That means we'd need 65 bits to encode all possible indexes, from -2^63 to 2^64-1.
    97  	// Instead, we keep track of whether x should be interpreted as signed or unsigned.
    98  	// y is known to be nonnegative and to fit in an int.
    99  	signed bool
   100  	code   boundsErrorCode
   101  }
   102  
   103  type boundsErrorCode uint8
   104  
   105  const (
   106  	boundsIndex boundsErrorCode = iota // s[x], 0 <= x < len(s) failed
   107  
   108  	boundsSliceAlen // s[?:x], 0 <= x <= len(s) failed
   109  	boundsSliceAcap // s[?:x], 0 <= x <= cap(s) failed
   110  	boundsSliceB    // s[x:y], 0 <= x <= y failed (but boundsSliceA didn't happen)
   111  
   112  	boundsSlice3Alen // s[?:?:x], 0 <= x <= len(s) failed
   113  	boundsSlice3Acap // s[?:?:x], 0 <= x <= cap(s) failed
   114  	boundsSlice3B    // s[?:x:y], 0 <= x <= y failed (but boundsSlice3A didn't happen)
   115  	boundsSlice3C    // s[x:y:?], 0 <= x <= y failed (but boundsSlice3A/B didn't happen)
   116  
   117  	// Note: in the above, len(s) and cap(s) are stored in y
   118  )
   119  
   120  // boundsErrorFmts provide error text for various out-of-bounds panics.
   121  // Note: if you change these strings, you should adjust the size of the buffer
   122  // in boundsError.Error below as well.
   123  var boundsErrorFmts = [...]string{
   124  	boundsIndex:      "index out of range [%x] with length %y",
   125  	boundsSliceAlen:  "slice bounds out of range [:%x] with length %y",
   126  	boundsSliceAcap:  "slice bounds out of range [:%x] with capacity %y",
   127  	boundsSliceB:     "slice bounds out of range [%x:%y]",
   128  	boundsSlice3Alen: "slice bounds out of range [::%x] with length %y",
   129  	boundsSlice3Acap: "slice bounds out of range [::%x] with capacity %y",
   130  	boundsSlice3B:    "slice bounds out of range [:%x:%y]",
   131  	boundsSlice3C:    "slice bounds out of range [%x:%y:]",
   132  }
   133  
   134  // boundsNegErrorFmts are overriding formats if x is negative. In this case there's no need to report y.
   135  var boundsNegErrorFmts = [...]string{
   136  	boundsIndex:      "index out of range [%x]",
   137  	boundsSliceAlen:  "slice bounds out of range [:%x]",
   138  	boundsSliceAcap:  "slice bounds out of range [:%x]",
   139  	boundsSliceB:     "slice bounds out of range [%x:]",
   140  	boundsSlice3Alen: "slice bounds out of range [::%x]",
   141  	boundsSlice3Acap: "slice bounds out of range [::%x]",
   142  	boundsSlice3B:    "slice bounds out of range [:%x:]",
   143  	boundsSlice3C:    "slice bounds out of range [%x::]",
   144  }
   145  
   146  func (e boundsError) RuntimeError() {}
   147  
   148  func appendIntStr(b []byte, v int64, signed bool) []byte {
   149  	if signed && v < 0 {
   150  		b = append(b, '-')
   151  		v = -v
   152  	}
   153  	var buf [20]byte
   154  	b = append(b, itoa(buf[:], uint64(v))...)
   155  	return b
   156  }
   157  
   158  func (e boundsError) Error() string {
   159  	fmt := boundsErrorFmts[e.code]
   160  	if e.signed && e.x < 0 {
   161  		fmt = boundsNegErrorFmts[e.code]
   162  	}
   163  	// max message length is 99: "runtime error: slice bounds out of range [::%x] with capacity %y"
   164  	// x can be at most 20 characters. y can be at most 19.
   165  	b := make([]byte, 0, 100)
   166  	b = append(b, "runtime error: "...)
   167  	for i := 0; i < len(fmt); i++ {
   168  		c := fmt[i]
   169  		if c != '%' {
   170  			b = append(b, c)
   171  			continue
   172  		}
   173  		i++
   174  		switch fmt[i] {
   175  		case 'x':
   176  			b = appendIntStr(b, e.x, e.signed)
   177  		case 'y':
   178  			b = appendIntStr(b, int64(e.y), true)
   179  		}
   180  	}
   181  	return string(b)
   182  }
   183  
   184  type stringer interface {
   185  	String() string
   186  }
   187  
   188  // printany prints an argument passed to panic.
   189  // If panic is called with a value that has a String or Error method,
   190  // it has already been converted into a string by preprintpanics.
   191  func printany(i interface{}) {
   192  	switch v := i.(type) {
   193  	case nil:
   194  		print("nil")
   195  	case bool:
   196  		print(v)
   197  	case int:
   198  		print(v)
   199  	case int8:
   200  		print(v)
   201  	case int16:
   202  		print(v)
   203  	case int32:
   204  		print(v)
   205  	case int64:
   206  		print(v)
   207  	case uint:
   208  		print(v)
   209  	case uint8:
   210  		print(v)
   211  	case uint16:
   212  		print(v)
   213  	case uint32:
   214  		print(v)
   215  	case uint64:
   216  		print(v)
   217  	case uintptr:
   218  		print(v)
   219  	case float32:
   220  		print(v)
   221  	case float64:
   222  		print(v)
   223  	case complex64:
   224  		print(v)
   225  	case complex128:
   226  		print(v)
   227  	case string:
   228  		print(v)
   229  	default:
   230  		printanycustomtype(i)
   231  	}
   232  }
   233  
   234  func printanycustomtype(i interface{}) {
   235  	eface := efaceOf(&i)
   236  	typestring := eface._type.string()
   237  
   238  	switch eface._type.kind {
   239  	case kindString:
   240  		print(typestring, `("`, *(*string)(eface.data), `")`)
   241  	case kindBool:
   242  		print(typestring, "(", *(*bool)(eface.data), ")")
   243  	case kindInt:
   244  		print(typestring, "(", *(*int)(eface.data), ")")
   245  	case kindInt8:
   246  		print(typestring, "(", *(*int8)(eface.data), ")")
   247  	case kindInt16:
   248  		print(typestring, "(", *(*int16)(eface.data), ")")
   249  	case kindInt32:
   250  		print(typestring, "(", *(*int32)(eface.data), ")")
   251  	case kindInt64:
   252  		print(typestring, "(", *(*int64)(eface.data), ")")
   253  	case kindUint:
   254  		print(typestring, "(", *(*uint)(eface.data), ")")
   255  	case kindUint8:
   256  		print(typestring, "(", *(*uint8)(eface.data), ")")
   257  	case kindUint16:
   258  		print(typestring, "(", *(*uint16)(eface.data), ")")
   259  	case kindUint32:
   260  		print(typestring, "(", *(*uint32)(eface.data), ")")
   261  	case kindUint64:
   262  		print(typestring, "(", *(*uint64)(eface.data), ")")
   263  	case kindUintptr:
   264  		print(typestring, "(", *(*uintptr)(eface.data), ")")
   265  	case kindFloat32:
   266  		print(typestring, "(", *(*float32)(eface.data), ")")
   267  	case kindFloat64:
   268  		print(typestring, "(", *(*float64)(eface.data), ")")
   269  	case kindComplex64:
   270  		print(typestring, *(*complex64)(eface.data))
   271  	case kindComplex128:
   272  		print(typestring, *(*complex128)(eface.data))
   273  	default:
   274  		print("(", typestring, ") ", eface.data)
   275  	}
   276  }
   277  
   278  // panicwrap generates a panic for a call to a wrapped value method
   279  // with a nil pointer receiver.
   280  //
   281  // It is called from the generated wrapper code.
   282  func panicwrap() {
   283  	pc := getcallerpc()
   284  	name := funcname(findfunc(pc))
   285  	// name is something like "main.(*T).F".
   286  	// We want to extract pkg ("main"), typ ("T"), and meth ("F").
   287  	// Do it by finding the parens.
   288  	i := bytealg.IndexByteString(name, '(')
   289  	if i < 0 {
   290  		throw("panicwrap: no ( in " + name)
   291  	}
   292  	pkg := name[:i-1]
   293  	if i+2 >= len(name) || name[i-1:i+2] != ".(*" {
   294  		throw("panicwrap: unexpected string after package name: " + name)
   295  	}
   296  	name = name[i+2:]
   297  	i = bytealg.IndexByteString(name, ')')
   298  	if i < 0 {
   299  		throw("panicwrap: no ) in " + name)
   300  	}
   301  	if i+2 >= len(name) || name[i:i+2] != ")." {
   302  		throw("panicwrap: unexpected string after type name: " + name)
   303  	}
   304  	typ := name[:i]
   305  	meth := name[i+2:]
   306  	panic(plainError("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer"))
   307  }
   308  

View as plain text