]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: Suggest *T in error for x.(T) if it would work.
authorDaniel Morsing <daniel.morsing@gmail.com>
Sat, 1 Sep 2012 17:52:55 +0000 (13:52 -0400)
committerRuss Cox <rsc@golang.org>
Sat, 1 Sep 2012 17:52:55 +0000 (13:52 -0400)
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

src/cmd/gc/typecheck.c
test/interface/explicit.go

index f21f07faaa6b8e59aaa5b31dcd48720e5b9e0c18..eeb46e2aa7edcee3aa0aa2553d49f50851775aef 100644 (file)
@@ -735,14 +735,20 @@ reswitch:
                }
                if(n->type != T && n->type->etype != TINTER)
                if(!implements(n->type, t, &missing, &have, &ptr)) {
-                       if(have)
-                               yyerror("impossible type assertion: %lN cannot have dynamic type %T"
-                                       " (wrong type for %S method)\n\thave %S%hT\n\twant %S%hT",
-                                       l, n->type, missing->sym, have->sym, have->type,
-                                       missing->sym, missing->type);
+                       if(have && have->sym == missing->sym)
+                               yyerror("impossible type assertion:\n\t%T does not implement %T (wrong type for %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 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
-                               yyerror("impossible type assertion: %lN cannot have dynamic type %T"
-                                       " (missing %S method)", l, n->type, missing->sym);
+                               yyerror("impossible type assertion:\n\t%T does not implement %T (missing %S method)",
+                                       n->type, t, missing->sym);
                        goto error;
                }
                goto ret;
index 7822b88d0832fc083fb03027c925e96f672ad114..eb81156e081d79ac08f966a423b28cc7313a6c31 100644 (file)
@@ -15,6 +15,10 @@ type T struct {
 
 var t *T
 
+type X int
+
+func (x *X) M() {}
+
 type I interface {
        M()
 }
@@ -66,6 +70,8 @@ func (Int) M(float64) {}
 
 var _ = m.(Int) // ERROR "impossible type assertion"
 
+var _ = m.(X) // ERROR "pointer receiver"
+
 var ii int
 var jj Int