-
Notifications
You must be signed in to change notification settings - Fork 18k
go/types: can't check interface satisfaction against interfaces defined in package builtin #69717
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
Comments
I can't speak to why |
That certainly works. I appreciate the shortcut. I think I'll leave the issue open since the bug was about a minimal viable example of working with a typechecked |
CC @golang/compiler |
As @dominikh points out, using The package The PS: The following code illustrates the problem by printing the package main
import (
"fmt"
"go/types"
"golang.org/x/tools/go/packages"
)
func main() {
cfg := &packages.Config{
Mode: packages.NeedTypes | packages.NeedImports | packages.NeedSyntax | packages.NeedTypesInfo,
}
builtinPkg, err := packages.Load(cfg, "builtin")
if err != nil {
panic(err)
}
builtinError := builtinPkg[0].Types.Scope().Lookup("error").Type().Underlying().(*types.Interface)
universeError := types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
fmt.Printf("builtinError = %s\n", builtinError)
fmt.Printf("universeError = %s\n", universeError)
ioPkg, err := packages.Load(cfg, "io")
if err != nil {
panic(err)
}
eofObj := ioPkg[0].Types.Scope().Lookup("EOF")
fmt.Println(types.Implements(eofObj.Type(), builtinError))
fmt.Println(types.Implements(eofObj.Type(), universeError))
} The result is:
|
Go version
go version go1.23-20240626-RC01 cl/646990413 +5a18e79687 X:fieldtrack,boringcrypto linux/amd64
Output of
go env
in your module/workspace:What did you do?
I built a small program to walk a type-checked AST to determine whether identifiers implement a given interface (for fun interface
error
frompackage builtin
). I used it to check ifio.EOF
implementserror
:What did you see happen?
It reports
false
.What did you expect to see?
If I hand-compose the interface
error
usingpackage types
it works, but it is rather cumbersome:It reports
true
.This is somewhat understandable since
package builtin
is a bit of a stub for things predeclared items in the universe block. Nevertheless, the failure is a bit surprising.The text was updated successfully, but these errors were encountered: