From: Robert Griesemer Date: Wed, 17 Oct 2012 18:08:42 +0000 (-0700) Subject: go spec: restrictions for index and slice expressions X-Git-Tag: go1.1rc2~2113 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ea7c57a03135118dde12cb27b735c48c24c88b37;p=gostls13.git go spec: restrictions for index and slice expressions At the moment, gc and gccgo report compile- time errors for certain constant indexes that are out of bounds. The spec however requests a run-time panic for out-of-bounds indexes (http://tip.golang.org/ref/spec#Indexes). Document the status quo. Fixes #4231. R=r, rsc, iant, ken CC=golang-dev https://golang.org/cl/6699048 --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 8248e8c696..dc08db991d 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -663,7 +663,7 @@ type literals. The static type (or just type) of a variable is the type defined by its declaration. Variables of interface type also have a distinct dynamic type, which -is the actual type of the value stored in the variable at run-time. +is the actual type of the value stored in the variable at run time. The dynamic type may vary during execution but is always assignable to the static type of the interface variable. For non-interface @@ -2495,15 +2495,29 @@ rules apply:

For a of type A or *A -where A is an array type, -or for a of type S where S is a slice type: +where A is an array type:

+ +

+For a of type S where S is a slice type: +

+

@@ -2511,12 +2525,15 @@ For a of type T where T is a string type:

@@ -2577,7 +2594,7 @@ a[low : high]

constructs a substring or slice. The index expressions low and high select which elements appear in the result. The result has -indexes starting at 0 and length equal to +indices starting at 0 and length equal to high - low. After slicing the array a

@@ -2610,9 +2627,13 @@ a[:] // same as a[0 : len(a)]

-For arrays or strings, the indexes low and high must -satisfy 0 <= low <= high <= length; for -slices, the upper bound is the capacity rather than the length. +For arrays or strings, the indices low and high are +in range if 0 <= low <= high <= len(a), +otherwise they are out of range. +For slices, the upper index bound is the slice capacity cap(a) rather than the length. +A constant index must not be negative, and if both indices +are constant, they must satisfy low <= high. If a is nil +or if the indices are out of range at run time, a run-time panic occurs.

@@ -2651,7 +2672,7 @@ If the type assertion holds, the value of the expression is the value stored in x and its type is T. If the type assertion is false, a run-time panic occurs. In other words, even though the dynamic type of x -is known only at run-time, the type of x.(T) is +is known only at run time, the type of x.(T) is known to be T in a correct program.

@@ -3604,7 +3625,7 @@ MyRunes("白鵬翔") // []rune{0x767d, 0x9d6c, 0x7fd4}

Constant expressions may contain only constant -operands and are evaluated at compile-time. +operands and are evaluated at compile time.