Repository: goplus/gop
Pull request #1706 (2024-02-03 13:35:57)

TypeAsParamsFunc/Method


Patch:
diff --git a/cl/compile.go b/cl/compile.go
index e7754acb..617bbc38 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -652,8 +652,8 @@ func initGopPkg(ctx *pkgCtx, pkg *gox.Package, gopSyms map[string]bool) {
 			ctx.loadType(lbi.(*ast.Ident).Name)
 		}
 	}
-	if pkg.Types.Scope().Lookup(gopPackage) == nil {
-		pkg.Types.Scope().Insert(types.NewConst(token.NoPos, pkg.Types, gopPackage, types.Typ[types.UntypedBool], constant.MakeBool(true)))
+	if scope := pkg.Types.Scope(); scope.Lookup(gopPackage) == nil {
+		scope.Insert(types.NewConst(token.NoPos, pkg.Types, gopPackage, types.Typ[types.UntypedBool], constant.MakeBool(true)))
 	}
 	gox.InitThisGopPkg(pkg.Types)
 }
@@ -933,11 +933,11 @@ func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, goFile
 				if debugLoad {
 					log.Println("==> Preload func", fname)
 				}
-				initLoader(parent, syms, name.Pos(), fname, fn, genFnBody) /* {
-					if strings.HasSuffix(fname, "Gopx_") { // Gopx_xxx func
+				if initLoader(parent, syms, name.Pos(), fname, fn, genFnBody) {
+					if strings.HasPrefix(fname, "Gopx_") { // Gopx_xxx func
 						ctx.lbinames = append(ctx.lbinames, fname)
 					}
-				} */
+				}
 			}
 		} else {
 			if name, ok := getRecvTypeName(parent, d.Recv, true); ok {
diff --git a/go.mod b/go.mod
index d2e72725..6b72d104 100644
--- a/go.mod
+++ b/go.mod
@@ -5,7 +5,7 @@ go 1.18
 require (
 	github.com/fsnotify/fsnotify v1.7.0
 	github.com/goplus/c2go v0.7.21
-	github.com/goplus/gox v1.14.2
+	github.com/goplus/gox v1.14.7
 	github.com/goplus/mod v0.13.0
 	github.com/qiniu/x v1.13.3
 	golang.org/x/tools v0.17.0
diff --git a/go.sum b/go.sum
index ec0dff2a..d133e80d 100644
--- a/go.sum
+++ b/go.sum
@@ -6,8 +6,9 @@ github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyT
 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
 github.com/goplus/c2go v0.7.21 h1:bRWda6KtQdY0ph+tnPEOGzZMq3OUkAI9cJBzrr5N6AM=
 github.com/goplus/c2go v0.7.21/go.mod h1:45zjbWGW8NDuGfTOrFchCIpH9TbiH7TPg8/zF6EdEyk=
-github.com/goplus/gox v1.14.2 h1:sc2W+R3D6j1EZ6G+/KWMUDSu3m6dcscZCvnTH2M4XG4=
 github.com/goplus/gox v1.14.2/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
+github.com/goplus/gox v1.14.7 h1:C70XxjEVRjrmkblNkbM5oSZOJTOgimsXRtK6/XVlk5s=
+github.com/goplus/gox v1.14.7/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
 github.com/goplus/mod v0.13.0 h1:M/VsI/NWTcs2+vtWBG7793zmlCimKlSMY5LXaiztXV8=
 github.com/goplus/mod v0.13.0/go.mod h1:Qin2SxKoK+3cNFwtg4Jnk1F9Qn5vR6Kwlvr6lTCRJNI=
 github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=


Test patch:
diff --git a/cl/compile_gop_test.go b/cl/compile_gop_test.go
index 04dde8d4..d5e5384a 100644
--- a/cl/compile_gop_test.go
+++ b/cl/compile_gop_test.go
@@ -20,6 +20,46 @@ import (
 	"testing"
 )
 
+func TestVargCommand(t *testing.T) {
+	gopClTest(t, `
+type foo int
+
+func (f foo) Ls(args ...string) {
+}
+
+var f foo
+f.ls
+`, `package main
+
+type foo int
+
+func (f foo) Ls(args ...string) {
+}
+
+var f foo
+
+func main() {
+	f.Ls()
+}
+`)
+}
+
+func TestCommandInPkg(t *testing.T) {
+	gopClTest(t, `
+func Ls(args ...string) {
+}
+
+ls
+`, `package main
+
+func Ls(args ...string) {
+}
+func main() {
+	Ls()
+}
+`)
+}
+
 func TestFuncAlias(t *testing.T) {
 	gopClTest(t, `
 func Foo(a ...int) {}
@@ -1042,6 +1082,61 @@ func main() {
 `, false)
 }
 
+func TestTypeAsParamsFunc(t *testing.T) {
+	gopMixedClTest(t, "main", `
+package main
+
+import (
+	"fmt"
+	"reflect"
+)
+
+type basetype interface {
+	int | string
+}
+
+func Gopx_Row__0[T basetype](name string) {
+}
+
+func Gopx_Row__1[Array any](v int) {
+}
+
+func Gopx_Col[T any](name string) {
+	fmt.Printf("%v: %s\n", reflect.TypeOf((*T)(nil)).Elem(), name)
+}
+
+type Table struct {
+}
+
+func Gopt_Table_Gopx_Col__0[T basetype](p *Table, name string) {
+}
+
+func Gopt_Table_Gopx_Col__1[Array any](p *Table, v int) {
+}
+`, `
+var tbl *Table
+
+col string, "name"
+col int, "age"
+
+row string, 100
+
+tbl.col string, "foo"
+tbl.col int, 100
+`, `package main
+
+var tbl *Table
+
+func main() {
+	Gopx_Col[string]("name")
+	Gopx_Col[int]("age")
+	Gopx_Row__1[string](100)
+	Gopt_Table_Gopx_Col__0[string](tbl, "foo")
+	Gopt_Table_Gopx_Col__1[int](tbl, 100)
+}
+`)
+}
+
 func Test_RangeExpressionIf_Issue1243(t *testing.T) {
 	gopClTest(t, `
 for i <- :10, i%3 == 0 {
diff --git a/cl/compile_test.go b/cl/compile_test.go
index 9c17a0b5..6c5d68d5 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -122,40 +122,6 @@ func gopClTestFS(t *testing.T, conf *cl.Config, fs parser.FileSystem, pkgname, e
 	}
 }
 
-func TestVargCommand(t *testing.T) {
-	gopClTest(t, `
-type foo int
-
-func (f foo) Ls(args ...string) {
-}
-
-var f foo
-f.ls
-`, `package main
-
-type foo int
-
-func (f foo) Ls(args ...string) {
-}
-
-var f foo
-
-func main() {
-	f.Ls()
-}
-`)
-}
-
-func _TestCommandInPkg(t *testing.T) {
-	gopClTest(t, `
-func Ls(args ...string) {
-}
-
-ls
-`, `
-`)
-}
-
 func Test_CastSlice_Issue1240(t *testing.T) {
 	gopClTest(t, `
 type fvec []float64
diff --git a/testdata/typeasparamsfunc/col.go b/testdata/typeasparamsfunc/col.go
new file mode 100644
index 00000000..292cfe4b
--- /dev/null
+++ b/testdata/typeasparamsfunc/col.go
@@ -0,0 +1,10 @@
+package main
+
+import (
+	"fmt"
+	"reflect"
+)
+
+func Gopx_Col[T any](name string) {
+	fmt.Printf("%v: %s\n", reflect.TypeOf((*T)(nil)).Elem(), name)
+}
diff --git a/testdata/typeasparamsfunc/typeAsParamsFunc.gop b/testdata/typeasparamsfunc/typeAsParamsFunc.gop
new file mode 100644
index 00000000..fed8257f
--- /dev/null
+++ b/testdata/typeasparamsfunc/typeAsParamsFunc.gop
@@ -0,0 +1,2 @@
+col string, "name"
+col int, "age"
diff --git a/testdata/typeasparamsmethod/col.go b/testdata/typeasparamsmethod/col.go
new file mode 100644
index 00000000..e8e01ccc
--- /dev/null
+++ b/testdata/typeasparamsmethod/col.go
@@ -0,0 +1,21 @@
+package main
+
+import (
+	"fmt"
+	"reflect"
+)
+
+type basetype interface {
+	int | string
+}
+
+type Table struct {
+}
+
+func Gopt_Table_Gopx_Col__0[T basetype](p *Table, name string) {
+	fmt.Printf("Gopt_Table_Gopx_Col__0 %v: %s\n", reflect.TypeOf((*T)(nil)).Elem(), name)
+}
+
+func Gopt_Table_Gopx_Col__1[Array any](p *Table, name string) {
+	fmt.Printf("Gopt_Table_Gopx_Col__1 %v: %s\n", reflect.TypeOf((*Array)(nil)).Elem(), name)
+}
diff --git a/testdata/typeasparamsmethod/typeAsParamsMethod.gop b/testdata/typeasparamsmethod/typeAsParamsMethod.gop
new file mode 100644
index 00000000..46c0d353
--- /dev/null
+++ b/testdata/typeasparamsmethod/typeAsParamsMethod.gop
@@ -0,0 +1,4 @@
+var tbl Table
+
+tbl.col int, "age"
+tbl.col bool, "male"