Repository: goplus/gop
Pull request #1656 (2024-01-22 04:38:48)

typeparams cast


Patch:
diff --git a/cl/compile.go b/cl/compile.go
index 54a9c21c..edad1258 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -193,6 +193,9 @@ type Config struct {
 	// See (*github.com/goplus/mod/gopmod.Module).LookupClass.
 	LookupClass func(ext string) (c *Project, ok bool)
 
+	// IsPkgtStandard checks a pkgPath is a Go standard package or not.
+	IsPkgtStandard func(pkgPath string) bool
+
 	// An Importer resolves import paths to Packages (optional).
 	Importer types.Importer
 
@@ -483,6 +486,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
 		Types:           conf.Types,
 		Fset:            fset,
 		Context:         conf.Context,
+		IsPkgtStandard:  conf.IsPkgtStandard,
 		Importer:        conf.Importer,
 		LoadNamed:       ctx.loadNamed,
 		HandleErr:       ctx.handleErr,
diff --git a/cl/outline/outline.go b/cl/outline/outline.go
index 7adab5ac..9e97dadd 100644
--- a/cl/outline/outline.go
+++ b/cl/outline/outline.go
@@ -47,6 +47,9 @@ type Config struct {
 	// LookupClass lookups a class by specified file extension.
 	LookupClass func(ext string) (c *Project, ok bool)
 
+	// IsPkgtStandard checks a pkgPath is a Go standard package or not.
+	IsPkgtStandard func(pkgPath string) bool
+
 	// An Importer resolves import paths to Packages.
 	Importer types.Importer
 }
@@ -63,6 +66,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (_ Package, err
 		C2goBase:       conf.C2goBase,
 		LookupPub:      conf.LookupPub,
 		LookupClass:    conf.LookupClass,
+		IsPkgtStandard: conf.IsPkgtStandard,
 		Importer:       conf.Importer,
 		NoFileLine:     true,
 		NoAutoGenMain:  true,
diff --git a/gengo.go b/gengo.go
index 967bc478..79100c0e 100644
--- a/gengo.go
+++ b/gengo.go
@@ -24,7 +24,6 @@ import (
 	"strings"
 	"syscall"
 
-	"github.com/goplus/gox"
 	"github.com/goplus/mod/gopmod"
 	"github.com/goplus/mod/modcache"
 	"github.com/goplus/mod/modfetch"
@@ -69,9 +68,6 @@ func genGoDir(dir string, conf *Config, genTestPkg, recursively bool, flags GenF
 	if conf == nil {
 		conf = new(Config)
 	}
-	if false && conf.Context == nil { // set Context
-		conf.Context = gox.NewContext()
-	}
 	if recursively {
 		var (
 			list errors.List
@@ -280,9 +276,6 @@ func GenGoFiles(autogen string, files []string, conf *Config) (result []string,
 	if conf == nil {
 		conf = new(Config)
 	}
-	if conf.Context == nil { // set Context
-		conf.Context = gox.NewContext()
-	}
 	if autogen == "" {
 		autogen = "gop_autogen.go"
 		if len(files) == 1 {
diff --git a/go.mod b/go.mod
index c59fbc53..cb5cf206 100644
--- a/go.mod
+++ b/go.mod
@@ -5,8 +5,8 @@ go 1.18
 require (
 	github.com/fsnotify/fsnotify v1.7.0
 	github.com/goplus/c2go v0.7.20-0.20240115161159-762f74054b17
-	github.com/goplus/gox v1.13.1-0.20240121122555-748a917ccef7
-	github.com/goplus/mod v0.12.2-0.20240107203906-5044606d0c51
+	github.com/goplus/gox v1.13.1-0.20240122040255-a954f8e0b9c1
+	github.com/goplus/mod v0.12.2-0.20240122042636-7db361b76b12
 	github.com/qiniu/x v1.13.2
 	golang.org/x/tools v0.17.0
 )
diff --git a/go.sum b/go.sum
index 2ecf5fc2..41c036f3 100644
--- a/go.sum
+++ b/go.sum
@@ -7,10 +7,11 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
 github.com/goplus/c2go v0.7.20-0.20240115161159-762f74054b17 h1:2+AWW9KjzTwaM8pfvRZqhI3PCXs7cMxzsn98AqDpgH0=
 github.com/goplus/c2go v0.7.20-0.20240115161159-762f74054b17/go.mod h1:WFbERV86IVV4wJ2/mLa8756Fy6iSXtlwGQbEqkYoIR4=
 github.com/goplus/gox v1.13.1-0.20240115155941-e657d899cb2e/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
-github.com/goplus/gox v1.13.1-0.20240121122555-748a917ccef7 h1:ld6FOGBInJLgnneCcrKoqDm2oMg+sf0kc9pjr770U6E=
-github.com/goplus/gox v1.13.1-0.20240121122555-748a917ccef7/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
-github.com/goplus/mod v0.12.2-0.20240107203906-5044606d0c51 h1:SXuaj7lYEL2fAQTrKzbCAUuRre2s2nMfGEyBqbUWooc=
+github.com/goplus/gox v1.13.1-0.20240122040255-a954f8e0b9c1 h1:14sIjKTXsqx2jfwKxDw+damXZ5lk3bTF27AsOVm18tk=
+github.com/goplus/gox v1.13.1-0.20240122040255-a954f8e0b9c1/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
 github.com/goplus/mod v0.12.2-0.20240107203906-5044606d0c51/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs=
+github.com/goplus/mod v0.12.2-0.20240122042636-7db361b76b12 h1:oQX3E8MWcXznTsQ6mzXYXGbniwyfKW4GtPl5vSdOr9k=
+github.com/goplus/mod v0.12.2-0.20240122042636-7db361b76b12/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs=
 github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
 github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
 github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
diff --git a/load.go b/load.go
index f5706c99..79890fb4 100644
--- a/load.go
+++ b/load.go
@@ -186,12 +186,13 @@ func LoadDir(dir string, conf *Config, genTestPkg bool, promptGenGo ...bool) (ou
 
 	var pkgTest *ast.Package
 	var clConf = &cl.Config{
-		Fset:         fset,
-		Context:      conf.Context,
-		RelativeBase: relativeBaseOf(mod),
-		Importer:     imp,
-		LookupClass:  mod.LookupClass,
-		LookupPub:    c2go.LookupPub(mod),
+		Fset:           fset,
+		Context:        conf.Context,
+		RelativeBase:   relativeBaseOf(mod),
+		Importer:       imp,
+		IsPkgtStandard: mod.IsPkgtStandard,
+		LookupClass:    mod.LookupClass,
+		LookupPub:      c2go.LookupPub(mod),
 	}
 
 	for name, pkg := range pkgs {
@@ -271,12 +272,13 @@ func LoadFiles(dir string, files []string, conf *Config) (out *gox.Package, err
 			imp = NewImporter(mod, gop, fset)
 		}
 		clConf := &cl.Config{
-			Fset:         fset,
-			Context:      conf.Context,
-			RelativeBase: relativeBaseOf(mod),
-			Importer:     imp,
-			LookupClass:  mod.LookupClass,
-			LookupPub:    c2go.LookupPub(mod),
+			Fset:           fset,
+			Context:        conf.Context,
+			RelativeBase:   relativeBaseOf(mod),
+			Importer:       imp,
+			LookupClass:    mod.LookupClass,
+			IsPkgtStandard: mod.IsPkgtStandard,
+			LookupPub:      c2go.LookupPub(mod),
 		}
 		out, err = cl.NewPackage("", pkg, clConf)
 		if err != nil {
diff --git a/x/typesutil/check.go b/x/typesutil/check.go
index 0f4d10a0..f6803f47 100644
--- a/x/typesutil/check.go
+++ b/x/typesutil/check.go
@@ -151,6 +151,7 @@ func (p *Checker) Files(goFiles []*goast.File, gopFiles []*ast.File) (err error)
 		C2goBase:       opts.C2goBase,
 		LookupPub:      c2go.LookupPub(mod),
 		LookupClass:    mod.LookupClass,
+		IsPkgtStandard: mod.IsPkgtStandard,
 		Importer:       newImporter(conf.Importer, mod, nil, fset),
 		Recorder:       gopRecorder{p.gopInfo},
 		NoFileLine:     true,


Test patch:
diff --git a/testdata/typeparamscast/foo.go b/testdata/typeparamscast/foo.go
new file mode 100644
index 00000000..b2d304f1
--- /dev/null
+++ b/testdata/typeparamscast/foo.go
@@ -0,0 +1,23 @@
+package main
+
+type M = map[string]any
+
+type basetype interface {
+	string | int | bool | float64
+}
+
+type Var__0[T basetype] struct {
+	Val T
+}
+
+type Var__1[T map[string]any] struct {
+	Val T
+}
+
+func Gopx_Var_Cast__0[T basetype]() *Var__0[T] {
+	return new(Var__0[T])
+}
+
+func Gopx_Var_Cast__1[T map[string]any]() *Var__1[T] {
+	return new(Var__1[T])
+}
diff --git a/testdata/typeparamscast/typecast.gop b/testdata/typeparamscast/typecast.gop
new file mode 100644
index 00000000..c3c95e53
--- /dev/null
+++ b/testdata/typeparamscast/typecast.gop
@@ -0,0 +1,1 @@
+println Var(int)