]> Cypherpunks repositories - gostls13.git/commitdiff
Revert "cmd/compile: improve error message if init is directly invoked"
authorRobert Griesemer <gri@golang.org>
Mon, 6 Mar 2017 23:41:56 +0000 (15:41 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 6 Mar 2017 23:48:37 +0000 (23:48 +0000)
This reverts commit cb6e0639fb090ea0e129b1ddb956a7e645cff285.

The fix is incorrect as it's perfectly fine to refer to an
identifier 'init' inside a function, and 'init' may even be
a variable of function value. Misspelling 'init' in that
context would lead to an incorrect error message.

Reopened #8481.

Change-Id: I49787fdf7738213370ae6f0cab54013e9e3394a8
Reviewed-on: https://go-review.googlesource.com/37876
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue8481.go [deleted file]
test/init.go

index 180bd70cc1ab69dd00de119a17f187772d460bd9..f49c4dd036b01e07d458c244d6245572f0ae6b07 100644 (file)
@@ -3667,19 +3667,11 @@ func typecheckdef(n *Node) *Node {
                                lineno = n.Pos
                        }
 
-                       switch n.Sym.Name {
-                       case "init":
-                               // As per the spec at:
-                               //  https://golang.org/ref/spec#Program_initialization_and_execution
-                               // init cannot be referred to in usercode.
-                               // See https://golang.org/issues/8481.
-                               yyerror("cannot refer to init functions")
-                       default:
-                               // Note: adderrorname looks for this string and
-                               // adds context about the outer expression
-                               yyerror("undefined: %v", n.Sym)
-                       }
+                       // Note: adderrorname looks for this string and
+                       // adds context about the outer expression
+                       yyerror("undefined: %v", n.Sym)
                }
+
                return n
        }
 
diff --git a/test/fixedbugs/issue8481.go b/test/fixedbugs/issue8481.go
deleted file mode 100644 (file)
index a692966..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-// errorcheck
-
-// Copyright 2016 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 main
-
-func init() {
-}
-
-func main() {
-       init() // ERROR "cannot refer to init functions"
-}
index 1855b4ff56da2c57a3c0a24b59fe48281963ac73..f4689443cf1415be71abe719d0c89aaebdecba76 100644 (file)
@@ -15,7 +15,7 @@ func init() {
 }
 
 func main() {
-       init()         // ERROR "cannot refer to init functions"
+       init()         // ERROR "undefined.*init"
        runtime.init() // ERROR "unexported.*runtime\.init"
-       var _ = init   // ERROR "cannot refer to init functions"
+       var _ = init   // ERROR "undefined.*init"
 }