mirror of
https://github.com/golang/go
synced 2024-11-22 03:14:41 -07:00
spec: index and array/slice size constants must fit into an int
R=r, rsc, iant, ken CC=golang-dev https://golang.org/cl/6903048
This commit is contained in:
parent
17b3766d18
commit
3906706297
@ -1,6 +1,6 @@
|
||||
<!--{
|
||||
"Title": "The Go Programming Language Specification",
|
||||
"Subtitle": "Version of December 10, 2012",
|
||||
"Subtitle": "Version of December 12, 2012",
|
||||
"Path": "/ref/spec"
|
||||
}-->
|
||||
|
||||
@ -815,12 +815,13 @@ ElementType = Type .
|
||||
</pre>
|
||||
|
||||
<p>
|
||||
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
|
||||
integer value. The length of array <code>a</code> can be discovered
|
||||
The length is part of the array's type; it must evaluate to a non-
|
||||
negative <a href="#Constants">constant</a> representable by a value
|
||||
of type <code>int</code>.
|
||||
The length of array <code>a</code> can be discovered
|
||||
using the built-in function <a href="#Length_and_capacity"><code>len</code></a>.
|
||||
The elements can be addressed by integer <a href="#Index_expressions">indices</a>
|
||||
indices 0 through <code>len(a)-1</code>.
|
||||
0 through <code>len(a)-1</code>.
|
||||
Array types are always one-dimensional but may be composed to form
|
||||
multi-dimensional types.
|
||||
</p>
|
||||
@ -2497,13 +2498,21 @@ The value <code>x</code> is called the
|
||||
rules apply:
|
||||
</p>
|
||||
|
||||
<p>
|
||||
If <code>a</code> is not a map:
|
||||
</p>
|
||||
<ul>
|
||||
<li>the index <code>x</code> must be an integer value; it is <i>in range</i> if <code>0 <= x < len(a)</code>,
|
||||
otherwise it is <i>out of range</i></li>
|
||||
<li>a <a href="#Constants">constant</a> index must be non-negative
|
||||
and representable by a value of type <code>int</code>
|
||||
</ul>
|
||||
|
||||
<p>
|
||||
For <code>a</code> of type <code>A</code> or <code>*A</code>
|
||||
where <code>A</code> is an <a href="#Array_types">array type</a>:
|
||||
</p>
|
||||
<ul>
|
||||
<li><code>x</code> must be an integer value; it is <i>in range</i> if <code>0 <= x < len(a)</code>,
|
||||
otherwise it is <i>out of range</i></li>
|
||||
<li>a <a href="#Constants">constant</a> index must be in range</li>
|
||||
<li>if <code>a</code> is <code>nil</code> or if <code>x</code> is out of range at run time,
|
||||
a <a href="#Run_time_panics">run-time panic</a> occurs</li>
|
||||
@ -2515,9 +2524,6 @@ where <code>A</code> is an <a href="#Array_types">array type</a>:
|
||||
For <code>a</code> of type <code>S</code> where <code>S</code> is a <a href="#Slice_types">slice type</a>:
|
||||
</p>
|
||||
<ul>
|
||||
<li><code>x</code> must be an integer value; it is <i>in range</i> if <code>0 <= x < len(a)</code>,
|
||||
otherwise it is <i>out of range</i></li>
|
||||
<li>a <a href="#Constants">constant</a> index must not be negative</li>
|
||||
<li>if the slice is <code>nil</code> or if <code>x</code> is out of range at run time,
|
||||
a <a href="#Run_time_panics">run-time panic</a> occurs</li>
|
||||
<li><code>a[x]</code> is the slice element at index <code>x</code> and the type of
|
||||
@ -2529,9 +2535,7 @@ For <code>a</code> of type <code>T</code>
|
||||
where <code>T</code> is a <a href="#String_types">string type</a>:
|
||||
</p>
|
||||
<ul>
|
||||
<li><code>x</code> must be an integer value; it is <i>in range</i> if <code>0 <= x < len(a)</code>,
|
||||
otherwise it is <i>out of range</i></li>
|
||||
<li>a <a href="#Constants">constant</a> index must not be negative, and it must be in range
|
||||
<li>a <a href="#Constants">constant</a> index must be in range
|
||||
if the string <code>a</code> is also constant</li>
|
||||
<li>if <code>x</code> is out of range at run time,
|
||||
a <a href="#Run_time_panics">run-time panic</a> occurs</li>
|
||||
@ -2635,7 +2639,9 @@ For arrays or strings, the indices <code>low</code> and <code>high</code> are
|
||||
<i>in range</i> if <code>0 <= <code>low</code> <= <code>high</code> <= len(a)</code>,
|
||||
otherwise they are <i>out of range</i>.
|
||||
For slices, the upper index bound is the slice capacity <code>cap(a)</code> rather than the length.
|
||||
A <a href="#Constant_expressions">constant</a> index must not be negative, and if both indices
|
||||
A <a href="#Constants">constant</a> index must be non-negative and representable by a value of type
|
||||
<code>int</code>.
|
||||
If both indices
|
||||
are constant, they must satisfy <code>low <= high</code>. If <code>a</code> is <code>nil</code>
|
||||
or if the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
|
||||
</p>
|
||||
@ -4985,8 +4991,9 @@ make(T, n) channel asynchronous channel of type T, buffer size n
|
||||
|
||||
<p>
|
||||
The size arguments <code>n</code> and <code>m</code> must be integer values.
|
||||
A <a href="#Constants">constant</a> size argument must not be negative, and
|
||||
if both <code>n</code> and <code>m</code> are provided and are constant, then
|
||||
A <a href="#Constants">constant</a> size argument must be non-negative and
|
||||
representable by a value of type <code>int</code>.
|
||||
If both <code>n</code> and <code>m</code> are provided and are constant, then
|
||||
<code>n</code> must be no larger than <code>m</code>.
|
||||
If <code>n</code> is negative or larger than <code>m</code> at run time,
|
||||
a <a href="#Run_time_panics">run-time panic</a> occurs.
|
||||
@ -4995,6 +5002,7 @@ a <a href="#Run_time_panics">run-time panic</a> occurs.
|
||||
<pre>
|
||||
s := make([]int, 10, 100) // slice with len(s) == 10, cap(s) == 100
|
||||
s := make([]int, 1e3) // slice with len(s) == cap(s) == 1000
|
||||
s := make([]int, 1<<63) // illegal: len(s) is not representable by a value of type int
|
||||
s := make([]int, 10, 0) // illegal: len(s) > cap(s)
|
||||
c := make(chan int, 10) // channel with a buffer size of 10
|
||||
m := make(map[string]int, 100) // map with initial space for 100 elements
|
||||
|
Loading…
Reference in New Issue
Block a user