-
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: package-level variable initialization order looks not correct #66585
Comments
CC @griesemer gccgo prints |
It looks like the type checker is computing the correct order. Here is the initorder trace for the above code:
|
Yes, this looks like cmd/compile/internal/staticinit not getting it right somehow. |
I'd like to dig further into this issue if no one else is claiming it. |
change to this var x = 0
var a = foo()
var b = x
func foo() int {
x = x + 1 // diferent
return x
}
func main() {
println(a, b)
// 1 1 # go1.22
// 1 0 # go1.21
} |
This is a bug in staticinit, where the compiler try optimizing:
to:
without recognizing that the optimization is unsafe, because foo was inlined. |
Change https://go.dev/cl/575175 mentions this issue: |
Change https://go.dev/cl/575216 mentions this issue: |
@randall77 Should we backport this issue? Though it's not a regression, it causes mis-compilation and make binary behaves wrongly. |
Change https://go.dev/cl/575336 mentions this issue: |
I don't think we should backport this. It is not a problem that any realistic program runs into, and it is easy to work around if it does. |
For #66585 Change-Id: Iddc407e3ef4c3b6ecf5173963b66b3e65e43c92d Reviewed-on: https://go-review.googlesource.com/c/go/+/575336 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Go version
go version go1.22.1 linux/amd64
Output of
go env
in your module/workspace:.
What did you do?
What did you see happen?
1 0
What did you expect to see?
1 1
The text was updated successfully, but these errors were encountered: