1
0
mirror of https://github.com/golang/go synced 2024-11-22 22:40:02 -07:00

spec: clarify when constant slice indices must be in range

This documents the status quo for most implementations,
with one exception: gc generates a run-time error for
constant but out-of-range indices when slicing a constant
string. See issue 7200 for a detailed discussion.

LGTM=r
R=r, rsc, iant, ken
CC=golang-codereviews
https://golang.org/cl/72160044
This commit is contained in:
Robert Griesemer 2014-03-06 17:11:13 -08:00
parent f4359afa7f
commit 6ffd235161

View File

@ -1,6 +1,6 @@
<!--{
"Title": "The Go Programming Language Specification",
"Subtitle": "Version of March 6, 2014",
"Subtitle": "Version of March 7, 2014",
"Path": "/ref/spec"
}-->
@ -2691,7 +2691,7 @@ For arrays or strings, the indices are <i>in range</i> if
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="#Constants">constant</a> index must be non-negative and representable by a value of type
<code>int</code>.
<code>int</code>; for arrays or constant strings, constant indices must also be in range.
If both indices are constant, they must satisfy <code>low &lt;= high</code>.
If the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
</p>
@ -2751,7 +2751,7 @@ If the sliced operand is an array, it must be <a href="#Address_operators">addre
The indices are <i>in range</i> if <code>0 &lt;= low &lt;= high &lt;= max &lt;= cap(a)</code>,
otherwise they are <i>out of range</i>.
A <a href="#Constants">constant</a> index must be non-negative and representable by a value of type
<code>int</code>.
<code>int</code>; for arrays, constant indices must also be in range.
If multiple indices are constant, the constants that are present must be in range relative to each
other.
If the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.