Skip to content

Commit 50f66fb

Browse files
committed
cmd/compile: disallow "init" as alias
Fixes #17637. Change-Id: I5af63b8277c0a0f9fef4880992bcb925ca088687 Reviewed-on: https://go-review.googlesource.com/32106 Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 89632aa commit 50f66fb

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/cmd/compile/internal/gc/noder.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,12 @@ func (p *noder) aliasDecl(decl *syntax.AliasDecl) {
214214
return
215215
}
216216

217-
// don't declare blank aliases
218-
if decl.Name.Value == "_" {
217+
// handle special cases
218+
switch decl.Name.Value {
219+
case "_":
220+
return // don't declare blank aliases
221+
case "init":
222+
yyerror("cannot declare init - must be non-alias function declaration")
219223
return
220224
}
221225

test/alias2.go

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package p
1010

1111
import (
12+
"flag"
1213
"fmt" // use at most once (to test "imported but not used" error)
1314
"go/build"
1415
. "go/build"
@@ -74,13 +75,19 @@ func _ => math.Sin
7475
func sin => math.Sin
7576
func sin1 => math.Pi // ERROR "math.Pi is not a function"
7677

78+
// aliases may not be called init
79+
func init => flag.Parse // ERROR "cannot declare init"
80+
7781
// alias reference to a package marks package as used
7882
func _ => fmt.Println
7983

8084
// re-exported aliases
8185
const Pi => math.Pi
86+
8287
type Writer => io.Writer
88+
8389
var Def => build.Default
90+
8491
func Sin => math.Sin
8592

8693
// type aliases denote identical types

0 commit comments

Comments
 (0)