Commit 6ffd2351 authored by Robert Griesemer's avatar Robert Griesemer

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
parent f4359afa
<!--{
"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.
......
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