From 5419f652b6593a0916f7b2155435b945e8ee0fb4 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 23 Apr 2024 17:22:02 -0700 Subject: spec: clarify prose for range over numeric range expressions Fixes #66967. Change-Id: I7b9d62dcb83bad60b2ce74e2e2bf1a36c6a8ae38 Reviewed-on: https://go-review.googlesource.com/c/go/+/581256 Reviewed-by: Robert Griesemer Reviewed-by: Ian Lance Taylor TryBot-Bypass: Robert Griesemer --- doc/go_spec.html | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'doc') diff --git a/doc/go_spec.html b/doc/go_spec.html index f5069f62d6..ac27c1d6c1 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -6656,13 +6656,13 @@ if the respective iteration variables are present:

-Range expression                          1st value          2nd value
+Range expression                                   1st value                2nd value
 
-array or slice  a  [n]E, *[n]E, or []E    index    i  int    a[i]       E
-string          s  string type            index    i  int    see below  rune
-map             m  map[K]V                key      k  K      m[k]       V
-channel         c  chan E, <-chan E       element  e  E
-integer         n  integer type           value    i  see below
+array or slice  a  [n]E, *[n]E, or []E             index    i  int          a[i]       E
+string          s  string type                     index    i  int          see below  rune
+map             m  map[K]V                         key      k  K            m[k]       V
+channel         c  chan E, <-chan E                element  e  E
+integer value   n  integer type, or untyped int    value    i  see below
 
    @@ -6703,8 +6703,17 @@ is nil, the range expression blocks forever.
  1. -For an integer value n, the iteration values 0 through n-1 +For an integer value n, where n is of integer type +or an untyped integer constant, the iteration values 0 through n-1 are produced in increasing order. +If n is of integer type, the iteration values have that same type. +Otherwise, the type of n is determined as if it were assigned to the +iteration variable. +Specifically: +if the iteration variable is preexisting, the type of the iteration values is the type of the iteration +variable, which must be of integer type. +Otherwise, if the iteration variable is declared by the "range" clause or is absent, +the type of the iteration values is the default type for n. If n <= 0, the loop does not run any iterations.
@@ -6716,11 +6725,7 @@ The iteration variables may be declared by the "range" clause using a form of In this case their scope is the block of the "for" statement and each iteration has its own new variables [Go 1.22] (see also "for" statements with a ForClause). -If the range expression is a (possibly untyped) integer expression n, -the variable has the same type as if it was -declared with initialization -expression n. -Otherwise, the variables have the types of their respective iteration values. +The variables have the types of their respective iteration values.

@@ -6728,9 +6733,6 @@ If the iteration variables are not explicitly declared by the "range" clause, they must be preexisting. In this case, the iteration values are assigned to the respective variables as in an assignment statement. -If the range expression is a (possibly untyped) integer expression n, -n too must be assignable to the iteration variable; -if there is no iteration variable, n must be assignable to int.

@@ -6778,6 +6780,10 @@ for i := range 10 {
 var u uint8
 for u = range 256 {
 }
+
+// invalid: 1e3 is a floating-point constant
+for range 1e3 {
+}
 
-- cgit v1.2.3-54-g00ecf