Skip to content
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: compiler allows unreachable statements #12920

Closed
shoenig opened this issue Oct 13, 2015 · 3 comments
Closed

cmd/compile: compiler allows unreachable statements #12920

shoenig opened this issue Oct 13, 2015 · 3 comments

Comments

@shoenig
Copy link
Contributor

shoenig commented Oct 13, 2015

I noticed this in some badly re-factored complex code, but it reduces nicely into this toy program. Basically the compiler allows unreachable statements that could be detected statically at compile time. It would be nice if code like this failed to compile.

package main

import "fmt"

func main() {
    fmt.Println("foo", foo())
}

func foo() int {
    fmt.Println("now you see me")
    return 0
    fmt.Println("now you don't")
    return 1
}
@mdempsky
Copy link
Member

While questionable, that's technically valid Go 1 code, so it has to be accepted by the compiler. It's also already caught by cmd/vet:

$ go vet issue12920.go
issue12920.go:12: unreachable code

@ianlancetaylor
Copy link
Contributor

Let me add that to try to ensure that all Go compilers agree on which programs can be compiled, the notion of an unreachable statement is actually defined by the language spec: https://golang.org/ref/spec#Terminating_statements .

@minux
Copy link
Member

minux commented Oct 13, 2015 via email

@golang golang locked and limited conversation to collaborators Oct 12, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants