-
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
language: go and gccgo has different behavior when returning multiple values #36430
Comments
The Go language does not fully specify the order of evaluation. With regard to this test case, in the CC @griesemer @mdempsky for a second opinion. |
Although it is not a bug, it is interesting that the behaviors between standard and short variable declarations are different (for gc): package main
import "fmt"
func f(p *int) int {
*p = 123
return *p
}
func bar() {
var x int
y, z := x, f(&x)
fmt.Println(y, z) // 123 123
}
func baz() {
var x int
var y, z = x, f(&x)
fmt.Println(y, z) // 0 123
}
func main() {
bar()
baz()
} |
Given:
I think the result is unambiguously
Edit: Oh, but since the original example uses Better assignment example is:
Or:
Agreed this is a legitimate difference in implementation specific behavior. |
@go101 Your examples showing discrepancy between normal and short variable initialization are interesting and probably worth fixing, but I think would be best as a separate issue. |
I'm going to go ahead and close as Working As Intended, since @ianlancetaylor and I are in agreement on that. |
Thanks for looking into this! |
The behavior of
The spec requires the rhs is evaluated first. @go101 want to fire an issue 😄 |
@cuonglm The RHS has to be fully evaluated before any assignments happen to the LHS, but it's not specified whether For example,
can be validly compiled as:
|
Yes right, but I mean the current
|
Fair point, and I think that hints to me why the behavior is different between @go101 's test cases. But it's also just an implementation detail. Compilers are allowed to freely reorder/combine statements and operations, as long as the program visible behavior is correct. Eg, assigning to |
What version of Go are you using (
go version
)?And for gccgo:
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?Arch Linux on 64-bit x86
go env
OutputWhat did you do?
In connection with simplebolt, I discovered that the test passed when running with
go
, but failed when running withgccgo
. I shrunk it down to this small test case:When running this, it outputs "Compiled with gccgo" when compiled with
gccgo
and "Compiled with go" when compiled withgo
.Also available at play.golang.org
What did you expect to see?
I would expect to see the same output for both
go
andgccgo
. I don't know which compilation is "correct".What did you see instead?
Different output for
go
andgccgo
.The text was updated successfully, but these errors were encountered: