New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: Manually calling init should provide a better error message, currently says "undefined: init" #8481

Open
augustoroman opened this Issue Aug 6, 2014 · 12 comments

Comments

Projects
None yet
8 participants
@augustoroman
Contributor

augustoroman commented Aug 6, 2014

Attempting to manually call an init() function fails with the somewhat cryptic compiler
error "undefined: init".

It should indicate that init is a special symbol (automatically run) and cannot be
manually called.

http://play.golang.org/p/d2A2m56Fvp
@cznic

This comment has been minimized.

Contributor

cznic commented Aug 6, 2014

Comment 1:

From http://golang.org/ref/spec#Program_initialization_and_execution
""""
Variables may also be initialized using functions named init declared in the package
block, with no arguments and no result parameters.
func init() { … }
Multiple such functions may be defined, even within a single source file. The init
identifier is not declared and thus init functions cannot be referred to from anywhere
in a program.
""""
IMO, the error message reflects the specification well. As the identifier is not
declared, calling it is like calling any other undefined function. The identifier is
special, the error is not.
@davecheney

This comment has been minimized.

Contributor

davecheney commented Aug 6, 2014

Comment 2:

I agree that this is technically correct, however the fact you can't call init is a
byproduct of the implementation which renames init to prevent colissions if multiple
init functions are present. 
Although I cannot suggest appropriate wording, I think this error message could be
improved.

Labels changed: added release-none, repo-main.

Status changed to Accepted.

@cznic

This comment has been minimized.

Contributor

cznic commented Aug 6, 2014

Comment 3:

Well, then it should not be forgotten to, for consistency, do the same
(improve/specialize) for any other error message rooted in "referring to [init] from
anywhere in a program", which the call statement is not the only case.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@rsc rsc removed release-none labels Apr 10, 2015

@rsc rsc changed the title from cmd/gc: Manually calling init should provide a better error message, currently says "undefined: init" to cmd/compile: Manually calling init should provide a better error message, currently says "undefined: init" Jun 8, 2015

@odeke-em

This comment has been minimized.

Member

odeke-em commented Jan 1, 2017

I've mailed https://go-review.googlesource.com/c/34790 to reignite the conversation.

@gopherbot

This comment has been minimized.

gopherbot commented Jan 1, 2017

CL https://golang.org/cl/34790 mentions this issue.

@griesemer

This comment has been minimized.

Contributor

griesemer commented Mar 5, 2017

I just saw the fix and I think it's not correct. See my comment on the CL, but in short init is only special at package-level. It's perfectly fine to call a variable named init if it's value is a function.

While the error message originally was not very helpful (but correct), now the error message is misleading. I think this requires a more careful approach.

Reopening.

@griesemer griesemer reopened this Mar 5, 2017

@griesemer griesemer modified the milestones: Go1.9, Unplanned Mar 5, 2017

@gopherbot

This comment has been minimized.

gopherbot commented Mar 6, 2017

CL https://golang.org/cl/37876 mentions this issue.

@griesemer griesemer self-assigned this Mar 6, 2017

gopherbot pushed a commit that referenced this issue Mar 6, 2017

Revert "cmd/compile: improve error message if init is directly invoked"
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 #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>
@odeke-em

This comment has been minimized.

Member

odeke-em commented Mar 7, 2017

Thank you @griesemer for taking a look at it. Gotcha, I'll yield to someone else to work on it.

@stemar94

This comment has been minimized.

stemar94 commented May 24, 2017

I think a message like in the reverted patch such as "cannot refer to init function in user code" is fine, but we should first check if there actually is an init function at package level which the user is/could be referring to.
I could do this but would need a hint how to extract the information if there are any init functions in the package.

@griesemer

This comment has been minimized.

Contributor

griesemer commented Jun 6, 2017

Not urgent. Has been like this for a while. Quite possibly will be addressed by the front-end cleanup that's in the works.

@griesemer griesemer modified the milestones: Go1.10, Go1.9 Jun 6, 2017

@griesemer griesemer modified the milestones: Go1.10, Go1.11 Nov 29, 2017

@griesemer

This comment has been minimized.

Contributor

griesemer commented Apr 11, 2018

Non-urgent. Moving to 1.12.

@griesemer griesemer modified the milestones: Go1.11, Go1.12 Apr 11, 2018

@griesemer

This comment has been minimized.

Contributor

griesemer commented Oct 22, 2018

Still not urgent, and the error message is correct per spec. Moving to unplanned.

@griesemer griesemer modified the milestones: Go1.12, Unplanned Oct 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment