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.Unalias to types assertions and types switches to get an underlying type instead of types.Alias #33868

Merged
merged 11 commits into from
Feb 19, 2025
Merged
26 changes: 13 additions & 13 deletions sdks/go/pkg/beam/util/starcgenx/starcgenx.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ func (e *Extractor) isRequired(ident string, obj types.Object, idsRequired, idsF
if recv := sig.Recv(); recv != nil && graph.IsLifecycleMethod(ident) {
// We don't want to care about pointers, so dereference to value type.
t := recv.Type()
p, ok := t.(*types.Pointer)
p, ok := types.Unalias(t).(*types.Pointer)
for ok {
t = p.Elem()
p, ok = t.(*types.Pointer)
p, ok = types.Unalias(t).(*types.Pointer)
}
ts := types.TypeString(t, e.qualifier)
e.Printf("recv %v has %v, ts: %s %s--- ", recv, sig, ts, ident)
Expand Down Expand Up @@ -405,10 +405,10 @@ func (e *Extractor) fromObj(fset *token.FileSet, id *ast.Ident, obj types.Object
}
// This must be a structural DoFn! We should generate a closure wrapper for it.
t := recv.Type()
p, ok := t.(*types.Pointer)
p, ok := types.Unalias(t).(*types.Pointer)
for ok {
t = p.Elem()
p, ok = t.(*types.Pointer)
p, ok = types.Unalias(t).(*types.Pointer)
}
ts := types.TypeString(t, e.qualifier)
mthdMap := e.wraps[ts]
Expand Down Expand Up @@ -453,7 +453,7 @@ func (e *Extractor) extractType(ot *types.TypeName) {
// 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 {
ot = t.Obj()
name = types.TypeString(t, e.qualifier)
}
Expand Down Expand Up @@ -484,17 +484,17 @@ func (e *Extractor) extractFromContainer(t types.Type) types.Type {
// Container types need to be iteratively unwrapped until we're at the base type,
// so we can get the import if necessary.
for {
if s, ok := t.(*types.Slice); ok {
if s, ok := types.Unalias(t).(*types.Slice); ok {
t = s.Elem()
continue
}

if p, ok := t.(*types.Pointer); ok {
if p, ok := types.Unalias(t).(*types.Pointer); ok {
t = p.Elem()
continue
}

if a, ok := t.(*types.Array); ok {
if a, ok := types.Unalias(t).(*types.Array); ok {
t = a.Elem()
continue
}
Expand All @@ -510,7 +510,7 @@ func (e *Extractor) extractFromTuple(tuple *types.Tuple) {
t := e.extractFromContainer(s.Type())

// 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 {
if pkg := t.Obj().Pkg(); pkg != nil {
e.imports[pkg.Path()] = struct{}{}
}
Expand Down Expand Up @@ -692,13 +692,13 @@ func (e *Extractor) makeInput(sig *types.Signature) (shimx.Input, bool) {
return shimx.Input{}, false
}
// Iterators must return a bool.
if b, ok := r.At(0).Type().(*types.Basic); !ok || b.Kind() != types.Bool {
if b, ok := types.Unalias(r.At(0).Type()).(*types.Basic); !ok || b.Kind() != types.Bool {
return shimx.Input{}, false
}
p := sig.Params()
for i := 0; i < p.Len(); i++ {
// All params for iterators must be pointers.
if _, ok := p.At(i).Type().(*types.Pointer); !ok {
if _, ok := types.Unalias(p.At(i).Type()).(*types.Pointer); !ok {
return shimx.Input{}, false
}
}
Expand Down Expand Up @@ -736,7 +736,7 @@ func (e *Extractor) makeInput(sig *types.Signature) (shimx.Input, bool) {
// deref returns the string identifier for the element type of a pointer var.
// deref panics if the var type is not a pointer.
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)
}

Expand All @@ -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) {
case *types.Signature:
if emt, ok := e.makeEmitter(a); ok {
return "Emit" + emt.Name
Expand Down
Loading