Source file
src/runtime/error.go
1
2
3
4
5 package runtime
6
7 import (
8 "internal/abi"
9 "internal/bytealg"
10 "internal/runtime/sys"
11 )
12
13
14
15
16
17
18
19
20
21
22
23
24 type Error interface {
25 error
26
27
28
29
30
31 RuntimeError()
32 }
33
34
35 type TypeAssertionError struct {
36 _interface *_type
37 concrete *_type
38 asserted *_type
39 missingMethod string
40 }
41
42 func (*TypeAssertionError) RuntimeError() {}
43
44 func (e *TypeAssertionError) Error() string {
45 inter := "interface"
46 if e._interface != nil {
47 inter = toRType(e._interface).string()
48 }
49 as := toRType(e.asserted).string()
50 if e.concrete == nil {
51 return "interface conversion: " + inter + " is nil, not " + as
52 }
53 cs := toRType(e.concrete).string()
54 if e.missingMethod == "" {
55 msg := "interface conversion: " + inter + " is " + cs + ", not " + as
56 if cs == as {
57
58 if toRType(e.concrete).pkgpath() != toRType(e.asserted).pkgpath() {
59 msg += " (types from different packages)"
60 } else {
61 msg += " (types from different scopes)"
62 }
63 }
64 return msg
65 }
66 return "interface conversion: " + cs + " is not " + as +
67 ": missing method " + e.missingMethod
68 }
69
70
71
72
73
74
75 func itoa(buf []byte, val uint64) []byte {
76 i := len(buf) - 1
77 for val >= 10 {
78 buf[i] = byte(val%10 + '0')
79 i--
80 val /= 10
81 }
82 buf[i] = byte(val + '0')
83 return buf[i:]
84 }
85
86
87 type errorString string
88
89 func (e errorString) RuntimeError() {}
90
91 func (e errorString) Error() string {
92 return "runtime error: " + string(e)
93 }
94
95 type errorAddressString struct {
96 msg string
97 addr uintptr
98 }
99
100 func (e errorAddressString) RuntimeError() {}
101
102 func (e errorAddressString) Error() string {
103 return "runtime error: " + e.msg
104 }
105
106 var _ error = errorAddressString{}
107
108
109
110
111
112
113 func (e errorAddressString) Addr() uintptr {
114 return e.addr
115 }
116
117
118
119
120 type plainError string
121
122 func (e plainError) RuntimeError() {}
123
124 func (e plainError) Error() string {
125 return string(e)
126 }
127
128 var _ error = plainError("")
129
130
131 type boundsError struct {
132 x int64
133 y int
134
135
136
137
138 signed bool
139 code abi.BoundsErrorCode
140 }
141
142 var _ error = boundsError{}
143
144
145
146
147 var boundsErrorFmts = [...]string{
148 abi.BoundsIndex: "index out of range [%x] with length %y",
149 abi.BoundsSliceAlen: "slice bounds out of range [:%x] with length %y",
150 abi.BoundsSliceAcap: "slice bounds out of range [:%x] with capacity %y",
151 abi.BoundsSliceB: "slice bounds out of range [%x:%y]",
152 abi.BoundsSlice3Alen: "slice bounds out of range [::%x] with length %y",
153 abi.BoundsSlice3Acap: "slice bounds out of range [::%x] with capacity %y",
154 abi.BoundsSlice3B: "slice bounds out of range [:%x:%y]",
155 abi.BoundsSlice3C: "slice bounds out of range [%x:%y:]",
156 abi.BoundsConvert: "cannot convert slice with length %y to array or pointer to array with length %x",
157 }
158
159
160 var boundsNegErrorFmts = [...]string{
161 abi.BoundsIndex: "index out of range [%x]",
162 abi.BoundsSliceAlen: "slice bounds out of range [:%x]",
163 abi.BoundsSliceAcap: "slice bounds out of range [:%x]",
164 abi.BoundsSliceB: "slice bounds out of range [%x:]",
165 abi.BoundsSlice3Alen: "slice bounds out of range [::%x]",
166 abi.BoundsSlice3Acap: "slice bounds out of range [::%x]",
167 abi.BoundsSlice3B: "slice bounds out of range [:%x:]",
168 abi.BoundsSlice3C: "slice bounds out of range [%x::]",
169 }
170
171 func (e boundsError) RuntimeError() {}
172
173 func appendIntStr(b []byte, v int64, signed bool) []byte {
174 if signed && v < 0 {
175 b = append(b, '-')
176 v = -v
177 }
178 var buf [20]byte
179 b = append(b, itoa(buf[:], uint64(v))...)
180 return b
181 }
182
183 func (e boundsError) Error() string {
184 fmt := boundsErrorFmts[e.code]
185 if e.signed && e.x < 0 {
186 fmt = boundsNegErrorFmts[e.code]
187 }
188
189
190 b := make([]byte, 0, 100)
191 b = append(b, "runtime error: "...)
192 for i := 0; i < len(fmt); i++ {
193 c := fmt[i]
194 if c != '%' {
195 b = append(b, c)
196 continue
197 }
198 i++
199 switch fmt[i] {
200 case 'x':
201 b = appendIntStr(b, e.x, e.signed)
202 case 'y':
203 b = appendIntStr(b, int64(e.y), true)
204 }
205 }
206 return string(b)
207 }
208
209 type stringer interface {
210 String() string
211 }
212
213
214
215
216
217
218
219
220
221 func printpanicval(v any) {
222 switch v := v.(type) {
223 case nil:
224 print("nil")
225 case bool:
226 print(v)
227 case int:
228 print(v)
229 case int8:
230 print(v)
231 case int16:
232 print(v)
233 case int32:
234 print(v)
235 case int64:
236 print(v)
237 case uint:
238 print(v)
239 case uint8:
240 print(v)
241 case uint16:
242 print(v)
243 case uint32:
244 print(v)
245 case uint64:
246 print(v)
247 case uintptr:
248 print(v)
249 case float32:
250 print(v)
251 case float64:
252 print(v)
253 case complex64:
254 print(v)
255 case complex128:
256 print(v)
257 case string:
258 printindented(v)
259 default:
260 printanycustomtype(v)
261 }
262 }
263
264
265 func printanycustomtype(i any) {
266 eface := efaceOf(&i)
267 typestring := toRType(eface._type).string()
268
269 switch eface._type.Kind() {
270 case abi.String:
271 print(typestring, `("`)
272 printindented(*(*string)(eface.data))
273 print(`")`)
274 case abi.Bool:
275 print(typestring, "(", *(*bool)(eface.data), ")")
276 case abi.Int:
277 print(typestring, "(", *(*int)(eface.data), ")")
278 case abi.Int8:
279 print(typestring, "(", *(*int8)(eface.data), ")")
280 case abi.Int16:
281 print(typestring, "(", *(*int16)(eface.data), ")")
282 case abi.Int32:
283 print(typestring, "(", *(*int32)(eface.data), ")")
284 case abi.Int64:
285 print(typestring, "(", *(*int64)(eface.data), ")")
286 case abi.Uint:
287 print(typestring, "(", *(*uint)(eface.data), ")")
288 case abi.Uint8:
289 print(typestring, "(", *(*uint8)(eface.data), ")")
290 case abi.Uint16:
291 print(typestring, "(", *(*uint16)(eface.data), ")")
292 case abi.Uint32:
293 print(typestring, "(", *(*uint32)(eface.data), ")")
294 case abi.Uint64:
295 print(typestring, "(", *(*uint64)(eface.data), ")")
296 case abi.Uintptr:
297 print(typestring, "(", *(*uintptr)(eface.data), ")")
298 case abi.Float32:
299 print(typestring, "(", *(*float32)(eface.data), ")")
300 case abi.Float64:
301 print(typestring, "(", *(*float64)(eface.data), ")")
302 case abi.Complex64:
303 print(typestring, *(*complex64)(eface.data))
304 case abi.Complex128:
305 print(typestring, *(*complex128)(eface.data))
306 default:
307 print("(", typestring, ") ", eface.data)
308 }
309 }
310
311
312 func printindented(s string) {
313 for {
314 i := bytealg.IndexByteString(s, '\n')
315 if i < 0 {
316 break
317 }
318 i += len("\n")
319 print(s[:i])
320 print("\t")
321 s = s[i:]
322 }
323 print(s)
324 }
325
326
327
328
329
330 func panicwrap() {
331 pc := sys.GetCallerPC()
332 name := funcNameForPrint(funcname(findfunc(pc)))
333
334
335
336 i := bytealg.IndexByteString(name, '(')
337 if i < 0 {
338 throw("panicwrap: no ( in " + name)
339 }
340 pkg := name[:i-1]
341 if i+2 >= len(name) || name[i-1:i+2] != ".(*" {
342 throw("panicwrap: unexpected string after package name: " + name)
343 }
344 name = name[i+2:]
345 i = bytealg.IndexByteString(name, ')')
346 if i < 0 {
347 throw("panicwrap: no ) in " + name)
348 }
349 if i+2 >= len(name) || name[i:i+2] != ")." {
350 throw("panicwrap: unexpected string after type name: " + name)
351 }
352 typ := name[:i]
353 meth := name[i+2:]
354 panic(plainError("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer"))
355 }
356
View as plain text