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: _ = 1/x optimized away even though 1/x may panic #8613

Closed
bradfitz opened this issue Aug 29, 2014 · 14 comments
Closed

cmd/compile: _ = 1/x optimized away even though 1/x may panic #8613

bradfitz opened this issue Aug 29, 2014 · 14 comments

Comments

@bradfitz
Copy link
Contributor

Does the spec specify whether an expression assigned to _ needs to be evaluated and thus
might panic?

See: http://play.golang.org/p/azEfUMu2Vg

This doesn't panic, even though I would expect it to:

package main

var x int

func main() {
    defer func() {
        if e := recover(); e != nil {
            println("Panic")
        }
    }()
    v := 1
    _ = 5 / (1 - v)
    // x = 5 / (1 - v)   // This actually panics.
    println("No panic")
}
@minux
Copy link
Member

minux commented Aug 29, 2014

Comment 1:

if i use _ = f(), then f() is actually called, so I'd expect
5/(1-v) to be also evaluated (and panic) for consistency.
http://play.golang.org/p/qPMDjks3So

@griesemer
Copy link
Contributor

Comment 2:

The spec doesn't say that assignments to _ can be "optimized away". It's an assignment
like any other. It just says that the value, once assigned to a _, cannot be  recovered
from the _. That said, perhaps we should be more explicit about it,
An implementation is free to optimize as much as it wants as long as there's no semantic
change. If an expression panics upon evaluation, it needs to keep panicking after the
optimization.
I believe we've had issues with this in the past (_ = f()). I'd say this is an
implementation bug. Always good to check what gccgo does.

@bradfitz
Copy link
Contributor Author

Comment 3:

So division can be skipped, but slicing out of bounds seems to still panic:
http://play.golang.org/p/VISwXHnZQ_
func main() {
    defer func() {
        if e := recover(); e != nil {
            println("Panic")
        }
    }()
    i := 1
    s := make([]byte, i + 1)
    _ = 1 + s[5]
    println("No panic")
}

@griesemer
Copy link
Contributor

Comment 4:

Looks like a bug in gc to me:
http://play.golang.org/p/B3T7Wt8GFq
division appears to be ignored as possible source of panics when it comes to the check
whether the rhs can be "optimized away".
Changing to a compiler bug.

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

Owner changed to ---.

Status changed to Accepted.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@rsc rsc changed the title cmd/gc: _ = 1/x optimized away even though 1/x may panic cmd/compile: _ = 1/x optimized away even though 1/x may panic Jun 8, 2015
@bradfitz
Copy link
Contributor Author

Does anybody know whether this has been fixed?

/cc @josharian @randall77 @mdempsky

@randall77
Copy link
Contributor

This is fixed in the SSA backend. It is not fixed for the legacy compiler (aka all the other architectures besides amd64).

@griesemer
Copy link
Contributor

Doesn't look like it's fixed:

package main

func main() {
    f(0)
}

func f(x int) {
    _ = 1 / x
}

runs w/o complaints.

@bradfitz
Copy link
Contributor Author

bradfitz commented Mar 31, 2016 via email

@randall77
Copy link
Contributor

I don't think there are any. It was fixed by happenstance, not deliberately. We could add a +build amd64 test.

@cespare
Copy link
Contributor

cespare commented Mar 31, 2016

@griesemer that program does panic for me at tip.

@bradfitz
Copy link
Contributor Author

@griesemer, yeah... I see it correctly failing at tip:

bradfitz@laptop ~$ GOROOT=$HOME/go1.6 go run div.go
bradfitz@laptop ~$ GOROOT=$HOME/go go run div.go
panic: runtime error: integer divide by zero

goroutine 1 [running]:
panic(0x4b660, 0xc82000a0c0)
        /Users/bradfitz/go/src/runtime/panic.go:500 +0x18e
main.main()
        /Users/bradfitz/div.go:4 +0x14
exit status 2

@bradfitz
Copy link
Contributor Author

@gopherbot
Copy link

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

@griesemer
Copy link
Contributor

My apologies. I thought I had an updated build but obviously I didn't (at home).

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

7 participants