Skip to content

Commit

Permalink
[release-branch.go1.22] spec: clarify iteration variable type for ran…
Browse files Browse the repository at this point in the history
…ge over integer

Also: report language version (plus date) in spec header.

For #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]>
Reviewed-on: https://go-review.googlesource.com/c/go/+/559875
  • Loading branch information
griesemer committed Jan 31, 2024
1 parent aa721d1 commit 9289b9c
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions doc/go_spec.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--{
"Title": "The Go Programming Language Specification",
"Subtitle": "Version of Dec 27, 2023",
"Subtitle": "Language version go1.22 (Jan 30, 2023)",
"Path": "/ref/spec"
}-->

Expand Down Expand Up @@ -6661,7 +6661,7 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
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, &lt;-chan E element e E
integer n integer type I value i I
integer n integer type value i see below
</pre>

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

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

<p>
The iteration values are assigned to the respective
iteration variables as in an <a href="#Assignment_statements">assignment statement</a>.
</p>

<p>
The iteration variables may be declared by the "range" clause using a form of
<a href="#Short_variable_declarations">short variable declaration</a>
(<code>:=</code>).
In this case their types are set to the types of the respective iteration values
and their <a href="#Declarations_and_scope">scope</a> is the block of the "for" statement;
each iteration has its own separate variables [<a href="#Go_1.22">Go 1.22</a>]
In this case their <a href="#Declarations_and_scope">scope</a> is the block of the "for" statement
and each iteration has its own new variables [<a href="#Go_1.22">Go 1.22</a>]
(see also <a href="#For_clause">"for" statements with a ForClause</a>).
If the iteration variables are declared outside the “for” statement,
after execution their values will be those of the last iteration.
If the range expression is a (possibly untyped) integer expression <code>n</code>,
the variable has the same type as if it was
<a href="#Variable_declarations">declared</a> with initialization
expression <code>n</code>.
Otherwise, the variables have the types of their respective iteration values.
</p>

<p>
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 <a href="#Assignment_statements">assignment statement</a>.
If the range expression is a (possibly untyped) integer expression <code>n</code>,
<code>n</code> too must be <a href="#Assignability">assignable</a> to the iteration variable;
if there is no iteration variable, <code>n</code> must be assignable to <code>int</code>.
</p>

<pre>
Expand Down Expand Up @@ -6765,6 +6772,11 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
// type of i is int (default type for untyped constant 10)
f(i)
}

// invalid: 256 cannot be assigned to uint8
var u uint8
for u = range 256 {
}
</pre>


Expand Down

0 comments on commit 9289b9c

Please sign in to comment.