2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
2016-10-12 20:17:52 +02:00
|
|
|
import "unsafe"
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// The Error interface identifies a run time error.
|
|
|
|
type Error interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
error
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// RuntimeError is a no-op function but
|
2015-10-31 01:59:47 +01:00
|
|
|
// serves to distinguish types that are run time
|
2011-12-03 03:17:34 +01:00
|
|
|
// errors from ordinary errors: a type is a
|
2015-10-31 01:59:47 +01:00
|
|
|
// run time error if it has a RuntimeError method.
|
2010-12-03 05:34:57 +01:00
|
|
|
RuntimeError()
|
|
|
|
}
|
|
|
|
|
|
|
|
// A TypeAssertionError explains a failed type assertion.
|
|
|
|
type TypeAssertionError struct {
|
2018-09-24 23:46:21 +02:00
|
|
|
_interface *_type
|
|
|
|
concrete *_type
|
|
|
|
asserted *_type
|
|
|
|
missingMethod string // one method needed by Interface, missing from Concrete
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*TypeAssertionError) RuntimeError() {}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (e *TypeAssertionError) Error() string {
|
2018-09-24 23:46:21 +02:00
|
|
|
inter := "interface"
|
|
|
|
if e._interface != nil {
|
|
|
|
inter = e._interface.string()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2018-09-24 23:46:21 +02:00
|
|
|
as := e.asserted.string()
|
|
|
|
if e.concrete == nil {
|
|
|
|
return "interface conversion: " + inter + " is nil, not " + as
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2018-09-24 23:46:21 +02:00
|
|
|
cs := e.concrete.string()
|
2010-12-03 05:34:57 +01:00
|
|
|
if e.missingMethod == "" {
|
2018-09-24 23:46:21 +02:00
|
|
|
msg := "interface conversion: " + inter + " is " + cs + ", not " + as
|
|
|
|
if cs == as {
|
|
|
|
// provide slightly clearer error message
|
|
|
|
if e.concrete.pkgpath() != e.asserted.pkgpath() {
|
|
|
|
msg += " (types from different packages)"
|
|
|
|
} else {
|
|
|
|
msg += " (types from different scopes)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2018-09-24 23:46:21 +02:00
|
|
|
return "interface conversion: " + cs + " is not " + as +
|
2010-12-03 05:34:57 +01:00
|
|
|
": missing method " + e.missingMethod
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// Remove quoted strings from gccgo reflection strings.
|
|
|
|
func unquote(s string) string {
|
|
|
|
ls := len(s)
|
|
|
|
var i int
|
|
|
|
for i = 0; i < ls; i++ {
|
|
|
|
if s[i] == '\t' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i == ls {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
var q bool
|
|
|
|
r := make([]byte, len(s))
|
|
|
|
j := 0
|
|
|
|
for i = 0; i < ls; i++ {
|
|
|
|
if s[i] == '\t' {
|
|
|
|
q = !q
|
|
|
|
} else if !q {
|
|
|
|
r[j] = s[i]
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(r[:j])
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// An errorString represents a runtime error described by a single string.
|
|
|
|
type errorString string
|
|
|
|
|
|
|
|
func (e errorString) RuntimeError() {}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (e errorString) Error() string {
|
2010-12-03 05:34:57 +01:00
|
|
|
return "runtime error: " + string(e)
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// An errorCString represents a runtime error described by a single C string.
|
2014-06-07 00:37:27 +02:00
|
|
|
// Not "type errorCString uintptr" because of http://golang.org/issue/7084.
|
|
|
|
type errorCString struct{ cstr uintptr }
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
func (e errorCString) RuntimeError() {}
|
|
|
|
|
|
|
|
func (e errorCString) Error() string {
|
2016-10-12 20:17:52 +02:00
|
|
|
return "runtime error: " + gostringnocopy((*byte)(unsafe.Pointer(e.cstr)))
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// For calling from C.
|
|
|
|
func NewErrorCString(s uintptr, ret *interface{}) {
|
2014-06-07 00:37:27 +02:00
|
|
|
*ret = errorCString{s}
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
// plainError represents a runtime error described a string without
|
|
|
|
// the prefix "runtime error: " after invoking errorString.Error().
|
|
|
|
// See Issue #14965.
|
|
|
|
type plainError string
|
|
|
|
|
|
|
|
func (e plainError) RuntimeError() {}
|
|
|
|
|
|
|
|
func (e plainError) Error() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
type stringer interface {
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2016-11-16 19:33:11 +01:00
|
|
|
func typestring(x interface{}) string {
|
|
|
|
e := efaceOf(&x)
|
2018-09-24 23:46:21 +02:00
|
|
|
return e._type.string()
|
2016-11-16 19:33:11 +01:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2018-01-28 00:44:29 +01:00
|
|
|
// printany prints an argument passed to panic.
|
2018-05-31 23:42:53 +02:00
|
|
|
// If panic is called with a value that has a String or Error method,
|
|
|
|
// it has already been converted into a string by preprintpanics.
|
2016-11-22 18:58:04 +01:00
|
|
|
func printany(i interface{}) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch v := i.(type) {
|
|
|
|
case nil:
|
|
|
|
print("nil")
|
2017-09-14 19:11:35 +02:00
|
|
|
case bool:
|
|
|
|
print(v)
|
2010-12-03 05:34:57 +01:00
|
|
|
case int:
|
|
|
|
print(v)
|
2017-09-14 19:11:35 +02:00
|
|
|
case int8:
|
|
|
|
print(v)
|
|
|
|
case int16:
|
|
|
|
print(v)
|
|
|
|
case int32:
|
|
|
|
print(v)
|
|
|
|
case int64:
|
|
|
|
print(v)
|
|
|
|
case uint:
|
|
|
|
print(v)
|
|
|
|
case uint8:
|
|
|
|
print(v)
|
|
|
|
case uint16:
|
|
|
|
print(v)
|
|
|
|
case uint32:
|
|
|
|
print(v)
|
|
|
|
case uint64:
|
|
|
|
print(v)
|
|
|
|
case uintptr:
|
|
|
|
print(v)
|
|
|
|
case float32:
|
|
|
|
print(v)
|
|
|
|
case float64:
|
|
|
|
print(v)
|
|
|
|
case complex64:
|
|
|
|
print(v)
|
|
|
|
case complex128:
|
|
|
|
print(v)
|
2010-12-03 05:34:57 +01:00
|
|
|
case string:
|
|
|
|
print(v)
|
|
|
|
default:
|
|
|
|
print("(", typestring(i), ") ", i)
|
|
|
|
}
|
|
|
|
}
|