-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: devirtualization changes program behavior #52072
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
Comments
In Changing |
This is consistent with the specification of Defer statements and Selectors. For
|
So the [Update]: so the core of the problem is whether or not So this is a bug undoubtedly. |
You're right... I read the specs again. I think it should be like this: when defer is executed, it will first calculate the function address and parameters (if any), and then invoke the function before returns. The problem is that nil is encountered when getting the function address. Defer:
Calls:
|
For the But the outputs of the following program is package main
type I interface{ M() }
type T struct{
x int
}
func (t T) M() {
println(t.x)
}
func f() {
var t = &T{1}
var i I = t
defer i.M()
t.x = 2
return
}
func g() {
var t = &T{1}
var i I = t
f := i.M
defer f()
i = &T{2}
return
}
func main() {
f()
g()
} |
@mdempsky maybe we should not devirtualize if there's deref op? Or if there's a deref op, rewrite it to:
|
I'm under the impression this only affects If so, I'm fine just not devirtualizing defer/go calls at all. |
Change https://go.dev/cl/428495 mentions this issue: |
Moved from #38634 (comment).
What version of Go are you using (
go version
)?It looks this problem started from Go 1.16.
Does this issue reproduce with the latest release?
Yes
What did you do?
[edit]: another example:
What did you expect to see?
Same return results.
What did you see instead?
Different results.
Changing the
i
variable to global makes this inconsistency gone.The text was updated successfully, but these errors were encountered: