-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: reject var main in package main #21256
Comments
var main
panics
This is enough to trigger a runtime panic:
I guess the question here is if we want the compiler to catch bad |
I think the compiler should catch this. The language spec says that the package |
Change https://golang.org/cl/53450 mentions this issue: |
@ALTree, @ianlancetaylor I don't believe that is correct: The spec says that a program is created by linking a single, unimported package
and
it is possible to compile, link, and execute:
produces
as output. This is not a compiler problem, this is a linker issue. We (or I) have long maintained (from day one in fact) that the Only when we link does it matter because we need to get to an entry point. |
I think that is less true these days than it used to be. For example, the compiler rejects
|
@ianlancetaylor Fair enough, but I'm not convinced yet that we should erode this further. There's a lot of implicit assumptions here that are not really written down in the spec. I also don't see much technical reason. It's basically an additional special case in the compiler where there doesn't need to be one as far as I can tell. |
I was thinking the same objection as @griesemer yesterday: according to the Go spec, a package named "main" is not necessary the "main package". Though seeing as cmd/go doesn't actually support packages named "main" and the compiler already treats packages named "main" as the main package for purposes of validating the "main" function's signature, I'm sort of inclined to just accept adding a compiler error against declaring main.main as a non-function. |
I respect @griesemer 's idea, but honestly, as @mdempsky says, I also regard |
@mdempsky I'm not per se against that but it would be an error that is not backed by the spec. As it is, the implementation is inconsistent. My suggestion: Let's see if the linker fix is trivial and if so, let's do it there. Otherwise, let's go back to the compiler. The main goal is to provide an good error message to the user rather than a crash. Also, this is really an esoteric issue, so not worth spending a lot of engineering complexity on it. |
@griesemer Hi, I tried to fix this issue from the aspect of |
Change https://golang.org/cl/54330 mentions this issue: |
btw, for good or bad, the issue makes it is possible that a Go program can run okay without a main entry function. package main
import (
"fmt"
"time"
)
var main int
func init() {
for {
time.Sleep(time.Second)
fmt.Println("aaa")
}
} |
The compiler should be the one to reject this. As Ian noted it already checks func main's signature in package main. Package main is special. |
Change https://golang.org/cl/79435 mentions this issue: |
What version of Go are you using?
1.8
What operating system and processor architecture are you using?
The Go Playground
What did you do?
https://play.golang.org/p/HBKmQFBAuv
What did you expect to see?
Either a compile-time error telling me that
main
must be a function (not a variable), or for it to work. The spec says:I didn't expect
func() main {}
andvar main = func() {}
to behave differently, but I understand that the latter isn't actually a function declaration.What did you see instead?
The text was updated successfully, but these errors were encountered: