Commit 39067062 authored by Robert Griesemer's avatar Robert Griesemer

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
parent 17b3766d
<!--{ <!--{
"Title": "The Go Programming Language Specification", "Title": "The Go Programming Language Specification",
"Subtitle": "Version of December 10, 2012", "Subtitle": "Version of December 12, 2012",
"Path": "/ref/spec" "Path": "/ref/spec"
}--> }-->
...@@ -815,12 +815,13 @@ ElementType = Type . ...@@ -815,12 +815,13 @@ ElementType = Type .
</pre> </pre>
<p> <p>
The length is part of the array's type and must be a The length is part of the array's type; it must evaluate to a non-
<a href="#Constant_expressions">constant expression</a> that evaluates to a non-negative negative <a href="#Constants">constant</a> representable by a value
integer value. The length of array <code>a</code> can be discovered 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>. 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> 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 Array types are always one-dimensional but may be composed to form
multi-dimensional types. multi-dimensional types.
</p> </p>
...@@ -2497,13 +2498,21 @@ The value <code>x</code> is called the ...@@ -2497,13 +2498,21 @@ The value <code>x</code> is called the
rules apply: rules apply:
</p> </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 &lt;= x &lt; 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> <p>
For <code>a</code> of type <code>A</code> or <code>*A</code> 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>: where <code>A</code> is an <a href="#Array_types">array type</a>:
</p> </p>
<ul> <ul>
<li><code>x</code> must be an integer value; it is <i>in range</i> if <code>0 &lt;= x &lt; 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>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, <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> 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>: ...@@ -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>: For <code>a</code> of type <code>S</code> where <code>S</code> is a <a href="#Slice_types">slice type</a>:
</p> </p>
<ul> <ul>
<li><code>x</code> must be an integer value; it is <i>in range</i> if <code>0 &lt;= x &lt; 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, <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> 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 <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> ...@@ -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>: where <code>T</code> is a <a href="#String_types">string type</a>:
</p> </p>
<ul> <ul>
<li><code>x</code> must be an integer value; it is <i>in range</i> if <code>0 &lt;= x &lt; len(a)</code>, <li>a <a href="#Constants">constant</a> index must be in range
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
if the string <code>a</code> is also constant</li> if the string <code>a</code> is also constant</li>
<li>if <code>x</code> is out of range at run time, <li>if <code>x</code> is out of range at run time,
a <a href="#Run_time_panics">run-time panic</a> occurs</li> 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 ...@@ -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 &lt;= <code>low</code> &lt;= <code>high</code> &lt;= len(a)</code>, <i>in range</i> if <code>0 &lt;= <code>low</code> &lt;= <code>high</code> &lt;= len(a)</code>,
otherwise they are <i>out of range</i>. 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. 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 &lt;= high</code>. If <code>a</code> is <code>nil</code> are constant, they must satisfy <code>low &lt;= 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. or if the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
</p> </p>
...@@ -4985,8 +4991,9 @@ make(T, n) channel asynchronous channel of type T, buffer size n ...@@ -4985,8 +4991,9 @@ make(T, n) channel asynchronous channel of type T, buffer size n
<p> <p>
The size arguments <code>n</code> and <code>m</code> must be integer values. 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 A <a href="#Constants">constant</a> size argument must be non-negative and
if both <code>n</code> and <code>m</code> are provided and are constant, then 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>. <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, 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. 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. ...@@ -4995,6 +5002,7 @@ a <a href="#Run_time_panics">run-time panic</a> occurs.
<pre> <pre>
s := make([]int, 10, 100) // slice with len(s) == 10, cap(s) == 100 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, 1e3) // slice with len(s) == cap(s) == 1000
s := make([]int, 1&lt;&lt;63) // illegal: len(s) is not representable by a value of type int
s := make([]int, 10, 0) // illegal: len(s) > cap(s) s := make([]int, 10, 0) // illegal: len(s) > cap(s)
c := make(chan int, 10) // channel with a buffer size of 10 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 m := make(map[string]int, 100) // map with initial space for 100 elements
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment