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.
9 // The Error interface identifies a run time error.
10 type Error
interface {
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.
20 // A TypeAssertionError explains a failed type assertion.
21 type TypeAssertionError
struct {
25 missingMethod
string // one method needed by Interface, missing from Concrete
28 func (*TypeAssertionError
) RuntimeError() {}
30 func (e
*TypeAssertionError
) Error() string {
32 if e
._interface
!= nil {
33 inter
= e
._interface
.string()
35 as
:= e
.asserted
.string()
36 if e
.concrete
== nil {
37 return "interface conversion: " + inter
+ " is nil, not " + as
39 cs
:= e
.concrete
.string()
40 if e
.missingMethod
== "" {
41 msg
:= "interface conversion: " + inter
+ " is " + cs
+ ", not " + as
43 // provide slightly clearer error message
44 if e
.concrete
.pkgpath() != e
.asserted
.pkgpath() {
45 msg
+= " (types from different packages)"
47 msg
+= " (types from different scopes)"
52 return "interface conversion: " + cs
+ " is not " + as
+
53 ": missing method " + e
.missingMethod
56 // Remove quoted strings from gccgo reflection strings.
57 func unquote(s
string) string {
60 for i
= 0; i
< ls
; i
++ {
69 r
:= make([]byte, len(s
))
71 for i
= 0; i
< ls
; i
++ {
83 // itoa converts val to a decimal representation. The result is
84 // written somewhere within buf and the location of the result is returned.
85 // buf must be at least 20 bytes.
86 func itoa(buf
[]byte, val
uint64) []byte {
89 buf
[i
] = byte(val%10
+ '0')
93 buf
[i
] = byte(val
+ '0')
97 // An errorString represents a runtime error described by a single string.
98 type errorString
string
100 func (e errorString
) RuntimeError() {}
102 func (e errorString
) Error() string {
103 return "runtime error: " + string(e
)
106 // An errorCString represents a runtime error described by a single C string.
107 // Not "type errorCString uintptr" because of http://golang.org/issue/7084.
108 type errorCString
struct{ cstr
uintptr }
110 func (e errorCString
) RuntimeError() {}
112 func (e errorCString
) Error() string {
113 return "runtime error: " + gostringnocopy((*byte)(unsafe
.Pointer(e
.cstr
)))
116 // For calling from C.
117 func NewErrorCString(s
uintptr, ret
*interface{}) {
118 *ret
= errorCString
{s
}
121 type errorAddressString
struct {
122 msg
string // error message
123 addr
uintptr // memory address where the error occurred
126 func (e errorAddressString
) RuntimeError() {}
128 func (e errorAddressString
) Error() string {
129 return "runtime error: " + e
.msg
132 // Addr returns the memory address where a fault occurred.
133 // The address provided is best-effort.
134 // The veracity of the result may depend on the platform.
135 // Errors providing this method will only be returned as
136 // a result of using runtime/debug.SetPanicOnFault.
137 func (e errorAddressString
) Addr() uintptr {
141 // plainError represents a runtime error described a string without
142 // the prefix "runtime error: " after invoking errorString.Error().
144 type plainError
string
146 func (e plainError
) RuntimeError() {}
148 func (e plainError
) Error() string {
152 // A boundsError represents an indexing or slicing operation gone wrong.
153 type boundsError
struct {
156 // Values in an index or slice expression can be signed or unsigned.
157 // That means we'd need 65 bits to encode all possible indexes, from -2^63 to 2^64-1.
158 // Instead, we keep track of whether x should be interpreted as signed or unsigned.
159 // y is known to be nonnegative and to fit in an int.
164 type boundsErrorCode
uint8
167 boundsIndex boundsErrorCode
= iota // s[x], 0 <= x < len(s) failed
169 boundsSliceAlen
// s[?:x], 0 <= x <= len(s) failed
170 boundsSliceAcap
// s[?:x], 0 <= x <= cap(s) failed
171 boundsSliceB
// s[x:y], 0 <= x <= y failed (but boundsSliceA didn't happen)
173 boundsSlice3Alen
// s[?:?:x], 0 <= x <= len(s) failed
174 boundsSlice3Acap
// s[?:?:x], 0 <= x <= cap(s) failed
175 boundsSlice3B
// s[?:x:y], 0 <= x <= y failed (but boundsSlice3A didn't happen)
176 boundsSlice3C
// s[x:y:?], 0 <= x <= y failed (but boundsSlice3A/B didn't happen)
178 boundsConvert
// (*[x]T)(s), 0 <= x <= len(s) failed
179 // Note: in the above, len(s) and cap(s) are stored in y
182 // boundsErrorFmts provide error text for various out-of-bounds panics.
183 // Note: if you change these strings, you should adjust the size of the buffer
184 // in boundsError.Error below as well.
185 var boundsErrorFmts
= [...]string{
186 boundsIndex
: "index out of range [%x] with length %y",
187 boundsSliceAlen
: "slice bounds out of range [:%x] with length %y",
188 boundsSliceAcap
: "slice bounds out of range [:%x] with capacity %y",
189 boundsSliceB
: "slice bounds out of range [%x:%y]",
190 boundsSlice3Alen
: "slice bounds out of range [::%x] with length %y",
191 boundsSlice3Acap
: "slice bounds out of range [::%x] with capacity %y",
192 boundsSlice3B
: "slice bounds out of range [:%x:%y]",
193 boundsSlice3C
: "slice bounds out of range [%x:%y:]",
194 boundsConvert
: "cannot convert slice with length %y to pointer to array with length %x",
197 // boundsNegErrorFmts are overriding formats if x is negative. In this case there's no need to report y.
198 var boundsNegErrorFmts
= [...]string{
199 boundsIndex
: "index out of range [%x]",
200 boundsSliceAlen
: "slice bounds out of range [:%x]",
201 boundsSliceAcap
: "slice bounds out of range [:%x]",
202 boundsSliceB
: "slice bounds out of range [%x:]",
203 boundsSlice3Alen
: "slice bounds out of range [::%x]",
204 boundsSlice3Acap
: "slice bounds out of range [::%x]",
205 boundsSlice3B
: "slice bounds out of range [:%x:]",
206 boundsSlice3C
: "slice bounds out of range [%x::]",
209 func (e boundsError
) RuntimeError() {}
211 func appendIntStr(b
[]byte, v
int64, signed
bool) []byte {
217 b
= append(b
, itoa(buf
[:], uint64(v
))...)
221 func (e boundsError
) Error() string {
222 fmt
:= boundsErrorFmts
[e
.code
]
223 if e
.signed
&& e
.x
< 0 {
224 fmt
= boundsNegErrorFmts
[e
.code
]
226 // max message length is 99: "runtime error: slice bounds out of range [::%x] with capacity %y"
227 // x can be at most 20 characters. y can be at most 19.
228 b
:= make([]byte, 0, 100)
229 b
= append(b
, "runtime error: "...)
230 for i
:= 0; i
< len(fmt
); i
++ {
239 b
= appendIntStr(b
, e
.x
, e
.signed
)
241 b
= appendIntStr(b
, int64(e
.y
), true)
247 type stringer
interface {
251 // printany prints an argument passed to panic.
252 // If panic is called with a value that has a String or Error method,
253 // it has already been converted into a string by preprintpanics.
254 func printany(i any
) {
255 switch v
:= i
.(type) {
293 printanycustomtype(i
)
297 func printanycustomtype(i any
) {
299 typestring
:= eface
._type
.string()
301 switch eface
._type
.kind
& ((1 << 5) - 1) {
303 print(typestring
, `("`, *(*string)(eface
.data
), `")`)
305 print(typestring
, "(", *(*bool)(eface
.data
), ")")
307 print(typestring
, "(", *(*int)(eface
.data
), ")")
309 print(typestring
, "(", *(*int8)(eface
.data
), ")")
311 print(typestring
, "(", *(*int16)(eface
.data
), ")")
313 print(typestring
, "(", *(*int32)(eface
.data
), ")")
315 print(typestring
, "(", *(*int64)(eface
.data
), ")")
317 print(typestring
, "(", *(*uint)(eface
.data
), ")")
319 print(typestring
, "(", *(*uint8)(eface
.data
), ")")
321 print(typestring
, "(", *(*uint16)(eface
.data
), ")")
323 print(typestring
, "(", *(*uint32)(eface
.data
), ")")
325 print(typestring
, "(", *(*uint64)(eface
.data
), ")")
327 print(typestring
, "(", *(*uintptr)(eface
.data
), ")")
329 print(typestring
, "(", *(*float32)(eface
.data
), ")")
331 print(typestring
, "(", *(*float64)(eface
.data
), ")")
333 print(typestring
, *(*complex64
)(eface
.data
))
335 print(typestring
, *(*complex128
)(eface
.data
))
337 print("(", typestring
, ") ", eface
.data
)