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

proposal: x/tools/packages: Use type aliases in packages.Package's Types tree. #64988

Closed
dils2k opened this issue Jan 6, 2024 · 2 comments
Closed
Labels
Milestone

Comments

@dils2k
Copy link

dils2k commented Jan 6, 2024

Proposal Details

I have a package that contains a type alias and a method, that has a parameter, type of which is the type alias.

package foo

type Alias = string

func Foo(s Alias) {}

When I parse the package using tools/packages, the type alias in the parameter is ignored and string type is set.

pkg, _ := packages.Load(&packages.Config{
	Dir:  "./foo",
	Mode: packages.NeedName | packages.NeedSyntax | packages.NeedTypes,
})

fn := pkg[0].Types.Scope().Lookup("Foo").(*types.Func)
sig := fn.Type().(*types.Signature)
typ := sig.Params().At(0).Type()

fmt.Println(typ.String()) // --> string, but actual type is Alias

This issue proposes to use actual type aliases in Types tree of *package.Package.

@dils2k dils2k added the Proposal label Jan 6, 2024
@gopherbot gopherbot added this to the Proposal milestone Jan 6, 2024
@dils2k dils2k changed the title proposal: golang.org/x/tools/packages: Show type aliases in packages.Package's Types tree. proposal: golang.org/x/tools/packages: Use type aliases in packages.Package's Types tree. Jan 6, 2024
@seankhliao seankhliao changed the title proposal: golang.org/x/tools/packages: Use type aliases in packages.Package's Types tree. proposal: x/tools/packages: Use type aliases in packages.Package's Types tree. Jan 6, 2024
@dils2k
Copy link
Author

dils2k commented Jan 8, 2024

MB it's a bug btw, not sure

@timothy-king
Copy link
Contributor

This is the expected behavior for Go versions <= 1.22. Alias is just another name for string. For a long time there was no representation of Alias as a go/types.Type. This is expected to change starting in go1.23 #63223 (also behind a GODEBUG flag).

Support for this is expected to start in go/types in go1.23. The status for support from x/tools/go/packages for go1.23 is still in progress: #63223 (comment). IIUC the way I expect this will be available to packages users will be to compile with >= go1.23.

No proposal is needed for this. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants