golang/go

cmd/compile: mis-calculates a constant

dvyukov opened this issue · 2 comments

For the following program:

package main
var f = 7 / "0"[0]

go tool compile says:

tmp.go:2: constant 1197 overflows byte

Now sure where 1197 came from, but it must be 0 and the program must compile successfully.

go version devel +514014c Thu Jun 18 15:54:35 2015 +0200 linux/amd64

I've looked into this a bit, and that particular value is coming from gc.Umagic:

ord("0") = 48
Umagic(48) = 171 with shift of 5
7 * 171 = 1197

In gc.walkdiv there is different logic used depending on if the divisor is a power of two or not, so it sometimes works:

https://play.golang.org/p/m4lGOWIADg

Same root cause as #11358

https://go-review.googlesource.com/11400

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