From 415d44792a6a6d5f3836ba70621dbcdb696cfb3a Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 17 Aug 2023 12:39:06 +0300 Subject: [PATCH] compiler: properly retrieve name of generic functions Fix panic described in #3040. Ref. #2376. Signed-off-by: Anna Shaleva --- pkg/compiler/func_scope.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/compiler/func_scope.go b/pkg/compiler/func_scope.go index c6b6e19b7..31fd9d381 100644 --- a/pkg/compiler/func_scope.go +++ b/pkg/compiler/func_scope.go @@ -1,6 +1,7 @@ package compiler import ( + "fmt" "go/ast" "go/types" ) @@ -85,7 +86,23 @@ func (c *codegen) getFuncNameFromDecl(pkgPath string, decl *ast.FuncDecl) string case *ast.Ident: name = t.Name + "." + name 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)