From 5473103666e6ddfb4c036cbc064447759b63d9d8 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 12 May 2011 09:15:59 -0700 Subject: [PATCH] go spec: clarify semantics of range clause This CL proposes some subtle language changes in an attempt to clarify the semantics of range clauses and simplify uses of maps. - nil maps behave like empty maps; but attempting to set a value in a nil map causes a run-time panic - nil channels are never ready for communication; sending or reading from a nil channel blocks forever - if there is only one index iteration variable in a range clause and len(range expression) would be a constant, the range expression is not evaluated. (was discrepancy with len/cap before) - the notion of what is a constant expression len(x) for (pointer to) arrays x has been generalized and simplified (can still be syntactically decided) (before: more restrictive syntactic rule that was not consistently implemented) Fixes #1713. R=r, rsc, iant, ken2, r2, bradfitz, rog CC=golang-dev https://golang.org/cl/4444050 --- doc/go_spec.html | 123 +++++++++++++++++++++++++++-------------------- 1 file changed, 72 insertions(+), 51 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 89ad2fae6c..8e2f0cddf5 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,5 +1,5 @@ - +

@@ -4439,17 +4460,17 @@ The implementation guarantees that the result always fits into an int
-Call      Argument type        Result
+Call      Argument type    Result
 
-len(s)    string type          string length in bytes
-          [n]T, *[n]T          array length (== n)
-          []T                  slice length
-          map[K]T              map length (number of defined keys)
-          chan T               number of elements queued in channel buffer
+len(s)    string type      string length in bytes
+          [n]T, *[n]T      array length (== n)
+          []T              slice length
+          map[K]T          map length (number of defined keys)
+          chan T           number of elements queued in channel buffer
 
-cap(s)    [n]T, *[n]T          array length (== n)
-          []T                  slice capacity
-          chan T               channel buffer capacity
+cap(s)    [n]T, *[n]T      array length (== n)
+          []T              slice capacity
+          chan T           channel buffer capacity
 

@@ -4467,20 +4488,17 @@ The length and capacity of a nil slice, map, or channel are 0.

-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. +The expression len(s) is constant if +s is a string constant. The expressions len(s) and +cap(s) are constants if the type of s is an array +or pointer to an array and the expression s does not contain +channel receives or +function calls; in this case s is not evaluated. +Otherwise, invocations of len and cap are not +constant and s is evaluated.

+

Allocation

@@ -5169,5 +5187,8 @@ The following minimal alignment properties are guaranteed:

Implementation differences - TODO