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

cmd/go2go: len() on an array type-parameter not recognized as compile-time constant #42029

Closed
AndrewWPhillips opened this issue Oct 16, 2020 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@AndrewWPhillips
Copy link

AndrewWPhillips commented Oct 16, 2020

What version of Go are you using (go version)?

$ go version
go version devel +e9e897e0fe Tue Sep 15 19:46:15 2020 +0000 windows/amd64

Also go2go playground (see link below)

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

func f[T interface { type [2]int, [3]int } ]() {
  var dummy T
  var a [len(dummy)]bool
  println(len(a))
}

func main() {
  f[[3]int]()
}

OR

https://go2goplay.golang.org/p/BQFXljOS9zF

What did you expect to see?

3

What did you see instead?

type checking failed for main
prog.go2:11:9: array length len(dummy) (value of type int) must be constant

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 17, 2020
@ianlancetaylor ianlancetaylor added this to the Unreleased milestone Oct 17, 2020
@AndrewWPhillips
Copy link
Author

AndrewWPhillips commented Oct 17, 2020

I tried to produce a simpler example but this works:

type a[T interface { type [2]int, [3]int }] T

func main() {
	var dummy a[[3]int]
	const c = len(dummy)
}

https://go2goplay.golang.org/p/bpK6OWMpRVN

I'm not sure what the difference but this allowed a workaround.

@AndrewWPhillips AndrewWPhillips changed the title cmd/go2go: len() on an array type-parameter not recognized as compile-time constant when type list has multiple entries cmd/go2go: len() on an array type-parameter not recognized as compile-time constant Dec 16, 2021
@AndrewWPhillips
Copy link
Author

I tested this again with Go1.18 beta1 and the problem is still there. It only seems to happen with type parameters on a function not a type. I know I found a workaround for what I was trying, but I think this code should still compile:

func f[T [3]int](dummy T) {
	const c = len(dummy)
}

but instead it gives the error: len(dummy) (value of type int) is not constant

$ go1.18beta1 version
go version go1.18beta1 windows/amd64

@griesemer
Copy link
Contributor

The (forthcoming) spec doesn't say that len for a type parameter value is constant - no matter how the type parameter is constrained.

A generic function must be compilable without instantiation; i.e., the body must type-check without knowledge of how the function is called. For a value a of type parameter P where P is constrained by (pointer to) array types, len(a) could be constant if and only if all those arrays have the same length. In any other case, len(a) cannot be constant because we don't know which array type the type parameter is instantiated with. But if all those arrays have the same length, we know the length, and we could just use it directly.

(Perhaps we still want to change the implementation if it simplifies the rules - but it's in no way important. See also #50226.)

The case of a generic type is a bit different: A generic type cannot be used without instantiation, and once it is instantiated it is a non-generic type. So if you instantiate the type with an array, after instantiation that array is known and len will do what it always did.

Thus, this is working as (currently) intended, pending decision on #50226.

@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants