Skip to content

Commit 5419f65

Browse files
committed
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 <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Bypass: Robert Griesemer <[email protected]>
1 parent 1ca31ea commit 5419f65

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

doc/go_spec.html

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Language version go1.22 (April 24, 2024)",
3+
"Subtitle": "Language version go1.22 (April 25, 2024)",
44
"Path": "/ref/spec"
55
}-->
66

@@ -6656,13 +6656,13 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
66566656
</p>
66576657

66586658
<pre class="grammar">
6659-
Range expression 1st value 2nd value
6659+
Range expression 1st value 2nd value
66606660

6661-
array or slice a [n]E, *[n]E, or []E index i int a[i] E
6662-
string s string type index i int see below rune
6663-
map m map[K]V key k K m[k] V
6664-
channel c chan E, &lt;-chan E element e E
6665-
integer n integer type value i see below
6661+
array or slice a [n]E, *[n]E, or []E index i int a[i] E
6662+
string s string type index i int see below rune
6663+
map m map[K]V key k K m[k] V
6664+
channel c chan E, &lt;-chan E element e E
6665+
integer value n integer type, or untyped int value i see below
66666666
</pre>
66676667

66686668
<ol>
@@ -6703,8 +6703,17 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
67036703
</li>
67046704

67056705
<li>
6706-
For an integer value <code>n</code>, the iteration values 0 through <code>n-1</code>
6706+
For an integer value <code>n</code>, where <code>n</code> is of <a href="#Numeric_types">integer type</a>
6707+
or an untyped <a href="#Constants">integer constant</a>, the iteration values 0 through <code>n-1</code>
67076708
are produced in increasing order.
6709+
If <code>n</code> is of integer type, the iteration values have that same type.
6710+
Otherwise, the type of <code>n</code> is determined as if it were assigned to the
6711+
iteration variable.
6712+
Specifically:
6713+
if the iteration variable is preexisting, the type of the iteration values is the type of the iteration
6714+
variable, which must be of integer type.
6715+
Otherwise, if the iteration variable is declared by the "range" clause or is absent,
6716+
the type of the iteration values is the <a href="#Constants">default type</a> for <code>n</code>.
67086717
If <code>n</code> &lt= 0, the loop does not run any iterations.
67096718
</li>
67106719
</ol>
@@ -6716,21 +6725,14 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
67166725
In this case their <a href="#Declarations_and_scope">scope</a> is the block of the "for" statement
67176726
and each iteration has its own new variables [<a href="#Go_1.22">Go 1.22</a>]
67186727
(see also <a href="#For_clause">"for" statements with a ForClause</a>).
6719-
If the range expression is a (possibly untyped) integer expression <code>n</code>,
6720-
the variable has the same type as if it was
6721-
<a href="#Variable_declarations">declared</a> with initialization
6722-
expression <code>n</code>.
6723-
Otherwise, the variables have the types of their respective iteration values.
6728+
The variables have the types of their respective iteration values.
67246729
</p>
67256730

67266731
<p>
67276732
If the iteration variables are not explicitly declared by the "range" clause,
67286733
they must be preexisting.
67296734
In this case, the iteration values are assigned to the respective variables
67306735
as in an <a href="#Assignment_statements">assignment statement</a>.
6731-
If the range expression is a (possibly untyped) integer expression <code>n</code>,
6732-
<code>n</code> too must be <a href="#Assignability">assignable</a> to the iteration variable;
6733-
if there is no iteration variable, <code>n</code> must be assignable to <code>int</code>.
67346736
</p>
67356737

67366738
<pre>
@@ -6778,6 +6780,10 @@ <h4 id="For_range">For statements with <code>range</code> clause</h4>
67786780
var u uint8
67796781
for u = range 256 {
67806782
}
6783+
6784+
// invalid: 1e3 is a floating-point constant
6785+
for range 1e3 {
6786+
}
67816787
</pre>
67826788

67836789

0 commit comments

Comments
 (0)