]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile: don't allow blank method declarations on builtins
authorHÃ¥vard Haugen <havard.haugen@gmail.com>
Sun, 15 Nov 2015 22:32:30 +0000 (23:32 +0100)
committerRobert Griesemer <gri@golang.org>
Wed, 2 Dec 2015 18:26:38 +0000 (18:26 +0000)
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>
src/cmd/compile/internal/gc/dcl.go
src/cmd/compile/internal/gc/typecheck.go
test/blank1.go

index 9b865bbee931d6ab4bb5398be25568dd761138ee..fc47a39ee62ee08919417c6079dd236beef5dbde 100644 (file)
@@ -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 {
@@ -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
 
index f04578ef8ff8cd3ca61e5b662b4d457173c2c791..70560d405df4e78bc3e47cf95dc2404fe8ec3ed3 100644 (file)
@@ -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)
        }
 
index 54a72976b77c64a6cac7efd6fcfa21cc9e6ee1ea..bf94d1a0fb91dca76c9c91653624c614be229492 100644 (file)
@@ -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
 }