From: Robert Griesemer Date: Wed, 7 May 2014 15:50:52 +0000 (-0700) Subject: spec: remove evaluation order inconsistency X-Git-Tag: go1.3beta2~129 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=dbe5f88804ad974a8c98d67421c9aac302873359;p=gostls13.git spec: remove evaluation order inconsistency This is a clarification of what happens already. Not a language change. Fixes #7137. LGTM=iant, r, rsc R=r, rsc, iant, ken CC=golang-codereviews https://golang.org/cl/96000044 --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 496a7b2c3b..114ceed86f 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -3996,8 +3996,11 @@ precision.

Order of evaluation

-When evaluating the operands of an expression, -assignment, or +At package level, initialization dependencies +determine the evaluation order of individual initialization expressions in +variable declarations. +Otherwise, when evaluating the operands of an +expression, assignment, or return statement, all function calls, method calls, and communication operations are evaluated in lexical left-to-right @@ -4005,7 +4008,7 @@ order.

-For example, in the assignment +For example, in the (function-local) assignment

 y[f()], ok = g(h(), i()+x[j()], <-c), k()
@@ -4022,11 +4025,33 @@ of y is not specified.
 
 a := 1
 f := func() int { a++; return a }
-x := []int{a, f()} // x may be [1, 2] or [2, 2]: evaluation order between a and f() is not specified
-m := map[int]int{a: 1, a: 2} // m may be {2: 1} or {2: 2}: evaluation order between the two map assignments is not specified
-m2 := map[int]int{a: f()} // m2 may be {2: 3} or {3: 3}: evaluation order between the key and the value is not specified
+x := []int{a, f()}            // x may be [1, 2] or [2, 2]: evaluation order between a and f() is not specified
+m := map[int]int{a: 1, a: 2}  // m may be {2: 1} or {2: 2}: evaluation order between the two map assignments is not specified
+n := map[int]int{a: f()}      // n may be {2: 3} or {3: 3}: evaluation order between the key and the value is not specified
 
+

+At package level, initialization dependencies override the left-to-right rule +for individual initialization expressions, but not for operands within each +expression: +

+ +
+var a, b, c = f() + v(), g(), sqr(u()) + v()
+
+func f() int        { return c }
+func g() int        { return a }
+func sqr(x int) int { return x*x }
+
+// functions u and v are independent of all other variables and functions
+
+ +

+The function calls happen in the order +u(), sqr(), v(), +f(), v(), and g(). +

+

Floating-point operations within a single expression are evaluated according to the associativity of the operators. Explicit parentheses affect the evaluation