1
0
mirror of https://github.com/golang/go synced 2024-11-22 00:24:41 -07:00

cmd/gc: Suggest *T in error for x.(T) if it would work.

Accomplished by synchronizing the formatting of conversion errors between typecheck.c and subr.c

Fixes #3984.

R=golang-dev, remyoudompheng, rsc
CC=golang-dev
https://golang.org/cl/6500064
This commit is contained in:
Daniel Morsing 2012-09-01 13:52:55 -04:00 committed by Russ Cox
parent 8f3c2055bd
commit 1c2021ca14
2 changed files with 19 additions and 7 deletions

View File

@ -735,14 +735,20 @@ reswitch:
} }
if(n->type != T && n->type->etype != TINTER) if(n->type != T && n->type->etype != TINTER)
if(!implements(n->type, t, &missing, &have, &ptr)) { if(!implements(n->type, t, &missing, &have, &ptr)) {
if(have) if(have && have->sym == missing->sym)
yyerror("impossible type assertion: %lN cannot have dynamic type %T" yyerror("impossible type assertion:\n\t%T does not implement %T (wrong type for %S method)\n"
" (wrong type for %S method)\n\thave %S%hT\n\twant %S%hT", "\t\thave %S%hhT\n\t\twant %S%hhT", n->type, t, missing->sym,
l, n->type, missing->sym, have->sym, have->type, have->sym, have->type, missing->sym, missing->type);
missing->sym, missing->type); else if(ptr)
yyerror("impossible type assertion:\n\t%T does not implement %T (%S method requires pointer receiver)",
n->type, t, missing->sym);
else if(have)
yyerror("impossible type assertion:\n\t%T does not implement %T (missing %S method)\n"
"\t\thave %S%hhT\n\t\twant %S%hhT", n->type, t, missing->sym,
have->sym, have->type, missing->sym, missing->type);
else else
yyerror("impossible type assertion: %lN cannot have dynamic type %T" yyerror("impossible type assertion:\n\t%T does not implement %T (missing %S method)",
" (missing %S method)", l, n->type, missing->sym); n->type, t, missing->sym);
goto error; goto error;
} }
goto ret; goto ret;

View File

@ -15,6 +15,10 @@ type T struct {
var t *T var t *T
type X int
func (x *X) M() {}
type I interface { type I interface {
M() M()
} }
@ -66,6 +70,8 @@ func (Int) M(float64) {}
var _ = m.(Int) // ERROR "impossible type assertion" var _ = m.(Int) // ERROR "impossible type assertion"
var _ = m.(X) // ERROR "pointer receiver"
var ii int var ii int
var jj Int var jj Int