]> Cypherpunks repositories - gostls13.git/commitdiff
gc: diagnose field+method of same name
authorRuss Cox <rsc@golang.org>
Sat, 11 Feb 2012 06:21:12 +0000 (01:21 -0500)
committerRuss Cox <rsc@golang.org>
Sat, 11 Feb 2012 06:21:12 +0000 (01:21 -0500)
Fixes #2828.

R=ken2
CC=golang-dev
https://golang.org/cl/5653065

src/cmd/gc/dcl.c
test/fixedbugs/bug416.go [new file with mode: 0644]

index 66edab9b94bc2e7f825600f47737acf12c8b79cf..4a0e7430ac614b4cd03d237c2198b21a3c790363 100644 (file)
@@ -1303,6 +1303,14 @@ addmethod(Sym *sf, Type *t, int local)
        }
 
        pa = f;
+       if(pa->etype == TSTRUCT) {
+               for(f=pa->type; f; f=f->down) {
+                       if(f->sym == sf) {
+                               yyerror("type %T has both field and method named %S", pa, sf);
+                               return;
+                       }
+               }
+       }
 
        n = nod(ODCLFIELD, newname(sf), N);
        n->type = t;
diff --git a/test/fixedbugs/bug416.go b/test/fixedbugs/bug416.go
new file mode 100644 (file)
index 0000000..cc6d4a9
--- /dev/null
@@ -0,0 +1,13 @@
+// errchk $G $D/$F.go
+
+// Copyright 2012 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+type T struct {
+       X int
+}
+
+func (t *T) X() {} // ERROR "type T has both field and method named X"