mirror of
https://github.com/golang/go
synced 2024-11-25 10:07:56 -07:00
spec: restrict when len(x) is constant
R=gri, iant, ken2, r CC=golang-dev https://golang.org/cl/1687047
This commit is contained in:
parent
971a04f1dc
commit
f4429181df
@ -527,9 +527,10 @@ A constant value is represented by an
|
|||||||
<a href="#String_literals">string</a> literal,
|
<a href="#String_literals">string</a> literal,
|
||||||
an identifier denoting a constant,
|
an identifier denoting a constant,
|
||||||
a <a href="#Constant_expressions">constant expression</a>, or
|
a <a href="#Constant_expressions">constant expression</a>, or
|
||||||
the result value of some built-in functions such as <code>unsafe.Sizeof</code>
|
the result value of some built-in functions such as
|
||||||
and <code>cap</code> or <code>len</code> applied to an array,
|
<code>unsafe.Sizeof</code> applied to any value,
|
||||||
<code>len</code> applied to a string constant,
|
<code>cap</code> or <code>len</code> applied to
|
||||||
|
<a href="#Length_and_capacity">some expressions</a>,
|
||||||
<code>real</code> and <code>imag</code> applied to a complex constant
|
<code>real</code> and <code>imag</code> applied to a complex constant
|
||||||
and <code>cmplx</code> applied to numeric constants.
|
and <code>cmplx</code> applied to numeric constants.
|
||||||
The boolean truth values are represented by the predeclared constants
|
The boolean truth values are represented by the predeclared constants
|
||||||
@ -754,8 +755,7 @@ ElementType = Type .
|
|||||||
The length is part of the array's type and must be a
|
The length is part of the array's type and must be a
|
||||||
<a href="#Constant_expressions">constant expression</a> that evaluates to a non-negative
|
<a href="#Constant_expressions">constant expression</a> that evaluates to a non-negative
|
||||||
integer value. The length of array <code>a</code> can be discovered
|
integer value. The length of array <code>a</code> can be discovered
|
||||||
using the built-in function <code>len(a)</code>, which is a
|
using the built-in function <code>len(a)</code>. The elements can be indexed by integer
|
||||||
compile-time constant. The elements can be indexed by integer
|
|
||||||
indices 0 through the <code>len(a)-1</code> (§<a href="#Indexes">Indexes</a>).
|
indices 0 through the <code>len(a)-1</code> (§<a href="#Indexes">Indexes</a>).
|
||||||
Array types are always one-dimensional but may be composed to form
|
Array types are always one-dimensional but may be composed to form
|
||||||
multi-dimensional types.
|
multi-dimensional types.
|
||||||
@ -805,7 +805,7 @@ a slice of length up to that capacity can be created by `slicing' a new
|
|||||||
one from the original slice (§<a href="#Slices">Slices</a>).
|
one from the original slice (§<a href="#Slices">Slices</a>).
|
||||||
The capacity of a slice <code>a</code> can be discovered using the
|
The capacity of a slice <code>a</code> can be discovered using the
|
||||||
built-in function <code>cap(a)</code> and the relationship between
|
built-in function <code>cap(a)</code> and the relationship between
|
||||||
<code>len()</code> and <code>cap()</code> is:
|
<code>len(a)</code> and <code>cap(a)</code> is:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
@ -4358,12 +4358,12 @@ The implementation guarantees that the result always fits into an <code>int</cod
|
|||||||
Call Argument type Result
|
Call Argument type Result
|
||||||
|
|
||||||
len(s) string type string length in bytes
|
len(s) string type string length in bytes
|
||||||
[n]T, *[n]T array length (== constant n)
|
[n]T, *[n]T array length (== n)
|
||||||
[]T slice length
|
[]T slice length
|
||||||
map[K]T map length (number of defined keys)
|
map[K]T map length (number of defined keys)
|
||||||
chan T number of elements queued in channel buffer
|
chan T number of elements queued in channel buffer
|
||||||
|
|
||||||
cap(s) [n]T, *[n]T array length (== constant n)
|
cap(s) [n]T, *[n]T array length (== n)
|
||||||
[]T slice capacity
|
[]T slice capacity
|
||||||
chan T channel buffer capacity
|
chan T channel buffer capacity
|
||||||
</pre>
|
</pre>
|
||||||
@ -4378,6 +4378,20 @@ At any time the following relationship holds:
|
|||||||
0 <= len(s) <= cap(s)
|
0 <= len(s) <= cap(s)
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
The expression
|
||||||
|
<code>len(s)</code> is a
|
||||||
|
<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 <code>s</code> is an (optionally parenthesized)
|
||||||
|
identifier or
|
||||||
|
<a href="#Qualified_identifiers">qualified identifier</a>
|
||||||
|
denoting an array or pointer to array.
|
||||||
|
Otherwise invocations of <code>len</code> and <code>cap</code> are not
|
||||||
|
constant.
|
||||||
|
</p>
|
||||||
|
|
||||||
<h3 id="Allocation">Allocation</h3>
|
<h3 id="Allocation">Allocation</h3>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user