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

proposal: spec: make len(a), cap(a) const for some type parameters #65592

Closed
griesemer opened this issue Feb 8, 2024 · 10 comments
Closed

proposal: spec: make len(a), cap(a) const for some type parameters #65592

griesemer opened this issue Feb 8, 2024 · 10 comments
Assignees
Labels
Milestone

Comments

@griesemer
Copy link
Contributor

Proposal Details

The code

func _[A C](a A) {
   const _ = len(a) // or cap(a)
}

should be valid if the constraint C describes a type set containing only (pointer to) arrays and all arrays have the same length. Currently, the length and capacity of a type parameter value are always non-constant. Either way, the type is defined to be int per the spec. An int constant value can be used wherever an int non-constant value is permitted. Thus, this change is strictly backward-compatible; it simply increases the set of valid programs.

See also #50226.

@griesemer griesemer added this to the Go1.23 milestone Feb 8, 2024
@griesemer griesemer self-assigned this Feb 8, 2024
@griesemer griesemer changed the title proposal: spec: len(a)/cap(a) should be constant if a's type is a type parameter constraint by arrays of the same length proposal: spec: len(a)/cap(a) should be constant if a's type is a type parameter constraint by arrays of the same length Feb 8, 2024
@griesemer griesemer changed the title proposal: spec: len(a)/cap(a) should be constant if a's type is a type parameter constraint by arrays of the same length proposal: spec: len(a)/cap(a) should be constant if a's type is a type parameter constrained by arrays of the same length Feb 8, 2024
@gopherbot
Copy link

Change https://go.dev/cl/562349 mentions this issue: go/types, types2: len/cap to be constant for certain array parameter values

@gopherbot
Copy link

Change https://go.dev/cl/562323 mentions this issue: spec: len/cap to be constant for certain type parameter values

@mdempsky
Copy link
Member

Thus, this change is strictly backward-compatible; it simply increases the set of valid programs.

I believe this is a counter-example that would stop type-checking with this change:

func F[T *[0]int](x T) {
	switch 0 {
	case 0:
	case len(x):
	}
}

If we want to be strictly backwards-compatible, I think this semantic change needs to be tied to the language version like for loops.

@go101
Copy link

go101 commented Feb 27, 2024

Bit-shift 1 << len(a) is also a blocker.

@griesemer
Copy link
Contributor Author

@mdempsky Indeed. Seems pretty esoteric, but it would break such code. Maybe not worth making this change.
(More generally, the same spec trick can be used for any similar language change where we might go from a variable value to a constant value.)

@go101
Copy link

go101 commented Feb 28, 2024

Similar to #28591

@go101
Copy link

go101 commented Feb 28, 2024

Honestly speaking, the change is quite small comparing to the loop var change.

@rsc
Copy link
Contributor

rsc commented Feb 28, 2024

Since the change is not backwards compatible, it seems like we should leave things as they are.

@griesemer
Copy link
Contributor Author

Retracting.

@rsc rsc changed the title proposal: spec: len(a)/cap(a) should be constant if a's type is a type parameter constrained by arrays of the same length proposal: spec: make len(a), cap(a) const for some type parameters Mar 1, 2024
@rsc
Copy link
Contributor

rsc commented Mar 1, 2024

This proposal has been declined as infeasible.
— rsc for the proposal review group

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Declined
Development

No branches or pull requests

5 participants