-
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: incorrect handling of iota in 1.18 #52438
Comments
Original discussion of this issue can be found here: arnodel/golua#87 |
Here is a standalone program showing the problem. This program should not panic. It does not panic when compiled with Go 1.17 or earlier, or with gccgo. It does panic with Go 1.18. package main
const C1 = 0 + iota<<8
const C2 = 1 + iota<<8
func main() {
if C1 != 0 {
panic(C1)
}
if C2 != 1 {
panic(C2)
}
} |
@gopherbot Please open a backport to 1.18. This is an invalid compilation of a valid Go file. Earlier releases are unaffected. |
Backport issue(s) opened: #52441 (for 1.18). Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases. |
FWIW I tried to bisect this, and it blamed the commit where |
No need to bisect. This is due to a completely new type checker, so it won't be useful to pin-point to a single change. I've identified the bug and will have a fix in a little bit. This is clearly a bad bug; but only manifests itself when using iota outside a grouped constant declaration, twice. As a temporary work-around, you can change your code to: // OpOr is a logical or (precedence 0)
const (OpOr Op = 0 + iota<<8)
// OpAnd is a logical and (precedence 1)
const (OpAnd Op = 1 + iota<<8) (put parentheses around the const declarations). |
Change https://go.dev/cl/401134 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Using golua, 1.18.1 causes panics which did not occur on previous versions of go. I have narrowed down the issue to this source file which functions differently now. Running this program on 1.18.1 yields different results than 1.17.9, but according to the language spec, 1.18.1 is exhibiting incorrect behavior.
What did you expect to see?
In the above program, OpAnd should have a value of 1 (
1 + iota<<8
should be1 + (0)<<8
).What did you see instead?
On go 1.17.9, the value is indeed 1. However, on go 1.18.1, the value is 257, meaning that
iota
here is incorrectly being set to 1 instead of 0. The language spec says that each constant declaration should resetiota
to 0, which is not happening on 1.18.1.The text was updated successfully, but these errors were encountered: