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
NeedsFix
Milestone

Comments

@griesemer
Copy link
Contributor

@griesemer griesemer commented Nov 9, 2021

type T[P (any)] struct{}

is not recognized correctly:

x.go:3:8: undefined: P
@griesemer griesemer added the NeedsFix 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

@griesemer griesemer commented Nov 10, 2021

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

@griesemer griesemer commented Nov 10, 2021

Closing as working as expected.

@go101
Copy link

@go101 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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix
Projects
None yet
Development

No branches or pull requests

2 participants