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 // Not "type errorCString uintptr" because of http://golang.org/issue/7084.
111 type errorCString
struct{ cstr
uintptr }
113 func (e errorCString
) RuntimeError() {}
115 func cstringToGo(uintptr) string
117 func (e errorCString
) Error() string {
118 return "runtime error: " + cstringToGo(e
.cstr
)
121 // For calling from C.
122 func NewErrorCString(s
uintptr, ret
*interface{}) {
123 *ret
= errorCString
{s
}
126 type stringer
interface {
130 func typestring(interface{}) string
132 // For calling from C.
133 // Prints an argument passed to panic.
134 // There's room for arbitrary complexity here, but we keep it
135 // simple and handle just a few important cases: int, string, and Stringer.
136 func Printany(i
interface{}) {
137 switch v
:= i
.(type) {
149 print("(", typestring(i
), ") ", i
)
153 // called from generated code
154 func panicwrap(pkg
, typ
, meth
string) {
155 panic("value method " + pkg
+ "." + typ
+ "." + meth
+ " called using nil *" + typ
+ " pointer")