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 os.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 interfaceType *Type // interface had this type
21 concreteType *Type // concrete value had this type
22 assertedType *Type // asserted type
23 interfaceString string
26 missingMethod string // one method needed by Interface, missing from Concrete
29 func (*TypeAssertionError) RuntimeError() {}
31 func (e *TypeAssertionError) String() string {
32 inter := e.interfaceString
36 if e.concreteType == nil {
37 return "interface conversion: " + inter + " is nil, not " + e.assertedString
39 if e.missingMethod == "" {
40 return "interface conversion: " + inter + " is " + e.concreteString +
41 ", not " + e.assertedString
43 return "interface conversion: " + e.concreteString + " is not " + e.assertedString +
44 ": missing method " + e.missingMethod
47 // Concrete returns the type of the concrete value in the failed type assertion.
48 // If the interface value was nil, Concrete returns nil.
49 func (e *TypeAssertionError) Concrete() *Type {
53 // Asserted returns the type incorrectly asserted by the type assertion.
54 func (e *TypeAssertionError) Asserted() *Type {
58 // If the type assertion is to an interface type, MissingMethod returns the
59 // name of a method needed to satisfy that interface type but not implemented
60 // by Concrete. If there are multiple such methods,
61 // MissingMethod returns one; which one is unspecified.
62 // If the type assertion is not to an interface type, MissingMethod returns an empty string.
63 func (e *TypeAssertionError) MissingMethod() string {
64 return e.missingMethod
67 // For calling from C.
68 func NewTypeAssertionError(pt1, pt2, pt3 *Type, ps1, ps2, ps3 *string, pmeth *string, ret *interface{}) {
70 var s1, s2, s3, meth string
93 *ret = &TypeAssertionError{t1, t2, t3, s1, s2, s3, meth}
96 // An errorString represents a runtime error described by a single string.
97 type errorString string
99 func (e errorString) RuntimeError() {}
101 func (e errorString) String() string {
102 return "runtime error: " + string(e)
105 // For calling from C.
106 func NewErrorString(s string, ret *interface{}) {
107 *ret = errorString(s)
110 type stringer interface {
114 func typestring(interface{}) string
116 // For calling from C.
117 // Prints an argument passed to panic.
118 // There's room for arbitrary complexity here, but we keep it
119 // simple and handle just a few important cases: int, string, and Stringer.
120 func Printany(i interface{}) {
121 switch v := i.(type) {
131 print("(", typestring(i), ") ", i)