-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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: improve error messages when types don't satisfy constraints #41125
Comments
If I'm reading the code correctly, there should be an error here. But it would be nice if the error were better. The line is err := ForEach(m2, func(i string) { Here The func ForEach[T any, I Iterator[T]](it I, consumer func(T)) error { So We have type Iterator[T any] interface {
Next() (T, bool)
} So the type of func (i *mapIt[T, U, I]) Next() (U, bool) { so in this case the type of the So I believe the type checker is correct to report an error. But the error shouldn't be That would be a start, anyhow. It's still pretty hard to understand what is going on here. It would be good to see if there is something we can do to make it clearer. |
Yes, that is indeed the issue, though I also didn't realize it at first. I've seen the type checker be able to tell the difference between methods with different instantiated parameters, so it's definitely able to report the error correctly in such cases |
Change https://golang.org/cl/251717 mentions this issue: |
The new error message is now: tmp.go2:245:9: *mapIt[string, int, reverse[string, ReverseIterator[string]]] does not satisfy Iterator[T]: wrong method signature
found func (*mapIt[T, U, I]).Next() (U, bool)
want func (Iterator[T any]).Next() (string, bool) This can be improved further (the T, U, I parameters should be substituted), but this is already better than what we had. Leaving this issue open for now for future improvements. @urandom, please simplify test cases when reporting an issue; it makes it significantly easier to figure out what's going on. |
…sfy constraint If the cause is a method with incorrect signature, report the expected and presented signature. Updates #41125. Change-Id: Ibd0fa23547b77df924ec5421cc8e657d41c5a5a6 Reviewed-on: https://go-review.googlesource.com/c/go/+/251717 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Perhaps the following example illustrates a similar message problem: The compiler outputs: I initially through that parametric interfaces weren't allowed in type assertion expressions. As pointed out in golang-nuts however, it appears that the problem is the fact that the interface has a type list. Perhaps the message should say so instead. |
@urandom Well, a type list is a type constraint. But agreed, this should probably say "type list" instead (or whatever we use as the official terminology once we have a spec). Not urgent. |
The Go 1.18 error message is now:
which seems pretty decent. Closing. |
The following program fails to compile: https://go2goplay.golang.org/p/-2Qmhtax9VX
The following error is observed:
prog.go2:238:9: *mapIt(string, int, reverse(string, ReverseIterator(string))) does not satisfy Iterator(T) (missing method Next)
Note that on line 224, the same parametric type is being instantiated, and used in 228 without a problem. Also note line 238, invoking the method the type checker complains about.
The text was updated successfully, but these errors were encountered: