Skip to content

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

Closed
@griesemer

Description

@griesemer

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.

Metadata

Metadata

Assignees

Labels

DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions