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