Retro68/gcc/libgo/go/runtime/error.go

171 lines
3.6 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00: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
import "unsafe"
2012-03-27 23:13:14 +00:00
// The Error interface identifies a run time error.
type Error interface {
error
// RuntimeError is a no-op function but
2017-04-10 11:32:00 +00:00
// serves to distinguish types that are run time
2012-03-27 23:13:14 +00:00
// errors from ordinary errors: a type is a
2017-04-10 11:32:00 +00:00
// run time error if it has a RuntimeError method.
2012-03-27 23:13:14 +00:00
RuntimeError()
}
// A TypeAssertionError explains a failed type assertion.
type TypeAssertionError struct {
2019-06-02 15:48:37 +00:00
_interface *_type
concrete *_type
asserted *_type
missingMethod string // one method needed by Interface, missing from Concrete
2012-03-27 23:13:14 +00:00
}
func (*TypeAssertionError) RuntimeError() {}
func (e *TypeAssertionError) Error() string {
2019-06-02 15:48:37 +00:00
inter := "interface"
if e._interface != nil {
inter = e._interface.string()
2012-03-27 23:13:14 +00:00
}
2019-06-02 15:48:37 +00:00
as := e.asserted.string()
if e.concrete == nil {
return "interface conversion: " + inter + " is nil, not " + as
2012-03-27 23:13:14 +00:00
}
2019-06-02 15:48:37 +00:00
cs := e.concrete.string()
2012-03-27 23:13:14 +00:00
if e.missingMethod == "" {
2019-06-02 15:48:37 +00: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
2012-03-27 23:13:14 +00:00
}
2019-06-02 15:48:37 +00:00
return "interface conversion: " + cs + " is not " + as +
2012-03-27 23:13:14 +00:00
": missing method " + e.missingMethod
}
2014-09-21 17:33:12 +00: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])
}
2012-03-27 23:13:14 +00:00
// An errorString represents a runtime error described by a single string.
type errorString string
func (e errorString) RuntimeError() {}
func (e errorString) Error() string {
return "runtime error: " + string(e)
}
2014-09-21 17:33:12 +00:00
// An errorCString represents a runtime error described by a single C string.
2015-08-28 15:33:40 +00:00
// Not "type errorCString uintptr" because of http://golang.org/issue/7084.
type errorCString struct{ cstr uintptr }
2014-09-21 17:33:12 +00:00
func (e errorCString) RuntimeError() {}
func (e errorCString) Error() string {
return "runtime error: " + gostringnocopy((*byte)(unsafe.Pointer(e.cstr)))
2014-09-21 17:33:12 +00:00
}
// For calling from C.
func NewErrorCString(s uintptr, ret *interface{}) {
2015-08-28 15:33:40 +00:00
*ret = errorCString{s}
2014-09-21 17:33:12 +00: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)
}
2012-03-27 23:13:14 +00:00
type stringer interface {
String() string
}
func typestring(x interface{}) string {
e := efaceOf(&x)
2019-06-02 15:48:37 +00:00
return e._type.string()
}
2012-03-27 23:13:14 +00:00
2018-12-28 15:30:48 +00:00
// printany prints an argument passed to panic.
// If panic is called with a value that has a String or Error method,
// it has already been converted into a string by preprintpanics.
func printany(i interface{}) {
2012-03-27 23:13:14 +00:00
switch v := i.(type) {
case nil:
print("nil")
2018-12-28 15:30:48 +00:00
case bool:
print(v)
2012-03-27 23:13:14 +00:00
case int:
print(v)
2018-12-28 15:30:48 +00: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)
2012-03-27 23:13:14 +00:00
case string:
print(v)
default:
print("(", typestring(i), ") ", i)
}
}