summaryrefslogtreecommitdiff
path: root/src/pkg/runtime/iface.c
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2010-03-31 15:55:10 -0700
committerRuss Cox <rsc@golang.org>2010-03-31 15:55:10 -0700
commit6fe680e0846efc1d26e57969512d725e780a098c (patch)
treec1ef2374ccd744e5677a25df9dbac3407b5f291e /src/pkg/runtime/iface.c
parenta20ea8d7a98348caa3986652db8c1c76560fc870 (diff)
downloadgolang-6fe680e0846efc1d26e57969512d725e780a098c.tar.gz
runtime: make type assertion a runtime.Error, the first of many
R=r CC=golang-dev http://codereview.appspot.com/805043
Diffstat (limited to 'src/pkg/runtime/iface.c')
-rw-r--r--src/pkg/runtime/iface.c55
1 files changed, 39 insertions, 16 deletions
diff --git a/src/pkg/runtime/iface.c b/src/pkg/runtime/iface.c
index ce4234627..1af7ca7f5 100644
--- a/src/pkg/runtime/iface.c
+++ b/src/pkg/runtime/iface.c
@@ -46,10 +46,13 @@ itab(InterfaceType *inter, Type *type, int32 canfail)
Itab *m;
UncommonType *x;
Type *itype;
+ Eface err;
if(inter->mhdr.len == 0)
throw("internal error - misuse of itab");
+ locked = 0;
+
// easy case
x = type->x;
if(x == nil) {
@@ -114,9 +117,12 @@ search:
if(!canfail) {
throw:
// didn't find method
- printf("%S is not %S: missing method %S\n",
- *type->string, *inter->string, *iname);
- throw("interface conversion");
+ ·newTypeAssertionError(nil, type, inter,
+ nil, type->string, inter->string,
+ iname, &err);
+ if(locked)
+ unlock(&ifacelock);
+ ·panic(err);
return nil; // not reached
}
m->bad = 1;
@@ -211,16 +217,21 @@ void
{
Itab *tab;
byte *ret;
+ Eface err;
ret = (byte*)(&i+1);
tab = i.tab;
if(tab == nil) {
- printf("interface is nil, not %S\n", *t->string);
- throw("interface conversion");
+ ·newTypeAssertionError(nil, nil, t,
+ nil, nil, t->string,
+ nil, &err);
+ ·panic(err);
}
if(tab->type != t) {
- printf("%S is %S, not %S\n", *tab->inter->string, *tab->type->string, *t->string);
- throw("interface conversion");
+ ·newTypeAssertionError(tab->inter, tab->type, t,
+ tab->inter->string, tab->type->string, t->string,
+ nil, &err);
+ ·panic(err);
}
copyout(t, &i.data, ret);
}
@@ -254,15 +265,21 @@ void
·ifaceE2T(Type *t, Eface e, ...)
{
byte *ret;
+ Eface err;
ret = (byte*)(&e+1);
+ if(e.type == nil) {
+ ·newTypeAssertionError(nil, nil, t,
+ nil, nil, t->string,
+ nil, &err);
+ ·panic(err);
+ }
if(e.type != t) {
- if(e.type == nil)
- printf("interface is nil, not %S\n", *t->string);
- else
- printf("interface is %S, not %S\n", *e.type->string, *t->string);
- throw("interface conversion");
+ ·newTypeAssertionError(nil, e.type, t,
+ nil, e.type->string, t->string,
+ nil, &err);
+ ·panic(err);
}
copyout(t, &e.data, ret);
}
@@ -336,12 +353,15 @@ void
·ifaceI2Ix(InterfaceType *inter, Iface i, Iface ret)
{
Itab *tab;
+ Eface err;
tab = i.tab;
if(tab == nil) {
// explicit conversions require non-nil interface value.
- printf("interface is nil, not %S\n", *inter->string);
- throw("interface conversion");
+ ·newTypeAssertionError(nil, nil, inter,
+ nil, nil, inter->string,
+ nil, &err);
+ ·panic(err);
} else {
ret = i;
if(tab->inter != inter)
@@ -385,12 +405,15 @@ void
ifaceE2I(InterfaceType *inter, Eface e, Iface *ret)
{
Type *t;
+ Eface err;
t = e.type;
if(t == nil) {
// explicit conversions require non-nil interface value.
- printf("interface is nil, not %S\n", *inter->string);
- throw("interface conversion");
+ ·newTypeAssertionError(nil, nil, inter,
+ nil, nil, inter->string,
+ nil, &err);
+ ·panic(err);
} else {
ret->data = e.data;
ret->tab = itab(inter, t, 0);