FAQ
Reviewers: r, rsc, iant, ken2,

Message:
Hello r@golang.org, rsc@golang.org, iant@golang.org, ken@golang.org (cc:
golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/go


Description:
spec: constant indexes and array/slice sizes must fit into int

Replacement CL for CL 6899045.

Please review this at https://codereview.appspot.com/6903048/

Affected files:
M doc/go_spec.html


Index: doc/go_spec.html
===================================================================
--- a/doc/go_spec.html
+++ b/doc/go_spec.html
@@ -1,6 +1,6 @@
<!--{
"Title": "The Go Programming Language Specification",
- "Subtitle": "Version of December 6, 2012",
+ "Subtitle": "Version of December 7, 2012",
"Path": "/ref/spec"
}-->

@@ -815,11 +815,11 @@
</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 non-
+negative <a href="#Constants">constant</a> that fits into an int.
+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 indexed by integer
+The elements can be addressed by integer
indices 0 through <code>len(a)-1</code> (§<a href="#Indexes">Indexes</a>).
Array types are always one-dimensional but may be composed to form
multi-dimensional types.
@@ -2498,12 +2498,19 @@
</p>

<p>
-For <code>a</code> of type <code>A</code> or <code>*A</code>
+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 not be negative and
fit into an <code>int</code>
+</ul>
+
+<p>
+Additionally, 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 &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>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 +2522,6 @@
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 &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,
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 +2533,7 @@
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 &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, 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 +2637,8 @@
<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>.
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 not be negative and fit
into an <code>int</code>.
+If both indices
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.
</p>
@@ -3359,7 +3362,7 @@
<pre>
t.Mv(7)
T.Mv(t, 7)
-(T).Mv(t, t)
+(T).Mv(t, 7)
f1 := T.Mv; f1(t, 7)
f2 := (T).Mv; f2(t, 7)
</pre>
@@ -4985,8 +4988,9 @@

<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 not be negative and
+fit into an <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 +4999,7 @@
<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&lt;&lt;63) // illegal: len(s) does not fit into
an 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

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 7, '12 at 6:11p
activeDec 7, '12 at 6:11p
posts1
users1
websitegolang.org

1 user in discussion

Gri: 1 post

People

Translate

site design / logo © 2022 Grokbase