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: poor diagnostics for type-lists of interface types used as type constraints #39711
Comments
package main
import (
"fmt"
)
type EmptyInterface interface {
type interface{}
}
func Return(type T EmptyInterface, T2 T)(x T2) T2 {
return x
}
func main() {
fmt.Println(Return(interface{}, interface{})(0))
} is valid.
|
package main
import (
"fmt"
"syscall"
)
type Stringy interface {
type fmt.Stringer, error
}
func Return(type T Stringy, T2 T)(x T2) T2 {
return x
}
func main() {
fmt.Println(Return(error, syscall.Errno)(syscall.ENOSYS))
}
Edited: Upon further reflection (and looking at the implementation), I believe this program should be considered incorrect: A function is always type-checked independent of instantiation (it may be exported and we don't know where it is used). For 2nd edit: It's more complicated than that: Sometimes this actually works, e.g. in this case: func _(type A interface{ type interface{} }, B A)() So there are some open issues here. I'd say the result here is a question: Should this be permitted or not? I suggest that it be left out; it could always be added if it were important. |
The last program package main
import (
"fmt"
"syscall"
)
type Stringy interface {
type interface{ Error() string }, interface{ String() string }
}
func Return(type T Stringy, T2 T)(x T2) T2 {
return x
}
func main() {
fmt.Println(Return(error, syscall.Errno)(syscall.ENOSYS))
} is valid (same reasons as before) and the error message is incorrect (the reason is that the interface are compared before they are "completed" and then they look the same). |
In summary, the last two programs shouldn't produce an error, but the first one is correct (and actually runs). |
Correction: The first program is correct, the 2nd one (likely) incorrect per the current design (see edited comment above), and the last program is correct and shouldn't report an error. |
Change https://golang.org/cl/239158 mentions this issue: |
… completed Fixes #39711. Change-Id: Id0efb854a42bd88a66be8165fa3444e33d1c8f8d Reviewed-on: https://go-review.googlesource.com/c/go/+/239158 Reviewed-by: Robert Griesemer <gri@golang.org>
Fixed error for last program on dev.go2go. Filed separate issue #39723 for 2nd program. Closing. |
I was experimenting a bit with type-lists as type constraints, since a type-list containing only interface types is conceptually itself an interface.
The program in https://go2goplay.golang.org/p/oobylbuDcfp does not produce an error today. Should it?
The program in https://go2goplay.golang.org/p/Y2yzWbOOS9t produces a misleading
T is not an interface
error (even thoughT
is necessarily an interface).The program in https://go2goplay.golang.org/p/82ff4-1eW5- produces a spurious
duplicate type … in type list
error.(CC @ianlancetaylor @griesemer)
The text was updated successfully, but these errors were encountered: