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/compile: parenthesized constraint causes problems #49485

Closed
griesemer opened this issue Nov 9, 2021 · 3 comments
Closed

cmd/compile: parenthesized constraint causes problems #49485

griesemer opened this issue Nov 9, 2021 · 3 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@griesemer
Copy link
Contributor

type T[P (any)] struct{}

is not recognized correctly:

x.go:3:8: undefined: P
@griesemer griesemer added the NeedsFix The path to resolution is known, but the work has not been done. label Nov 9, 2021
@griesemer griesemer added this to the Go1.18 milestone Nov 9, 2021
@griesemer griesemer self-assigned this Nov 9, 2021
@griesemer
Copy link
Contributor Author

It turns out that we cannot make this work as expected (i.e., to be interpreted as a type parameter followed by a parameterized constraint):

In general, an expression of the form T(x) may be a conversion or a function call, both of which may potentially result in a constant result (there are constant conversions, and some built-in functions like len can produce constant results). Thus, when we see [T (x)] we cannot assume a type parameter T followed by a parameterized constraint x without type checking; it could potentially be an array length. Here's a valid example:

package main

type A [len(*p)]struct{} // array declaration, not a parameterized type

var p *[10]byte

func main() {
	println(len(A{}))
}

@griesemer
Copy link
Contributor Author

Closing as working as expected.

@go101
Copy link

go101 commented Mar 5, 2022

Another intended case:

package main

type A [len (S)]struct{} // array declaration, not a parameterized type

const S = "Go"

func main() {
	println(len(A{})) // 2
}

@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 NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants