From 7dd90621f8f8ed25708daf279067e8c0024af20b Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Sat, 11 Feb 2012 01:21:12 -0500 Subject: [PATCH] gc: diagnose field+method of same name Fixes #2828. R=ken2 CC=golang-dev https://golang.org/cl/5653065 --- src/cmd/gc/dcl.c | 8 ++++++++ test/fixedbugs/bug416.go | 13 +++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test/fixedbugs/bug416.go diff --git a/src/cmd/gc/dcl.c b/src/cmd/gc/dcl.c index 66edab9b94b..4a0e7430ac6 100644 --- a/src/cmd/gc/dcl.c +++ b/src/cmd/gc/dcl.c @@ -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 index 00000000000..cc6d4a9f288 --- /dev/null +++ b/test/fixedbugs/bug416.go @@ -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"