You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
yet the following program is accepted (playground):
package main
constmaxUint=18446744073709551615funcmain() {
forrangeuint(maxUint) {
break
}
}
The range expression uint(maxUint) is an integer expression but cannot be assigned to an int variable, yet this code is permitted. Or perhaps the intent was that the assignability requirement to int is only present if the expression x is untyped, but it's not super-clear from the prose. Furthermore, it doesn't say anything about rune constants x.
In general, an (integer) numeric range expression x produces the index values from 0, 1, ... x-1 and so we want the same type behavior as we get in assignments. If the index variable i is:
declared outside the for statement, i = x must be valid
:= declared in the for-range statement, i := x must be valid (but we may want to allow floating point constants x representable as int values
not declared, _ = x must be valid
The spec needs to be updated accordingly.
The text was updated successfully, but these errors were encountered:
griesemer
changed the title
cmd/compile: for range maxUint should not be accepted, spec is inconsistent
spec: per spec, a valid range uint(maxUint) is not accepted
Apr 22, 2024
griesemer
changed the title
spec: per spec, a valid range uint(maxUint) is not accepted
spec: a valid range uint(maxUint) is not accepted
Apr 22, 2024
Per the spec:
yet the following program is accepted (playground):
The range expression
uint(maxUint)
is an integer expression but cannot be assigned to an int variable, yet this code is permitted. Or perhaps the intent was that the assignability requirement toint
is only present if the expressionx
is untyped, but it's not super-clear from the prose. Furthermore, it doesn't say anything about rune constantsx
.In general, an (integer) numeric range expression x produces the index values from 0, 1, ... x-1 and so we want the same type behavior as we get in assignments. If the index variable i is:
i = x
must be validi := x
must be valid (but we may want to allow floating point constantsx
representable asint
values_ = x
must be validThe spec needs to be updated accordingly.
The text was updated successfully, but these errors were encountered: