-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: assignments to _ are not evaluated for possible panics #5790
Comments
If you assign to an actual variable, then the program panics. The problem appears to be that because you are assigning to _, the expression is not evaluated. I suppose this ought to be fixed but it's hardly likely to be a problem in real code. Labels changed: added priority-someday, compilerbug, removed priority-triage. |
Looks like the expression is (at least partially) evaluated for int64 on 32 bit architecture. It came up while I was looking at dave's benchmark (agree, hardly a real code): for n := 0; n < b.N; n++ { for i, row := range a { for j, _ := range row { println(a[j][i]) _ = a[i][j] / a[j][i] } } } |
Ah, this is an unrelated arm ?runtime bug? func main() { a := 0 b := 1 / a _ = b } amd64: panic: runtime error: integer divide by zero [signal 0x8 code=0x1 addr=0x400c12 pc=0x400c12] goroutine 1 [running]: main.main() /tmp/main.go:7 +0x12 arm6: runtime: unknown pc 0x1 at frame 12 fatal error: invalid stack goroutine 1 [running]: [fp=0xb6f8fe70] runtime.gc(0x0) /d/go/src/pkg/runtime/mgc0.c:2017 +0x1b8 [fp=0xb6f8fea4] runtime.mallocgc(0x8, 0x0, 0x1, 0x1) /d/go/src/pkg/runtime/zmalloc_linux_arm.c:103 +0x1d0 ... |
The spec is not explicit about it, but there's no word why it should not be evaluated. For one, the function f in http://play.golang.org/p/3iGuytfm0F is called even though its result is assigned to _ . Assignment to _ permits "throwing away" values we don't care about, the values are still computed. Status changed to Accepted. |
Interestingly, while a, b := 1, 0 _ = a/b does not panic; the following does: a, b := 1, 0 _, _ = 1, a/b as does: a, b := 1, 0 x, _ := 1, a/b So gc is not consistent in this case. Again, I argue that the expressions on the rhs must be evaluated if there are observable side-effects (perhaps excluding execution time). |
@iant done https://golang.org/issue/5805 Panic in the runtime would be (sort of) consistent with the static check: a := 1 _ = a/0 ./main.go:7: division by zero |
At least for the original bug report in #5790 (comment), $ go version
go version devel +8887be4 Thu Dec 22 22:43:44 2016 +0000 darwin/amd64
$ cat main.go && go run main.go
package main
import "fmt"
func main() {
b := []int16{2, 0}
_ = b[0] / b[1]
var x int
fmt.Printf("still alive: %v\n", x)
}
panic: runtime error: integer divide by zero
goroutine 1 [running]:
main.main()
/Users/emmanuelodeke/Desktop/openSrc/bugs/golang/5790/main.go:7 +0xc6
exit status 2 /cc @randall77 and the other compiler folks, was this bug fixed as one of the I'll submit a CL to lock in this test/behavior, but for starters I've filed #18421 so that we can fix the condition |
CL https://golang.org/cl/34712 mentions this issue. |
Yes, this was probably fixed as a side effect of the SSA conversion. |
by capnm9:
The text was updated successfully, but these errors were encountered: