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

Add types.Unlias to types assertions and types switches to get an underlying type instead of types.Alias #33868

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

synenka
Copy link

@synenka synenka commented Feb 5, 2025

Go 1.24's types package changes type representation: types created as aliases would be represented by types.Alias instead of its underlying type as before. This requires updates to any code using go/types. Specifically, types.Unalias(t) may be needed before type assertions/switches to handle aliases.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@github-actions github-actions bot added the go label Feb 5, 2025
Copy link
Contributor

github-actions bot commented Feb 5, 2025

Assigning reviewers. If you would like to opt out of this review, comment assign to next reviewer:

R: @lostluck for label go.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Copy link
Contributor

@lostluck lostluck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the other comments are the main concerns: We don't want to generate code with underlying/aliased types, we want to use the aliased types for naming things.

There's certainly value in the cases where an alias could be made instead of the raw code. The Unalias handling would have been great when this was originally written, and would have lead to a different approach, had it existed pre generics at all.

However, as I said in my other message: There isn't a great deal of value in fixing this code: users should be migrating to the generic register package, vs trying to use this belabored code generator.

cc: @jrmccluskey @damccorm

Comment on lines 453 to +456
// A single level is safe since the code we're analysing imports it,
// so we can assume the generated code can access it too.
if ot.IsAlias() {
if t, ok := ot.Type().(*types.Named); ok {
if t, ok := types.Unalias(ot.Type()).(*types.Named); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned with this particular type extraction, as it will unalias an arbitrary number of alias layers, and as listed, does run the risk of crossing an internal package boundary.

Comment on lines 512 to +513
// Here's where we ensure we register new imports.
if t, ok := t.(*types.Named); ok {
if t, ok := types.Unalias(t).(*types.Named); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one also feels risky WRT deriving the right import afterwards, where we'd want to have the alias' package.

It's a bit compounded with the unaliases from the extractFromContainer call too.

Comment on lines 738 to 741
func (e *Extractor) deref(v *types.Var) string {
p := v.Type().(*types.Pointer)
p := types.Unalias(v.Type()).(*types.Pointer)
return types.TypeString(p.Elem(), e.qualifier)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change is unnecessary and will break things since it's already expecting a pointer to a value and is trying to get the name of the pointer type anyway.

Given types like this:

type A = someType

var a *A

should still produce "A" for this case.

@@ -749,7 +749,7 @@ func (e *Extractor) varString(v *types.Var) string {
// NameType turns a reflect.Type into a string based on it's name.
// It prefixes Emit or Iter if the function satisfies the constrains of those types.
func (e *Extractor) NameType(t types.Type) string {
switch a := t.(type) {
switch a := types.Unalias(t).(type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one doesn't need to change, since the default case already handled Aliases as desired AFAICT https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/go/types/typestring.go;l=340

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

Successfully merging this pull request may close these issues.

2 participants