Skip to content
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

spec: a valid range uint(maxUint) is not accepted #66967

Closed
griesemer opened this issue Apr 22, 2024 · 2 comments
Closed

spec: a valid range uint(maxUint) is not accepted #66967

griesemer opened this issue Apr 22, 2024 · 2 comments
Assignees
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@griesemer
Copy link
Contributor

griesemer commented Apr 22, 2024

Per the spec:

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

const maxUint = 18446744073709551615

func main() {
	for range uint(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.

@griesemer griesemer added the NeedsFix The path to resolution is known, but the work has not been done. label Apr 22, 2024
@griesemer griesemer added this to the Go1.23 milestone Apr 22, 2024
@griesemer griesemer self-assigned this Apr 22, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Apr 22, 2024
@griesemer
Copy link
Contributor Author

cc: @findleyr for visibility

@griesemer griesemer added Documentation and removed compiler/runtime Issues related to the Go compiler and/or runtime. labels Apr 22, 2024
@griesemer 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 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
@gopherbot
Copy link

Change https://go.dev/cl/581256 mentions this issue: spec: fix prose for range over numeric range expression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

2 participants