]> Cypherpunks repositories - gostls13.git/commitdiff
spec: clarify what is considered a function call for len/cap special case
authorRobert Griesemer <gri@golang.org>
Tue, 4 Mar 2014 04:07:34 +0000 (20:07 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 4 Mar 2014 04:07:34 +0000 (20:07 -0800)
gccgo considers built-in function calls returning a constant not as function call (issue 7386)
go/types considers any call (regular or built-in) as a function call

The wording and examples clarify that only "function calls" that are issued
at run-time (and thus do not result in a constant result) are considered
function calls in this case.

gc is inconsistent (issue 7385)
gccgo already interprets the spec accordingly and issue 7386 is moot.
go/types considers all calls (constant or not) as function calls (issue 7457).

Fixes #7387.
Fixes #7386.

LGTM=r, rsc, iant
R=r, rsc, iant, ken
CC=golang-codereviews
https://golang.org/cl/66860046

doc/go_spec.html

index 59b30739c64a74ba1af5965c8b9b6288b4cc96c5..9043431c4d1df1e687e425d18f3de76af5e9009f 100644 (file)
@@ -1,6 +1,6 @@
 <!--{
        "Title": "The Go Programming Language Specification",
-       "Subtitle": "Version of Feb 27, 2014",
+       "Subtitle": "Version of March 4, 2014",
        "Path": "/ref/spec"
 }-->
 
@@ -5271,12 +5271,22 @@ The expression <code>len(s)</code> is <a href="#Constants">constant</a> if
 <code>s</code> is a string constant. The expressions <code>len(s)</code> and
 <code>cap(s)</code> are constants if the type of <code>s</code> is an array
 or pointer to an array and the expression <code>s</code> does not contain
-<a href="#Receive_operator">channel receives</a> or
+<a href="#Receive_operator">channel receives</a> or (non-constant)
 <a href="#Calls">function calls</a>; in this case <code>s</code> is not evaluated.
 Otherwise, invocations of <code>len</code> and <code>cap</code> are not
 constant and <code>s</code> is evaluated.
 </p>
 
+<pre>
+const (
+       c1 = imag(2i)                    // imag(2i) = 2.0 is a constant
+       c2 = len([10]float64{2})         // [10]float64{2} contains no function calls
+       c3 = len([10]float64{c1})        // [10]float64{c1} contains no function calls
+       c4 = len([10]float64{imag(2i)})  // imag(2i) is a constant and no function call is issued
+       c5 = len([10]float64{imag(z)})   // invalid: imag(x) is a (non-constant) function call
+)
+var z complex128
+</pre>
 
 <h3 id="Allocation">Allocation</h3>