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.
7 // The Error interface identifies a run time error.
11 // RuntimeError is a no-op function but
12 // serves to distinguish types that are runtime
13 // errors from ordinary errors: a type is a
14 // runtime error if it has a RuntimeError method.
18 // A TypeAssertionError explains a failed type assertion.
19 type TypeAssertionError
struct {
20 interfaceString
string
23 missingMethod
string // one method needed by Interface, missing from Concrete
26 func (*TypeAssertionError
) RuntimeError() {}
28 func (e
*TypeAssertionError
) Error() string {
29 inter
:= e
.interfaceString
33 if e
.concreteString
== "" {
34 return "interface conversion: " + inter
+ " is nil, not " + e
.assertedString
36 if e
.missingMethod
== "" {
37 return "interface conversion: " + inter
+ " is " + e
.concreteString
+
38 ", not " + e
.assertedString
40 return "interface conversion: " + e
.concreteString
+ " is not " + e
.assertedString
+
41 ": missing method " + e
.missingMethod
44 // For calling from C.
45 func NewTypeAssertionError(ps1
, ps2
, ps3
*string, pmeth
*string, ret
*interface{}) {
46 var s1
, s2
, s3
, meth
string
61 // For gccgo, strip out quoted strings.
66 *ret
= &TypeAssertionError
{s1
, s2
, s3
, meth
}
69 // Remove quoted strings from gccgo reflection strings.
70 func unquote(s
string) string {
73 for i
= 0; i
< ls
; i
++ {
82 r
:= make([]byte, len(s
))
84 for i
= 0; i
< ls
; i
++ {
95 // An errorString represents a runtime error described by a single string.
96 type errorString
string
98 func (e errorString
) RuntimeError() {}
100 func (e errorString
) Error() string {
101 return "runtime error: " + string(e
)
104 // For calling from C.
105 func NewErrorString(s
string, ret
*interface{}) {
106 *ret
= errorString(s
)
109 // An errorCString represents a runtime error described by a single C string.
110 type errorCString
uintptr
112 func (e errorCString
) RuntimeError() {}
114 func cstringToGo(uintptr) string
116 func (e errorCString
) Error() string {
117 return "runtime error: " + cstringToGo(uintptr(e
))
120 // For calling from C.
121 func NewErrorCString(s
uintptr, ret
*interface{}) {
122 *ret
= errorCString(s
)
125 type stringer
interface {
129 func typestring(interface{}) string
131 // For calling from C.
132 // Prints an argument passed to panic.
133 // There's room for arbitrary complexity here, but we keep it
134 // simple and handle just a few important cases: int, string, and Stringer.
135 func Printany(i
interface{}) {
136 switch v
:= i
.(type) {
148 print("(", typestring(i
), ") ", i
)
152 // called from generated code
153 func panicwrap(pkg
, typ
, meth
string) {
154 panic("value method " + pkg
+ "." + typ
+ "." + meth
+ " called using nil *" + typ
+ " pointer")