From f4429181df814edefb122f7c2a4a4e093d52ff71 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 1 Jul 2010 17:49:47 -0700 Subject: [PATCH] spec: restrict when len(x) is constant R=gri, iant, ken2, r CC=golang-dev https://golang.org/cl/1687047 --- doc/go_spec.html | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index f296c2a38ec..f8c5c0594da 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -527,9 +527,10 @@ A constant value is represented by an string literal, an identifier denoting a constant, a constant expression, or -the result value of some built-in functions such as unsafe.Sizeof -and cap or len applied to an array, -len applied to a string constant, +the result value of some built-in functions such as +unsafe.Sizeof applied to any value, +cap or len applied to +some expressions, real and imag applied to a complex constant and cmplx applied to numeric constants. The boolean truth values are represented by the predeclared constants @@ -754,8 +755,7 @@ ElementType = Type . The length is part of the array's type and must be a constant expression that evaluates to a non-negative integer value. The length of array a can be discovered -using the built-in function len(a), which is a -compile-time constant. The elements can be indexed by integer +using the built-in function len(a). The elements can be indexed by integer indices 0 through the len(a)-1Indexes). Array types are always one-dimensional but may be composed to form multi-dimensional types. @@ -805,7 +805,7 @@ a slice of length up to that capacity can be created by `slicing' a new one from the original slice (§Slices). The capacity of a slice a can be discovered using the built-in function cap(a) and the relationship between -len() and cap() is: +len(a) and cap(a) is:

@@ -4358,12 +4358,12 @@ The implementation guarantees that the result always fits into an int
@@ -4378,6 +4378,20 @@ At any time the following relationship holds:
 0 <= len(s) <= cap(s)
 
+

+The expression +len(s) is a +constant if s is a string constant. +The expressions +len(s) and +cap(s) are +constants if s is an (optionally parenthesized) +identifier or +qualified identifier +denoting an array or pointer to array. +Otherwise invocations of len and cap are not +constant. +

Allocation