obtaining the original type from a named alias type with go/types?

175 views
Skip to first unread message

Dan Kortschak

unread,
Aug 15, 2020, 6:45:34 PM8/15/20
to golang-nuts
I would like to be able to obtain the original type for an alias given
a source input. I can see in "go/types" that it's possible to know
whether a named type is an alias by `typ.Obj().IsAlias()`, but I cannot
see how to obtain the actual original type unless it is an alias for a
basic type.

Can someone point me to a way to get this information? From the source
it looks something like `typ.Obj().Type().(*types.Named).Obj().Type()`.
Is this correct assuming that the original type is a named type?

thanks
Dan


Ian Lance Taylor

unread,
Aug 16, 2020, 12:45:23 AM8/16/20
to Dan Kortschak, golang-nuts
I haven't tested it but I *think* you can just write typ.Underlying().

Ian

Dan Kortschak

unread,
Aug 22, 2020, 12:16:56 AM8/22/20
to golang-nuts
Thanks, Ian.

No that doesn't work. For example with type byte, you get back the byte
name.

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

Dan


Ian Lance Taylor

unread,
Aug 22, 2020, 12:51:53 AM8/22/20
to Dan Kortschak, golang-nuts
The underlying type of byte is indeed byte.  What are you hoping for?

Ian

Paul Jolly

unread,
Aug 22, 2020, 1:00:44 AM8/22/20
to Dan Kortschak, golang-nuts
I think you were unlucky with your choice of type to experiment with.
My understanding is that byte is special cased, despite being an
alias:

https://github.com/golang/go/blob/13e41bcde8c788224f4896503b56d42614e0bf97/src/go/types/universe.go#L25

Consider instead https://play.golang.org/p/MHjgBvbOG__G

On Sat, 22 Aug 2020 at 05:16, 'Dan Kortschak' via golang-nuts
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d3f6c5b7a76f7ea2db93f34fa87917651dcc7ad5.camel%40kortschak.io.

Dan Kortschak

unread,
Aug 22, 2020, 1:23:54 AM8/22/20
to golang-nuts
On Fri, 2020-08-21 at 21:51 -0700, Ian Lance Taylor wrote:
>
> > Thanks, Ian.
> >
> > No that doesn't work. For example with type byte, you get back the
> > byte
> > name.
> >
> > https://play.golang.org/p/PPjHBotsIsw
>
> The underlying type of byte is indeed byte. What are you hoping for?
>
> Ian

I'm hoping for a resolution of the type to its base unaliased type. The
types.Type of a "byte" is &types.Basic{kind: types.Uint8, info:
types.IsInteger|types.IsUnsigned, name: "byte"} while the type for
"uint8" (obtained either from syntax or from types.Typ[types.Uint8], or
types.Typ[types.Byte]) is the same, but with name = "uint8". The same
situation is true for rune/int32.

https://play.golang.org/p/cbbU7-qzwxQ

I would like to resolve all types that are aliases to a single string
representation.

Paul's code at https://play.golang.org/p/MHjgBvbOG__G shows that for
other cases .Underlying() is not needed, it resolves to the original
type.

It it's just uint8/byte and int32/rune, I can special case them and do
a looking into types.Typ with the type's Kind.

Dan



Dan Kortschak

unread,
Aug 22, 2020, 1:24:05 AM8/22/20
to golang-nuts
On Sat, 2020-08-22 at 06:00 +0100, Paul Jolly wrote:
> I think you were unlucky with your choice of type to experiment with.
> My understanding is that byte is special cased, despite being an
> alias:
>
>
https://github.com/golang/go/blob/13e41bcde8c788224f4896503b56d42614e0bf97/src/go/types/universe.go#L25
>
> Consider instead https://play.golang.org/p/MHjgBvbOG__G

Thanks, Paul.

Not so much unlucky as attempting to find the worst case so that all
others work as well when it's solved.

That snippet is helpful, but given https://golang.org/issue/40965 I'm
concerned user defined aliases will suffer the same issue when it's
resolved.

Dan



Paul Jolly

unread,
Aug 22, 2020, 1:37:05 AM8/22/20
to Dan Kortschak, golang-nuts
> That snippet is helpful, but given https://golang.org/issue/40965 I'm
> concerned user defined aliases will suffer the same issue when it's
> resolved.

I would be very surprised if that affects Type() to be honest because
that issue is about error message reporting, i.e. "unwinding" to
actual type name a user referenced instead of the actual type.

Best,


Paul
Reply all
Reply to author
Forward
0 new messages