-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: untyped float constants representable as int are rejected in range clauses #66561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@gri for spec issues |
Agreed. The spec should be made consistent here (as needed) and the implementation relaxed accordingly. |
Few comments: for range '\x01' {}
for range '1' {} Both rune literals are valid in Regarding floating-point constant in for-loop: reflect.TypeOf(1e0) == float64 // true
for i, v := range [1e0]struct{}{} {} // ok
for i, v := range [1.0]struct{}{} {} // ok
for i, v := range [1.1]struct{}{} {} // array length 1.1 (untyped float constant) must be integer So untyped floating-point constants with 0 fractional part The last point might relate to this issue as well: #52080 |
Rune literals are integer values, there's no issue there. |
Change https://go.dev/cl/580937 mentions this issue: |
Change https://go.dev/cl/581295 mentions this issue: |
It turns out that this is a bit more subtle than anticipated.
|
Change https://go.dev/cl/581256 mentions this issue: |
After going back and forth on this (including implementing it in full), and discussing internally, I believe we should leave things as they are, for the following reasons: Contrary to the initial claim, the In contrast, in a We could allow untyped numeric constants of non-integer type as long as they are representable as integers, but that actually adds an additional rule (the assignment equivalence doesn't work directly anymore) and it seems an unnecessary extra complication for a rather hypothetical (or at least rare) case. If one really wants to write In any case, I have updated the spec (CL 581256) to hopefully be clearer, and I have also fixed the implementation where we had a related issue (#67027). Closing this as working as intended. |
While at it, slightly improve documentation and code. Also, add additional test cases for #66561. Updates #66561. Fixes #67027. Change-Id: I682b0e9227e065d6bbd199871c2e1ecff13edc66 Reviewed-on: https://go-review.googlesource.com/c/go/+/580937 Reviewed-by: Robert Griesemer <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
Change https://go.dev/cl/590056 mentions this issue: |
…oint iteration variable While at it, slightly improve documentation and code. Also, add additional test cases for #66561. Updates #66561. Fixes #67798. Change-Id: I682b0e9227e065d6bbd199871c2e1ecff13edc66 Reviewed-on: https://go-review.googlesource.com/c/go/+/580937 Reviewed-by: Robert Griesemer <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/590056
Go version
go version go1.22.0 linux/amd64
Output of
go env
in your module/workspace:What did you do?
Used a constant representable as int in a
range
clause.https://go.dev/play/p/Zkd_wK5-Y20
(More realistically, I wrote a number of iterations for a test as
const N = 2e5
, then usedN
in a range clause.)What did you see happen?
./prog.go:8:12: cannot range over 1e0 (untyped float constant 1)
What did you expect to see?
Since constant
1e0
is representable as int, I expect it to be implicitly typed as such in arange
clause. AFAIK this is the only place the compiler rejects floating-point constants representable as an integer type where an integer expression is expected; e.g.,[1e0]struct{}{}
is accepted.Per the spec's section on
for
loops withrange
clauses:along with the section on constants:
Arguably, the fact that range clauses specify "a (possibly untyped) integer expression" is the justification that floating point constants are always rejected. In particular, that section is the only place where the phrase "integer expression" appears in the spec. However, the section on constants also classifies rune constants separately from integer constants ("Rune, integer, floating-point, and complex constants are collectively called numeric constants"), yet the compiler does not reject a rune constant in a range clause: https://go.dev/play/p/eh307dPM6s5. So, my reading of the spec is that both rune and floating-point constants should be either accepted or rejected.
The text was updated successfully, but these errors were encountered: