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: confusing error: cannot use x (variable of type []string) as []string value #58611
Comments
The func (s *Set[T]) String() T {
keys := make([]T, 0, len(*s))
for k := range *s {
keys = append(keys, k)
}
sort.Strings(keys) // cannot use keys (variable of type []T) as []string value in argument to sort.Strings (!!!)
return strings.Join(keys, ",") // cannot use keys (variable of type []T) as []string value in argument to strings.Join (!!!)
} |
Yes I realized as much after scratching my head and asking for help, but that doesn't make the compiler message helpful as it is |
Please see https://go.dev/doc/faq#types_in_method_declaration . But, yes, perhaps the compiler can give a better error message if the error has two different types with the same name. |
CC @griesemer |
Thanks! Someone suggested to use |
Agreed that the compiler should do a better job in cases like these. |
@ldemailly FWIW I don't think that using IMO it would be a better fix if the compiler would say something like |
Change https://go.dev/cl/470075 mentions this issue: |
Thx for super quick fix! one question though, I am not super clear from the mr what the new error message looks like? |
For the program in the playground it will now report:
It's not the most beautiful error message but it hopefully makes it clear which of the More generally, whenever a type parameter name matches a name of a predeclared type or object (even (The new error message should actually be present in the |
… errors If a type parameter has the same name as a predeclared type, error messages can be very confusing. In these rare cases, explicitly point out where the type parameter is declared (types2) or that it is a type parameter (go/types). (We can't point out where the type parameter is declared in go/types because we don't have access to the file set in the type writer at the moment.) Fixes golang#58611. Change-Id: I5c150c2b0afae5fad320821e7e5935090dc2ef4c Reviewed-on: https://go-review.googlesource.com/c/go/+/470075 Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com>
What version of Go are you using (
go version
)?Same for go 1.18 to 1.20.1
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?n/a
What did you do?
https://go.dev/play/p/YhkT3b691OB
What did you expect to see?
instead
What did you see instead?
ps: I'm now aware that using Set[string] in that context doesn't specialize the Set to string, but this was super confusing
An even better error message could be in the receiver to say something
Set[string] should be Set[T comparable]
or some such as while string isn't a reserved keyword (sadly in this case) it's very confusingThe text was updated successfully, but these errors were encountered: