compiler: properly retrieve name of generic functions

Fix panic described in #3040. Ref. #2376.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2023-08-17 12:39:06 +03:00
parent 5c6a111d00
commit 415d44792a

View file

@ -1,6 +1,7 @@
package compiler package compiler
import ( import (
"fmt"
"go/ast" "go/ast"
"go/types" "go/types"
) )
@ -85,7 +86,23 @@ func (c *codegen) getFuncNameFromDecl(pkgPath string, decl *ast.FuncDecl) string
case *ast.Ident: case *ast.Ident:
name = t.Name + "." + name name = t.Name + "." + name
case *ast.StarExpr: case *ast.StarExpr:
name = t.X.(*ast.Ident).Name + "." + name switch t.X.(type) {
case *ast.Ident:
name = t.X.(*ast.Ident).Name + "." + name
case *ast.IndexExpr:
// Generic func declaration receiver: func (x *Pointer[T]) Load() *T
name = t.X.(*ast.IndexExpr).X.(*ast.Ident).Name + "." + name
default:
panic(fmt.Errorf("unexpected function `%s` receiver type: %T", name, t.X))
}
case *ast.IndexExpr:
switch t.X.(type) {
case *ast.Ident:
// Generic func declaration receiver: func (x Pointer[T]) Load() *T
name = t.X.(*ast.Ident).Name + "." + name
default:
panic(fmt.Errorf("unexpected function `%s` receiver type: %T", name, t.X))
}
} }
} }
return c.getIdentName(pkgPath, name) return c.getIdentName(pkgPath, name)