Skip to content

Commit 41d6687

Browse files
committed
spec: clarify iteration variable type for range over integer
Also: report language version (plus date) in spec header. Fixes #65137. Change-Id: I4f1d220d5922c40a36264df2d0a7bb7cd0756bac Reviewed-on: https://go-review.googlesource.com/c/go/+/557596 TryBot-Bypass: Robert Griesemer <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent c2dbbe4 commit 41d6687

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

doc/go_spec.html

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of Dec 27, 2023",
3+
"Subtitle": "Language version go1.22 (Jan 30, 2023)",
44
"Path": "/ref/spec"
55
}-->
66

@@ -6661,7 +6661,7 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
66616661
string s string type index i int see below rune
66626662
map m map[K]V key k K m[k] V
66636663
channel c chan E, &lt;-chan E element e E
6664-
integer n integer type I value i I
6664+
integer n integer type value i see below
66656665
</pre>
66666666

66676667
<ol>
@@ -6703,26 +6703,33 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
67036703

67046704
<li>
67056705
For an integer value <code>n</code>, the iteration values 0 through <code>n-1</code>
6706-
are produced in increasing order, with the same type as <code>n</code>.
6706+
are produced in increasing order.
67076707
If <code>n</code> &lt= 0, the loop does not run any iterations.
67086708
</li>
67096709
</ol>
67106710

6711-
<p>
6712-
The iteration values are assigned to the respective
6713-
iteration variables as in an <a href="#Assignment_statements">assignment statement</a>.
6714-
</p>
6715-
67166711
<p>
67176712
The iteration variables may be declared by the "range" clause using a form of
67186713
<a href="#Short_variable_declarations">short variable declaration</a>
67196714
(<code>:=</code>).
6720-
In this case their types are set to the types of the respective iteration values
6721-
and their <a href="#Declarations_and_scope">scope</a> is the block of the "for" statement;
6722-
each iteration has its own separate variables [<a href="#Go_1.22">Go 1.22</a>]
6715+
In this case their <a href="#Declarations_and_scope">scope</a> is the block of the "for" statement
6716+
and each iteration has its own new variables [<a href="#Go_1.22">Go 1.22</a>]
67236717
(see also <a href="#For_clause">"for" statements with a ForClause</a>).
6724-
If the iteration variables are declared outside the “for” statement,
6725-
after execution their values will be those of the last iteration.
6718+
If the range expression is a (possibly untyped) integer expression <code>n</code>,
6719+
the variable has the same type as if it was
6720+
<a href="#Variable_declarations">declared</a> with initialization
6721+
expression <code>n</code>.
6722+
Otherwise, the variables have the types of their respective iteration values.
6723+
</p>
6724+
6725+
<p>
6726+
If the iteration variables are not explicitly declared by the "range" clause,
6727+
they must be preexisting.
6728+
In this case, the iteration values are assigned to the respective variables
6729+
as in an <a href="#Assignment_statements">assignment statement</a>.
6730+
If the range expression is a (possibly untyped) integer expression <code>n</code>,
6731+
<code>n</code> too must be <a href="#Assignability">assignable</a> to the iteration variable;
6732+
if there is no iteration variable, <code>n</code> must be assignable to <code>int</code>.
67266733
</p>
67276734

67286735
<pre>
@@ -6765,6 +6772,11 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
67656772
// type of i is int (default type for untyped constant 10)
67666773
f(i)
67676774
}
6775+
6776+
// invalid: 256 cannot be assigned to uint8
6777+
var u uint8
6778+
for u = range 256 {
6779+
}
67686780
</pre>
67696781

67706782

0 commit comments

Comments
 (0)