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

import generic functions from extracted Go code as interpreted code #1647

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 63 additions & 7 deletions extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strconv"
"strings"
"text/template"

"golang.org/x/tools/go/packages"
)

const model = `// Code generated by 'yaegi extract {{.ImportPath}}'. DO NOT EDIT.
Expand Down Expand Up @@ -141,7 +143,7 @@ type Extractor struct {
Tag []string // Comma separated of build tags to be added to the created package.
}

func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, error) {
func (e *Extractor) genContent(importPath string, p *types.Package, fset *token.FileSet) ([]byte, error) {
prefix := "_" + importPath + "_"
prefix = strings.NewReplacer("/", "_", "-", "_", ".", "_", "~", "_").Replace(prefix)

Expand Down Expand Up @@ -201,8 +203,31 @@ func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, err
val[name] = Val{pname, false}
}
case *types.Func:
// Skip generic functions and methods.
// Generic functions and methods must be extracted as code that
// can be interpreted, since they cannot be compiled in.
if s := o.Type().(*types.Signature); s.TypeParams().Len() > 0 || s.RecvTypeParams().Len() > 0 {
scope := o.Scope()
start, end := scope.Pos(), scope.End()
ff := fset.File(start)
base := token.Pos(ff.Base())
start -= base
end -= base

f, err := os.Open(ff.Name())
if err != nil {
return nil, err
}
b := make([]byte, end-start)
_, err = f.ReadAt(b, int64(start))
if err != nil {
return nil, err
}
// only add if we have a //yaegi:add directive
if !bytes.Contains(b, []byte(`//yaegi:add`)) {
continue
}
val[name] = Val{fmt.Sprintf("interp.GenericFunc(%q)", b), false}
imports["github.com/traefik/yaegi/interp"] = true
continue
}
val[name] = Val{pname, false}
Expand Down Expand Up @@ -447,16 +472,47 @@ func (e *Extractor) Extract(pkgIdent, importPath string, rw io.Writer) (string,
return "", err
}

pkg, err := importer.ForCompiler(token.NewFileSet(), "source", nil).Import(pkgIdent)
if err != nil {
return "", err
var pkg *types.Package
isRelative := strings.HasPrefix(pkgIdent, ".")
fset := token.NewFileSet()
// If we are relative with a manual import path, we cannot use modules
// and must fall back on the standard go/importer loader.
if isRelative && importPath != "" {
pkg, err = importer.ForCompiler(fset, "source", nil).Import(pkgIdent)
if err != nil {
return "", err
}
} else {
// Otherwise, we can use the much faster x/tools/go/packages loader.
if isRelative {
// We must be in the location of the module for the loader to work correctly.
err := os.Chdir(pkgIdent)
if err != nil {
return "", err
}
// Our path must point back to ourself here.
pkgIdent = filepath.Join("..", filepath.Base(pkgIdent))
}
// NeedsSyntax is needed for getting the scopes of generic functions.
pkgs, err := packages.Load(&packages.Config{Mode: packages.NeedTypes | packages.NeedSyntax}, pkgIdent)
if err != nil {
return "", err
}
if len(pkgs) != 1 {
return "", fmt.Errorf("expected one package, got %d", len(pkgs))
}
ppkg := pkgs[0]
if len(ppkg.Errors) > 0 {
return "", ppkg.Errors[0]
}
pkg = ppkg.Types
fset = ppkg.Fset
}

content, err := e.genContent(ipp, pkg)
content, err := e.genContent(ipp, pkg, fset)
if err != nil {
return "", err
}

if _, err := rw.Write(content); err != nil {
return "", err
}
Expand Down
24 changes: 24 additions & 0 deletions extract/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ type _guthib_com_variadic_Variadic struct {
func (W _guthib_com_variadic_Variadic) Call(method string, args ...[]interface{}) (interface{}, error) {
return W.WCall(method, args...)
}
`[1:],
},
{
desc: "using relative path, function is generic",
wd: "./testdata/8/src/guthib.com/generic",
arg: "../generic",
importPath: "guthib.com/generic",
expected: `
// Code generated by 'yaegi extract guthib.com/generic'. DO NOT EDIT.

package generic

import (
"github.com/traefik/yaegi/interp"
"guthib.com/generic"
"reflect"
)

func init() {
Symbols["guthib.com/generic/generic"] = map[string]reflect.Value{
// function, constant and variable definitions
"Hello": reflect.ValueOf(interp.GenericFunc("func Hello[T comparable](v T) *T { //yaegi:add\n\treturn &v\n}")),
}
}
`[1:],
},
}
Expand Down
5 changes: 5 additions & 0 deletions extract/testdata/8/src/guthib.com/generic/generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package generic

func Hello[T comparable](v T) *T { //yaegi:add
return &v
}
4 changes: 4 additions & 0 deletions extract/testdata/8/src/guthib.com/generic/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module guthib.com/generic

go 1.21

7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module github.com/traefik/yaegi

go 1.21

require golang.org/x/tools v0.22.0

require (
golang.org/x/mod v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
24 changes: 21 additions & 3 deletions interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,11 +1214,12 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string

case c0.isType(sc):
// Type conversion expression
c1 := n.child[1]
var c1 *node
switch len(n.child) {
case 1:
err = n.cfgErrorf("missing argument in conversion to %s", c0.typ.id())
case 2:
c1 = n.child[1]
err = check.conversion(c1, c0.typ)
default:
err = n.cfgErrorf("too many arguments in conversion to %s", c0.typ.id())
Expand Down Expand Up @@ -1293,7 +1294,10 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
default:
// The call may be on a generic function. In that case, replace the
// generic function AST by an instantiated one before going further.
if isGeneric(c0.typ) {
if c0.typ == nil {
err = c0.cfgErrorf("nil type for function call: likely generic type error")
break
} else if isGeneric(c0.typ) {
fun := c0.typ.node.anc
var g *node
var types []*itype
Expand Down Expand Up @@ -2553,7 +2557,18 @@ func (n *node) isType(sc *scope) bool {
return true // Imported source type
}
case identExpr:
return sc.getType(n.ident) != nil
sym, _, found := sc.lookup(n.ident)
if found {
return sym.kind == typeSym
}
// note: in case of generic functions, the type might not exist within
// the scope where the generic function was defined, so we
// fall back on comparing the scopes: anything out of scope is assumed
// to be a type.
if n.typ == nil || n.typ.scope == nil {
return false
}
return n.typ.scope.pkgID != sc.pkgID
case indexExpr:
// Maybe a generic type.
sym, _, ok := sc.lookup(n.child[0].ident)
Expand Down Expand Up @@ -2879,6 +2894,9 @@ func setExec(n *node) {
set(n.fnext)
}
}
if n.gen == nil {
n.gen = nop
}
n.gen(n)
}

Expand Down
21 changes: 16 additions & 5 deletions interp/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"sync/atomic"
)

// GenericFunc contains the code of a generic function.
// This is used in the `yaegi extract` command to represent generic functions
// instead of the actual value of the function, since you cannot get the
// [reflect.Value] of a generic function. This is then used to interpret the
// function when it is imported in yaegi.
type GenericFunc string

// adot produces an AST dot(1) directed acyclic graph for the given node. For debugging only.
// func (n *node) adot() { n.astDot(dotWriter(n.interp.dotCmd), n.ident) }

Expand Down Expand Up @@ -58,7 +65,7 @@ func genAST(sc *scope, root *node, types []*itype) (*node, bool, error) {

case fieldList:
// Node is the type parameters list of a generic function.
if root.kind == funcDecl && n.anc == root.child[2] && childPos(n) == 0 {
if root.kind == funcDecl && n.anc == root.child[2] && childPos(n) == 0 && len(types) > 0 {
// Fill the types lookup table used for type substitution.
for _, c := range n.child {
l := len(c.child) - 1
Expand Down Expand Up @@ -291,11 +298,15 @@ func inferTypesFromCall(sc *scope, fun *node, args []*node) ([]*itype, error) {
if err != nil {
return nil, err
}
lt, err := inferTypes(typ, args[i].typ)
if err != nil {
return nil, err
if i < len(args) {
lt, err := inferTypes(typ, args[i].typ)
if err != nil {
return nil, err
}
types = append(types, lt...)
} else {
types = append(types, typ)
}
types = append(types, lt...)
}

return types, nil
Expand Down
Loading
Loading