Skip to content

Commit

Permalink
cmd/compile: don't allow blank method declarations on builtins
Browse files Browse the repository at this point in the history
Move test for isblank into addmethod so that most of the type checking
for methods is also performed for blank methods.

Fixes #11366.

Change-Id: I13d554723bf96d906d0b3ff390d7b7c87c1a5020
Reviewed-on: https://go-review.googlesource.com/16866
Reviewed-by: Robert Griesemer <gri@golang.org>
  • Loading branch information
osocurioso authored and griesemer committed Dec 2, 2015
1 parent 0c516c1 commit 8a34cf7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/cmd/compile/internal/gc/dcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,15 @@ func addmethod(sf *Sym, t *Type, local bool, nointerface bool) {
}

pa = f
if local && !pa.Local {
Yyerror("cannot define new methods on non-local type %v", pa)
return
}

if isblanksym(sf) {
return
}

if pa.Etype == TSTRUCT {
for f := pa.Type; f != nil; f = f.Down {
if f.Sym == sf {
Expand All @@ -1383,13 +1392,6 @@ func addmethod(sf *Sym, t *Type, local bool, nointerface bool) {
}
}

if local && !pa.Local {
// defining method on non-local type.
Yyerror("cannot define new methods on non-local type %v", pa)

return
}

n := Nod(ODCLFIELD, newname(sf), nil)
n.Type = t

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/gc/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -3441,7 +3441,7 @@ func typecheckfunc(n *Node) {
n.Type = t
t.Nname = n.Func.Nname
rcvr := getthisx(t).Type
if rcvr != nil && n.Func.Shortname != nil && !isblank(n.Func.Shortname) {
if rcvr != nil && n.Func.Shortname != nil {
addmethod(n.Func.Shortname.Sym, t, true, n.Func.Nname.Nointerface)
}

Expand Down
4 changes: 4 additions & 0 deletions test/blank1.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ var t struct {
_ int
}

func (x int) _() { // ERROR "cannot define new methods on non-local type"
println(x)
}

type T struct {
_ []int
}
Expand Down

0 comments on commit 8a34cf7

Please sign in to comment.