Skip to content
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

go/types: Package.Scope.Lookup returns aliased type instead of declared alias type #22975

Closed
willfaught opened this issue Dec 3, 2017 · 3 comments
Assignees
Milestone

Comments

@willfaught
Copy link
Contributor

@willfaught willfaught commented Dec 3, 2017

For example, "golang.org/x/net/context".Context now aliases "context".Context. I expected the Lookup call for the "go/types".Scope for the "go/types".Package for "context" to give me the "go/types".Type for the "golang.org/x/net/context".Context declaration, but it gave me the one for the "context".Context declaration, so the result of "go/types".Named.Obj().IsAlias() for that type is false when I expected true. Is there another way you're supposed to look up the type of a package's alias declaration by name, such that IsAlias returns true? If not, is there a workaround?

What did you do?

https://play.golang.org/p/oIWD5Ok2Xl

Note that it gets an error in the playground because golang.org/x/net/context isn't installed. You'll have to run it on your own machine for it to work.

What did you expect to see?

golang.org/x/net/context.Context is alias: actual true, expected true

What did you see instead?

golang.org/x/net/context.Context is alias: actual false, expected true

System details

go version go1.9.2 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/willfaught/Developer/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/_1/ggvd2t1x7hz_185crsb36zlr0000gp/T/go-build802077183=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOROOT/bin/go version: go version go1.9.2 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.9.2
uname -v: Darwin Kernel Version 17.2.0: Fri Sep 29 18:27:05 PDT 2017; root:xnu-4570.20.62~3/RELEASE_X86_64
ProductName:	Mac OS X
ProductVersion:	10.13.1
BuildVersion:	17B1003
lldb --version: lldb-900.0.57
  Swift-4.0
@smasher164
Copy link
Member

@smasher164 smasher164 commented Dec 4, 2017

I modified the example to call Info.ObjectOf on the type's declared identifier, and I observe the same behavior.
https://play.golang.org/p/LKg-Wr247J

Interestingly, printing the type's declaration shows the following:

type golang.org/x/net/context.Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key interface{}) interface{}}

Loading

@griesemer
Copy link
Contributor

@griesemer griesemer commented Dec 5, 2017

This is working as expected and you have a bug in your code: You are calling the IsAlias() method on the original type name rather than the aliased type name. Keep in mind that a Type is never an alias, it's a TypeName that may be an alias (names can be aliases, types are never aliases).

Or in other words, the Type of a TypeName object is always the actual (non-alias) type, no matter if the TypeName is an alias or not. In your code

tp.Scope().Lookup("Context").Type().(*types.Named).Obj().IsAlias()

you are getting the Type of the Context object, which is the original (std lib) named Context Type. Then you look up it's TypeName Object, which is (std lib) context.Context, and of course that's not an alias.

On the other hand, if you check if the looked up Context object is an an alias, you will get the expected result:

tp.Scope().Lookup("Context").(*types.TypeName).IsAlias()

Loading

@willfaught
Copy link
Contributor Author

@willfaught willfaught commented Dec 5, 2017

Ah, that makes sense. I didn't realize the Object returned by Lookup was the TypeName. Sorry for the mixup.

Loading

@golang golang locked and limited conversation to collaborators Dec 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants