1
0
mirror of https://github.com/golang/go synced 2024-11-25 10:07:56 -07:00

allow conversion to interface type

when implicit assignment would have been okay.

R=ken
OCL=31225
CL=31227
This commit is contained in:
Russ Cox 2009-07-06 17:20:48 -07:00
parent 53ebd163c6
commit d436a70193
4 changed files with 22 additions and 10 deletions

View File

@ -3136,10 +3136,10 @@ runifacechecks(void)
t, iface, m->sym, m->type);
else if(!p->explicit && needexplicit) {
if(m)
yyerror("need explicit conversion to use %T as %T\n\tmissing %S%hhT",
yyerror("need type assertion to use %T as %T\n\tmissing %S%hhT",
p->src, p->dst, m->sym, m->type);
else
yyerror("need explicit conversion to use %T as %T",
yyerror("need type assertion to use %T as %T",
p->src, p->dst);
}
}

View File

@ -1275,7 +1275,6 @@ walkconv(Node *n)
// if using .(T), interface assertion.
if(n->op == ODOTTYPE) {
// interface conversion
defaultlit(l, T);
if(!isinter(l->type))
yyerror("type assertion requires interface on left, have %T", l->type);
@ -1308,6 +1307,14 @@ walkconv(Node *n)
n->op = OCONVNOP;
return;
}
// to/from interface.
// ifaceas1 will generate a good error
// if the conversion is invalid.
if(t->etype == TINTER || l->type->etype == TINTER) {
indir(n, ifacecvt(t, l, ifaceas1(t, l->type, 0)));
return;
}
// simple fix-float
if(isint[l->type->etype] || isfloat[l->type->etype])

View File

@ -130,9 +130,7 @@ func send(req *Request) (resp *Response, err os.Error) {
resp.AddHeader(key, value);
}
// TODO(rsc): Make this work:
// r := io.Reader(reader);
var r io.Reader = reader;
r := io.Reader(reader);
if v := resp.GetHeader("Transfer-Encoding"); v == "chunked" {
r = newChunkedReader(reader);
}

View File

@ -15,13 +15,14 @@ type I interface { M() }
var i I
type I2 interface { M(); N(); }
var i2 I2;
var i2 I2
var e interface { };
type E interface { }
var e E
func main() {
e = t; // ok
t = e; // ERROR "need explicit"
t = e; // ERROR "need explicit|need type assertion"
// neither of these can work,
// because i has an extra method
@ -30,5 +31,11 @@ func main() {
t = i; // ERROR "missing|incompatible|is not"
i = i2; // ok
i2 = i; // ERROR "need explicit"
i2 = i; // ERROR "need explicit|need type assertion"
i = I(i2); // ok
i2 = I2(i); // ERROR "need explicit|need type assertion"
e = E(t); // ok
t = T(e); // ERROR "need explicit|need type assertion"
}