]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: disallow "init" as alias
authorRobert Griesemer <gri@golang.org>
Thu, 27 Oct 2016 19:53:25 +0000 (12:53 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 27 Oct 2016 20:01:20 +0000 (20:01 +0000)
Fixes #17637.

Change-Id: I5af63b8277c0a0f9fef4880992bcb925ca088687
Reviewed-on: https://go-review.googlesource.com/32106
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/noder.go
test/alias2.go

index 644abcc204e3532f251997bf1053b497c0f7bb4e..f8056fee97398c87124016cef41a907de0b53b84 100644 (file)
@@ -214,8 +214,12 @@ func (p *noder) aliasDecl(decl *syntax.AliasDecl) {
                return
        }
 
-       // don't declare blank aliases
-       if decl.Name.Value == "_" {
+       // handle special cases
+       switch decl.Name.Value {
+       case "_":
+               return // don't declare blank aliases
+       case "init":
+               yyerror("cannot declare init - must be non-alias function declaration")
                return
        }
 
index f160d384b14abca6c7d3140198c85a8f348da609..6fad914420e938bf3436d617927590cd4eb02076 100644 (file)
@@ -9,6 +9,7 @@
 package p
 
 import (
+       "flag"
        "fmt" // use at most once (to test "imported but not used" error)
        "go/build"
        . "go/build"
@@ -74,13 +75,19 @@ func _ => math.Sin
 func sin => math.Sin
 func sin1 => math.Pi // ERROR "math.Pi is not a function"
 
+// aliases may not be called init
+func init => flag.Parse // ERROR "cannot declare init"
+
 // alias reference to a package marks package as used
 func _ => fmt.Println
 
 // re-exported aliases
 const Pi => math.Pi
+
 type Writer => io.Writer
+
 var Def => build.Default
+
 func Sin => math.Sin
 
 // type aliases denote identical types