1
0
mirror of https://github.com/golang/go synced 2024-11-24 18:10:02 -07:00

gc: explain why invalid receiver types are invalid

Fixes #1680.

R=ken2
CC=golang-dev
https://golang.org/cl/4446061
This commit is contained in:
Russ Cox 2011-04-25 17:16:44 -04:00
parent 8698bb6c8c
commit 4684df520f
2 changed files with 34 additions and 2 deletions

View File

@ -1139,6 +1139,32 @@ addmethod(Sym *sf, Type *t, int local)
pa = pa->type;
f = methtype(pa);
if(f == T) {
t = pa;
if(t != T) {
if(isptr[t->etype]) {
if(t->sym != S) {
yyerror("invalid receiver type %T (%T is a pointer type)", pa, t);
return;
}
t = t->type;
}
}
if(t != T) {
if(t->sym == S) {
yyerror("invalid receiver type %T (%T is an unnamed type)", pa, t);
return;
}
if(isptr[t->etype]) {
yyerror("invalid receiver type %T (%T is a pointer type)", pa, t);
return;
}
if(t->etype == TINTER) {
yyerror("invalid receiver type %T (%T is an interface type)", pa, t);
return;
}
}
// Should have picked off all the reasons above,
// but just in case, fall back to generic error.
yyerror("invalid receiver type %T", pa);
return;
}

View File

@ -12,8 +12,14 @@ type T struct {
type P *T
type P1 *T
func (p P) val() int { return 1 } // ERROR "receiver"
func (p *P1) val() int { return 1 } // ERROR "receiver"
func (p P) val() int { return 1 } // ERROR "receiver.* pointer"
func (p *P1) val() int { return 1 } // ERROR "receiver.* pointer"
type I interface{}
type I1 interface{}
func (p I) val() int { return 1 } // ERROR "receiver.*interface"
func (p *I1) val() int { return 1 } // ERROR "receiver.*interface"
type Val interface {
val() int