The program below tests the order-of-evaluation semantics of return *p, f(p). With gccgo, it prints 0 (i.e., evaluating *p before calling f(p)), and with cmd/compile it prints 2 (i.e., evaluating *p after f(p) returns).
But is it allowed to print 1? I.e., can *p be evaluated in the middle of f(p) being evaluated, after the *p = 1 and before the *p = 2?
The spec says simply:
For example, in the (function-local) assignment
y[f()], ok = g(h(), i()+x[j()], <-c), k()
the function calls and communication happen in the order f(), h(), i(), j(), <-c, g(), and k(). However, the order of those events compared to the evaluation and indexing of x and the evaluation of y is not specified.
I don't think there's any other text in the spec that requires evaluation of a function call to within an expression to be handled atomically with respect to other expressions either.
@griesemer and I agree only 0 or 2 should be allowed (i.e., 1 should not be allowed), but that the spec could be clearer about this.
package main
func main() {
x, _ := g()
println(x)
}
func g() (int, int) {
p := new(int)
return *p, f(p)
}
func f(p *int) int {
*p = 1
*p = 2
return 0
}
The program below tests the order-of-evaluation semantics of
return *p, f(p). With gccgo, it prints 0 (i.e., evaluating*pbefore callingf(p)), and with cmd/compile it prints 2 (i.e., evaluating*pafterf(p)returns).But is it allowed to print
1? I.e., can*pbe evaluated in the middle off(p)being evaluated, after the*p = 1and before the*p = 2?The spec says simply:
I don't think there's any other text in the spec that requires evaluation of a function call to within an expression to be handled atomically with respect to other expressions either.
@griesemer and I agree only 0 or 2 should be allowed (i.e., 1 should not be allowed), but that the spec could be clearer about this.