Skip to content

Commit

Permalink
Revert "cmd/compile: improve error message if init is directly invoked"
Browse files Browse the repository at this point in the history
This reverts commit cb6e063.

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 golang#8481.

Change-Id: I49787fdf7738213370ae6f0cab54013e9e3394a8
Reviewed-on: https://go-review.googlesource.com/37876
Run-TryBot: Robert Griesemer <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
  • Loading branch information
griesemer committed Mar 6, 2017
1 parent a6bd42f commit cf71094
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 28 deletions.
16 changes: 4 additions & 12 deletions src/cmd/compile/internal/gc/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
14 changes: 0 additions & 14 deletions test/fixedbugs/issue8481.go

This file was deleted.

4 changes: 2 additions & 2 deletions test/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit cf71094

Please sign in to comment.