Skip 30_threads/future/members/poll.cc on hppa*-*-linux*
[official-gcc.git] / libgo / go / runtime / error.go
blobc7e2385a43b69dbb21cb7bc0c4e3de6446610639
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.
5 package runtime
7 import "unsafe"
9 // The Error interface identifies a run time error.
10 type Error interface {
11 error
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()
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
28 func (*TypeAssertionError) RuntimeError() {}
30 func (e *TypeAssertionError) Error() string {
31 inter := "interface"
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
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)"
50 return msg
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 {
58 ls := len(s)
59 var i int
60 for i = 0; i < ls; i++ {
61 if s[i] == '\t' {
62 break
65 if i == ls {
66 return s
68 var q bool
69 r := make([]byte, len(s))
70 j := 0
71 for i = 0; i < ls; i++ {
72 if s[i] == '\t' {
73 q = !q
74 } else if !q {
75 r[j] = s[i]
76 j++
79 return string(r[:j])
82 //go:nosplit
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 {
87 i := len(buf) - 1
88 for val >= 10 {
89 buf[i] = byte(val%10 + '0')
90 i--
91 val /= 10
93 buf[i] = byte(val + '0')
94 return buf[i:]
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 {
138 return e.addr
141 // plainError represents a runtime error described a string without
142 // the prefix "runtime error: " after invoking errorString.Error().
143 // See Issue #14965.
144 type plainError string
146 func (e plainError) RuntimeError() {}
148 func (e plainError) Error() string {
149 return string(e)
152 // A boundsError represents an indexing or slicing operation gone wrong.
153 type boundsError struct {
154 x int64
155 y int
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.
160 signed bool
161 code boundsErrorCode
164 type boundsErrorCode uint8
166 const (
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 {
212 if signed && v < 0 {
213 b = append(b, '-')
214 v = -v
216 var buf [20]byte
217 b = append(b, itoa(buf[:], uint64(v))...)
218 return b
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++ {
231 c := fmt[i]
232 if c != '%' {
233 b = append(b, c)
234 continue
237 switch fmt[i] {
238 case 'x':
239 b = appendIntStr(b, e.x, e.signed)
240 case 'y':
241 b = appendIntStr(b, int64(e.y), true)
244 return string(b)
247 type stringer interface {
248 String() string
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) {
256 case nil:
257 print("nil")
258 case bool:
259 print(v)
260 case int:
261 print(v)
262 case int8:
263 print(v)
264 case int16:
265 print(v)
266 case int32:
267 print(v)
268 case int64:
269 print(v)
270 case uint:
271 print(v)
272 case uint8:
273 print(v)
274 case uint16:
275 print(v)
276 case uint32:
277 print(v)
278 case uint64:
279 print(v)
280 case uintptr:
281 print(v)
282 case float32:
283 print(v)
284 case float64:
285 print(v)
286 case complex64:
287 print(v)
288 case complex128:
289 print(v)
290 case string:
291 print(v)
292 default:
293 printanycustomtype(i)
297 func printanycustomtype(i any) {
298 eface := efaceOf(&i)
299 typestring := eface._type.string()
301 switch eface._type.kind & ((1 << 5) - 1) {
302 case kindString:
303 print(typestring, `("`, *(*string)(eface.data), `")`)
304 case kindBool:
305 print(typestring, "(", *(*bool)(eface.data), ")")
306 case kindInt:
307 print(typestring, "(", *(*int)(eface.data), ")")
308 case kindInt8:
309 print(typestring, "(", *(*int8)(eface.data), ")")
310 case kindInt16:
311 print(typestring, "(", *(*int16)(eface.data), ")")
312 case kindInt32:
313 print(typestring, "(", *(*int32)(eface.data), ")")
314 case kindInt64:
315 print(typestring, "(", *(*int64)(eface.data), ")")
316 case kindUint:
317 print(typestring, "(", *(*uint)(eface.data), ")")
318 case kindUint8:
319 print(typestring, "(", *(*uint8)(eface.data), ")")
320 case kindUint16:
321 print(typestring, "(", *(*uint16)(eface.data), ")")
322 case kindUint32:
323 print(typestring, "(", *(*uint32)(eface.data), ")")
324 case kindUint64:
325 print(typestring, "(", *(*uint64)(eface.data), ")")
326 case kindUintptr:
327 print(typestring, "(", *(*uintptr)(eface.data), ")")
328 case kindFloat32:
329 print(typestring, "(", *(*float32)(eface.data), ")")
330 case kindFloat64:
331 print(typestring, "(", *(*float64)(eface.data), ")")
332 case kindComplex64:
333 print(typestring, *(*complex64)(eface.data))
334 case kindComplex128:
335 print(typestring, *(*complex128)(eface.data))
336 default:
337 print("(", typestring, ") ", eface.data)