Repository: goplus/gop
Pull request #2280 (2025-04-11 23:48:47)

cl preloadGopFile: ensure game type is loaded


Patch:
diff --git a/cl/compile.go b/cl/compile.go
index 7289376c..15845d5f 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -810,7 +810,9 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c
 				}
 				if sp != nil && !goxTestFile {
 					if gameClass != "" {
-						typ := toType(ctx, &ast.StarExpr{X: &ast.Ident{Name: gameClass}})
+						typ := toType(ctx, &ast.Ident{Name: gameClass})
+						getUnderlying(ctx, typ) // ensure type is loaded
+						typ = types.NewPointer(typ)
 						name := getTypeName(typ)
 						if !chk.chkRedecl(ctx, name, pos) {
 							fld := types.NewField(pos, pkg, name, typ, true)


Test patch:
diff --git a/cl/_testspx/nogame/bar.tspx b/cl/_testspx/nogame/bar.tspx
new file mode 100644
index 00000000..e69de29b
diff --git a/cl/_testspx/nogame/out.go b/cl/_testspx/nogame/out.go
new file mode 100644
index 00000000..33ff7a8e
--- /dev/null
+++ b/cl/_testspx/nogame/out.go
@@ -0,0 +1,20 @@
+package main
+
+import "github.com/goplus/gop/cl/internal/spx"
+
+type bar struct {
+	spx.Sprite
+	*MyGame
+}
+type MyGame struct {
+	*spx.MyGame
+}
+
+func (this *MyGame) Main() {
+	spx.Gopt_MyGame_Main(this)
+}
+func (this *bar) Main() {
+}
+func main() {
+	new(MyGame).Main()
+}
diff --git a/cl/compile_spx_test.go b/cl/compile_spx_test.go
index 97c5cea0..1e45651d 100644
--- a/cl/compile_spx_test.go
+++ b/cl/compile_spx_test.go
@@ -43,31 +43,6 @@ func gopSpxErrorTestEx(t *testing.T, msg, gmx, spxcode, gmxfile, spxfile string)
 	cltest.SpxErrorEx(t, msg, gmx, spxcode, gmxfile, spxfile)
 }
 
-func TestSpxNoGame(t *testing.T) {
-	gopSpxTestEx(t, ``, `
-`, `package main
-
-import "github.com/goplus/gop/cl/internal/spx"
-
-type Kai struct {
-	spx.Sprite
-	*MyGame
-}
-type MyGame struct {
-	*spx.MyGame
-}
-
-func (this *Kai) Main() {
-}
-func (this *MyGame) Main() {
-	spx.Gopt_MyGame_Main(this)
-}
-func main() {
-	new(MyGame).Main()
-}
-`, "", "Kai.tspx")
-}
-
 func TestSpxError(t *testing.T) {
 	gopSpxErrorTestEx(t, `Game.tgmx:6:2: userScore redeclared
 	Game.tgmx:5:2 other declaration of userScore`, `
diff --git a/x/build/build_test.go b/x/build/build_test.go
index cdc10801..3c03bbb5 100644
--- a/x/build/build_test.go
+++ b/x/build/build_test.go
@@ -407,12 +407,12 @@ type MyGame struct {
 	spx.MyGame
 }
 
-func (this *Cat) Main() {
-	fmt.Println("hi")
-}
 func (this *MyGame) Main() {
 	spx.Gopt_MyGame_Main(this)
 }
+func (this *Cat) Main() {
+	fmt.Println("hi")
+}
 func main() {
 	new(MyGame).Main()
 }
diff --git a/x/typesutil/info_test.go b/x/typesutil/info_test.go
index ffcce113..15942644 100644
--- a/x/typesutil/info_test.go
+++ b/x/typesutil/info_test.go
@@ -1726,35 +1726,34 @@ func onCloned() {
 	say("Hi")
 }
 `, `== types ==
-000:  0: 0 | *MyGame             *ast.StarExpr                  | type    : *main.MyGame | type
-001:  0: 0 | Kai                 *ast.Ident                     | type    : main.Kai | type
-002:  0: 0 | MyGame              *ast.Ident                     | type    : main.MyGame | type
-003:  3: 4 | int                 *ast.Ident                     | type    : int | type
-004:  6:11 | struct {
+000:  0: 0 | Kai                 *ast.Ident                     | type    : main.Kai | type
+001:  0: 0 | MyGame              *ast.Ident                     | type    : main.MyGame | type
+002:  3: 4 | int                 *ast.Ident                     | type    : int | type
+003:  6:11 | struct {
 	x int
 	y int
 } *ast.StructType                | type    : struct{x int; y int} | type
-005:  7: 4 | int                 *ast.Ident                     | type    : int | type
-006:  8: 4 | int                 *ast.Ident                     | type    : int | type
-007: 12: 2 | a                   *ast.Ident                     | var     : int | variable
-008: 12: 6 | 1                   *ast.BasicLit                  | value   : untyped int = 1 | constant
-009: 13: 2 | clone               *ast.Ident                     | value   : func(sprite any) | value
-010: 14: 2 | clone               *ast.Ident                     | value   : func(sprite any, data any) | value
-011: 14: 2 | clone info{1, 2}    *ast.CallExpr                  | void    : () | no value
-012: 14: 8 | info                *ast.Ident                     | type    : main.info | type
-013: 14: 8 | info{1, 2}          *ast.CompositeLit              | value   : main.info | value
-014: 14:13 | 1                   *ast.BasicLit                  | value   : untyped int = 1 | constant
-015: 14:15 | 2                   *ast.BasicLit                  | value   : untyped int = 2 | constant
-016: 15: 2 | clone               *ast.Ident                     | value   : func(sprite any, data any) | value
-017: 15: 2 | clone &info{1, 2}   *ast.CallExpr                  | void    : () | no value
-018: 15: 8 | &info{1, 2}         *ast.UnaryExpr                 | value   : *main.info | value
-019: 15: 9 | info                *ast.Ident                     | type    : main.info | type
-020: 15: 9 | info{1, 2}          *ast.CompositeLit              | value   : main.info | value
-021: 15:14 | 1                   *ast.BasicLit                  | value   : untyped int = 1 | constant
-022: 15:16 | 2                   *ast.BasicLit                  | value   : untyped int = 2 | constant
-023: 19: 2 | say                 *ast.Ident                     | value   : func(msg string, secs ...float64) | value
-024: 19: 2 | say("Hi")           *ast.CallExpr                  | void    : () | no value
-025: 19: 6 | "Hi"                *ast.BasicLit                  | value   : untyped string = "Hi" | constant
+004:  7: 4 | int                 *ast.Ident                     | type    : int | type
+005:  8: 4 | int                 *ast.Ident                     | type    : int | type
+006: 12: 2 | a                   *ast.Ident                     | var     : int | variable
+007: 12: 6 | 1                   *ast.BasicLit                  | value   : untyped int = 1 | constant
+008: 13: 2 | clone               *ast.Ident                     | value   : func(sprite any) | value
+009: 14: 2 | clone               *ast.Ident                     | value   : func(sprite any, data any) | value
+010: 14: 2 | clone info{1, 2}    *ast.CallExpr                  | void    : () | no value
+011: 14: 8 | info                *ast.Ident                     | type    : main.info | type
+012: 14: 8 | info{1, 2}          *ast.CompositeLit              | value   : main.info | value
+013: 14:13 | 1                   *ast.BasicLit                  | value   : untyped int = 1 | constant
+014: 14:15 | 2                   *ast.BasicLit                  | value   : untyped int = 2 | constant
+015: 15: 2 | clone               *ast.Ident                     | value   : func(sprite any, data any) | value
+016: 15: 2 | clone &info{1, 2}   *ast.CallExpr                  | void    : () | no value
+017: 15: 8 | &info{1, 2}         *ast.UnaryExpr                 | value   : *main.info | value
+018: 15: 9 | info                *ast.Ident                     | type    : main.info | type
+019: 15: 9 | info{1, 2}          *ast.CompositeLit              | value   : main.info | value
+020: 15:14 | 1                   *ast.BasicLit                  | value   : untyped int = 1 | constant
+021: 15:16 | 2                   *ast.BasicLit                  | value   : untyped int = 2 | constant
+022: 19: 2 | say                 *ast.Ident                     | value   : func(msg string, secs ...float64) | value
+023: 19: 2 | say("Hi")           *ast.CallExpr                  | void    : () | no value
+024: 19: 6 | "Hi"                *ast.BasicLit                  | value   : untyped string = "Hi" | constant
 == defs ==
 000:  0: 0 | Main                | func (*main.Kai).Main()
 001:  0: 0 | this                | var this *main.Kai