-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: Bad error message on missing type constraint #55961
Comments
Sorry, but this is working as intended. What you wrote is an array declaration (and if you use an IDE it may even format it accordingly): type X [T]struct {
t T
} This is an array of structs. It reports an error because the length const T = 10
type X [T]struct {
t T
} you'd just get an error for the field ( And exactly because we are aware of this potential problem, the error message is explicitly saying |
See also #51145. |
Even the IDE formats it (and the error message indicates something), it is not obvious as to why this is getting parsed as an array instead of a generic type. In other languages (Java, TypeScript, Rust etc.), one generally doesn't need to explicitly specify the equivalent of an
IIUC, the error is happening during name resolution (it is complaining about a missing definition), which means the code was already parsed. When emitting the error, would it possible to check that the next token (or the start of the next node in the parse tree), see if it is a |
type X [T]someType was always an array declaration in Go (for the last 15 years), it's not new syntax. Go is not other languages (where arrays may look differently). Maybe it's not obvious, which is why the array message explicitly says Yes, the error is happening during name resolution - because the code has already been parsed as an array declaration (because that is what it is, syntactically). Checking the next token doesn't help because type X [T]struct{} is a syntactically correct array declaration. The parser cannot know if About the likelihood of the error: I don't know that it's more common to have a generic type than an array type now. Note that there are no missing tokens in the array case, there's just a missing declaration (perhaps in a separate, missing file). Anyway, I filed #56064. |
Change https://go.dev/cl/439476 mentions this issue: |
Change https://go.dev/cl/439559 mentions this issue: |
…nstraint If a generic type declaration is missing a constraint, syntactically it is an array type declaration with an undefined array length. Mention the possibility of a missing constraint in the error message for the undefined array length. For #56064. For #55961. For #51145. Change-Id: Ic161aeda9ea44faa8aa3bf3e9d62b3b13a95d4c5 Reviewed-on: https://go-review.googlesource.com/c/go/+/439559 Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
…nstraint If a generic type declaration is missing a constraint, syntactically it is an array type declaration with an undefined array length. Mention the possibility of a missing constraint in the error message for the undefined array length. For golang#56064. For golang#55961. For golang#51145. Change-Id: Ic161aeda9ea44faa8aa3bf3e9d62b3b13a95d4c5 Reviewed-on: https://go-review.googlesource.com/c/go/+/439559 Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Try to compile this file:
What did you expect to see?
A useful error message, something like: (IDK if 'constraint' is the right verbiage)
There shouldn't be an error on line 3 because it is not useful.
What did you see instead?
An error which doesn't clearly indicate the problem:
The text was updated successfully, but these errors were encountered: