Repository: goplus/gop
Pull request #1116 (2022-02-07 06:50:14)

genDefaultGopMod bugfix


Patch:
diff --git a/cl/compile.go b/cl/compile.go
index 4fe078a1..9cddd01c 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -96,9 +96,6 @@ type Config struct {
 
 	// RelativePath = true means to generate file line comments with relative file path.
 	RelativePath bool
-
-	// NoAutoGenMain = true means not to auto generate main func is no entry.
-	NoAutoGenMain bool
 }
 
 type nodeInterp struct {
@@ -399,15 +396,6 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
 		load()
 	}
 	err = ctx.complete()
-
-	if !conf.NoAutoGenMain && pkg.Name == "main" {
-		if obj := p.Types.Scope().Lookup("main"); obj == nil {
-			old := p.SetInTestingFile(false)
-			p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).End()
-			p.SetInTestingFile(old)
-		}
-	}
-
 	return
 }
 
@@ -554,26 +542,6 @@ func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targe
 			},
 		}}}
 	}
-	// check class project no MainEntry and auto added
-	if !conf.NoAutoGenMain && f.IsProj && !f.NoEntrypoint && f.Name.Name == "main" {
-		entry := getEntrypoint(f, false)
-		var hasEntry bool
-		for _, decl := range f.Decls {
-			switch d := decl.(type) {
-			case *ast.FuncDecl:
-				if d.Name.Name == entry {
-					hasEntry = true
-				}
-			}
-		}
-		if !hasEntry {
-			f.Decls = append(f.Decls, &ast.FuncDecl{
-				Name: ast.NewIdent(entry),
-				Type: &ast.FuncType{Params: &ast.FieldList{}},
-				Body: &ast.BlockStmt{},
-			})
-		}
-	}
 	for _, decl := range f.Decls {
 		switch d := decl.(type) {
 		case *ast.FuncDecl:
diff --git a/cl/func_type_and_var.go b/cl/func_type_and_var.go
index 632fadd0..f24de2da 100644
--- a/cl/func_type_and_var.go
+++ b/cl/func_type_and_var.go
@@ -209,36 +209,15 @@ func toStructType(ctx *blockCtx, v *ast.StructType) *types.Struct {
 	fieldList := v.Fields.List
 	fields := make([]*types.Var, 0, len(fieldList))
 	tags := make([]string, 0, len(fieldList))
-	names := make(map[string]token.Pos)
-	chkRedecl := func(name string, pos token.Pos) bool {
-		if name == "_" {
-			return false
-		}
-		if opos, ok := names[name]; ok {
-			npos := ctx.Position(pos)
-			ctx.handleCodeErrorf(&npos, "%v redeclared\n\t%v other declaration of %v",
-				name, ctx.Position(opos), name)
-			return true
-		}
-		names[name] = pos
-		return false
-	}
 	for _, field := range fieldList {
 		typ := toType(ctx, field.Type)
 		if field.Names == nil { // embedded
-			name := getTypeName(typ)
-			if chkRedecl(name, field.Type.Pos()) {
-				continue
-			}
-			fld := types.NewField(token.NoPos, pkg, name, typ, true)
+			fld := types.NewField(token.NoPos, pkg, getTypeName(typ), typ, true)
 			fields = append(fields, fld)
 			tags = append(tags, toFieldTag(field.Tag))
 			continue
 		}
 		for _, name := range field.Names {
-			if chkRedecl(name.Name, name.NamePos) {
-				continue
-			}
 			fld := types.NewField(token.NoPos, pkg, name.Name, typ, false)
 			fields = append(fields, fld)
 			tags = append(tags, toFieldTag(field.Tag))
diff --git a/parser/parser.go b/parser/parser.go
index 304387a6..fab81edf 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -1588,9 +1588,6 @@ func (p *parser) parseCallOrConversion(fun ast.Expr, isCmd bool) *ast.CallExpr {
 				break
 			}
 		}
-		if isCmd && p.tok == token.RBRACE {
-			break
-		}
 		if !p.atComma("argument list", endTok) {
 			break
 		}
diff --git a/x/gopproj/projctx.go b/x/gopproj/projctx.go
index 1262007f..dc74da86 100644
--- a/x/gopproj/projctx.go
+++ b/x/gopproj/projctx.go
@@ -211,10 +211,17 @@ func runCommand(dir, command string, args ...string) error {
 func genDefaultGopMod(modfile string) {
 	dir, _ := filepath.Split(modfile)
 	dummy := dir + "dummy"
+	tmodfile := dummy + "/go.mod"
 	os.MkdirAll(dummy, 0755)
-	genGomodFile(modfile)
+	genGomodFile(tmodfile)
 	genDummyProject(dummy)
-	execCommand(dir, "go", "mod", "tidy")
+	execCommand(dummy, "go", "mod", "tidy")
+	os.Rename(tmodfile, modfile)
+	os.Rename(toGoSum(tmodfile), toGoSum(modfile))
+}
+
+func toGoSum(modfile string) string {
+	return modfile[:len(modfile)-3] + "sum"
 }
 
 // -----------------------------------------------------------------------------


Test patch:
diff --git a/cl/compile_spx_test.go b/cl/compile_spx_test.go
index 9bf77e2c..7c5c264f 100644
--- a/cl/compile_spx_test.go
+++ b/cl/compile_spx_test.go
@@ -70,10 +70,6 @@ func gopSpxTest(t *testing.T, gmx, spxcode, expected string) {
 }
 
 func gopSpxTestEx(t *testing.T, gmx, spxcode, expected, gmxfile, spxfile string) {
-	gopSpxTestExConf(t, gblConf, gmx, spxcode, expected, gmxfile, spxfile)
-}
-
-func gopSpxTestExConf(t *testing.T, conf *cl.Config, gmx, spxcode, expected, gmxfile, spxfile string) {
 	cl.SetDisableRecover(true)
 	defer cl.SetDisableRecover(false)
 
@@ -84,7 +80,7 @@ func gopSpxTestExConf(t *testing.T, conf *cl.Config, gmx, spxcode, expected, gmx
 		t.Fatal("ParseFSDir:", err)
 	}
 	bar := pkgs["main"]
-	pkg, err := cl.NewPackage("", bar, conf)
+	pkg, err := cl.NewPackage("", bar, gblConf)
 	if err != nil {
 		t.Fatal("NewPackage:", err)
 	}
@@ -396,91 +392,3 @@ func (this *Kai) onMsg(msg string) {
 }
 `, "Game.t2gmx", "Kai.t2spx")
 }
-
-func TestSpxMainEntry(t *testing.T) {
-	conf := *gblConf
-	conf.NoAutoGenMain = false
-
-	gopSpxTestExConf(t, &conf, `
-`, `
-`, `package main
-
-import spx2 "github.com/goplus/gop/cl/internal/spx2"
-
-type Game struct {
-	spx2.Game
-}
-
-func (this *Game) MainEntry() {
-}
-func main() {
-	new(Game).Main()
-}
-`, "Game.t2gmx", "Kai.t2spx")
-	gopSpxTestExConf(t, &conf, `
-var (
-	Kai Kai
-)
-`, `
-`, `package main
-
-import spx2 "github.com/goplus/gop/cl/internal/spx2"
-
-type Game struct {
-	spx2.Game
-	Kai Kai
-}
-type Kai struct {
-	spx2.Sprite
-	*Game
-}
-
-func (this *Game) MainEntry() {
-}
-func main() {
-	new(Game).Main()
-}
-`, "Game.t2gmx", "Kai.t2spx")
-
-	gopSpxTestExConf(t, &conf, `
-var (
-	Kai Kai
-)
-func MainEntry() {
-	println "Hi"
-}
-`, `
-func Main() {
-	println "Hello"
-}
-func onMsg(msg string) {
-}
-`, `package main
-
-import (
-	fmt "fmt"
-	spx2 "github.com/goplus/gop/cl/internal/spx2"
-)
-
-type Game struct {
-	spx2.Game
-	Kai Kai
-}
-type Kai struct {
-	spx2.Sprite
-	*Game
-}
-
-func (this *Game) MainEntry() {
-	fmt.Println("Hi")
-}
-func main() {
-	new(Game).Main()
-}
-func (this *Kai) Main() {
-	fmt.Println("Hello")
-}
-func (this *Kai) onMsg(msg string) {
-}
-`, "Game.t2gmx", "Kai.t2spx")
-}
diff --git a/cl/compile_test.go b/cl/compile_test.go
index 8f2fcc91..f52f74c8 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -60,19 +60,18 @@ func init() {
 		panic(err)
 	}
 	gblConf = &cl.Config{
-		Fset:          gblFset,
-		Importer:      imp,
-		LookupClass:   lookupClass,
-		NoFileLine:    true,
-		NoAutoGenMain: true,
+		Fset:        gblFset,
+		Importer:    imp,
+		LookupClass: lookupClass,
+		NoFileLine:  true,
 	}
 }
 
 func gopClTest(t *testing.T, gopcode, expected string) {
-	gopClTestEx(t, gblConf, "main", gopcode, expected)
+	gopClTestEx(t, "main", gopcode, expected)
 }
 
-func gopClTestEx(t *testing.T, conf *cl.Config, pkgname, gopcode, expected string) {
+func gopClTestEx(t *testing.T, pkgname, gopcode, expected string) {
 	cl.SetDisableRecover(true)
 	defer cl.SetDisableRecover(false)
 
@@ -83,7 +82,7 @@ func gopClTestEx(t *testing.T, conf *cl.Config, pkgname, gopcode, expected strin
 		t.Fatal("ParseFSDir:", err)
 	}
 	bar := pkgs[pkgname]
-	pkg, err := cl.NewPackage("github.com/goplus/gop/cl", bar, conf)
+	pkg, err := cl.NewPackage("github.com/goplus/gop/cl", bar, gblConf)
 	if err != nil {
 		t.Fatal("NewPackage:", err)
 	}
@@ -3516,7 +3515,7 @@ func main() {
 	fmt.Println("init")
 }
 `)
-	gopClTestEx(t, gblConf, "bar", `package bar
+	gopClTestEx(t, "bar", `package bar
 println("init")
 `, `package bar
 
@@ -3620,42 +3619,3 @@ func main() {
 }
 `)
 }
-
-func TestMainEntry(t *testing.T) {
-	conf := *gblConf
-	conf.NoAutoGenMain = false
-
-	gopClTestEx(t, &conf, "main", `
-`, `package main
-
-func main() {
-}
-`)
-	gopClTestEx(t, &conf, "main", `
-func test() {
-	println "hello"
-}
-`, `package main
-
-import fmt "fmt"
-
-func test() {
-	fmt.Println("hello")
-}
-func main() {
-}
-`)
-
-	gopClTestEx(t, &conf, "main", `
-func main() {
-	println "hello"
-}
-`, `package main
-
-import fmt "fmt"
-
-func main() {
-	fmt.Println("hello")
-}
-`)
-}
diff --git a/cl/error_msg_test.go b/cl/error_msg_test.go
index bdba2d13..07563b08 100644
--- a/cl/error_msg_test.go
+++ b/cl/error_msg_test.go
@@ -806,22 +806,3 @@ import "fmt"
 n, err := fmt.println
 `)
 }
-
-func TestFiledsNameRedecl(t *testing.T) {
-	codeErrorTest(t, `./bar.gop:6:2: Id redeclared
-	./bar.gop:5:2 other declaration of Id
-./bar.gop:7:2: Id redeclared
-	./bar.gop:5:2 other declaration of Id
-./bar.gop:9:2: name redeclared
-	./bar.gop:8:2 other declaration of name`, `
-type Id struct {
-}
-type A struct {
-	Id   int
-	Id   string
-	Id
-	name string
-	name string
-}
-`)
-}
diff --git a/parser/_testdata/cmdlinestyle4/cmd4.gop b/parser/_testdata/cmdlinestyle4/cmd4.gop
deleted file mode 100644
index b1d457c4..00000000
--- a/parser/_testdata/cmdlinestyle4/cmd4.gop
+++ /dev/null
@@ -1,5 +0,0 @@
-func call(fn func(x int)) { fn(100) }
-
-call(func(x int) { println })
-call(func(x int) { println x })
-call(func(x int) { println {"x": 100, "y": 200}, x })
diff --git a/parser/_testdata/cmdlinestyle4/parser.expect b/parser/_testdata/cmdlinestyle4/parser.expect
deleted file mode 100644
index faec429d..00000000
--- a/parser/_testdata/cmdlinestyle4/parser.expect
+++ /dev/null
@@ -1,164 +0,0 @@
-package main
-
-file cmd4.gop
-noEntrypoint
-ast.FuncDecl:
-  Name:
-    ast.Ident:
-      Name: call
-  Type:
-    ast.FuncType:
-      Params:
-        ast.FieldList:
-          List:
-            ast.Field:
-              Names:
-                ast.Ident:
-                  Name: fn
-              Type:
-                ast.FuncType:
-                  Params:
-                    ast.FieldList:
-                      List:
-                        ast.Field:
-                          Names:
-                            ast.Ident:
-                              Name: x
-                          Type:
-                            ast.Ident:
-                              Name: int
-  Body:
-    ast.BlockStmt:
-      List:
-        ast.ExprStmt:
-          X:
-            ast.CallExpr:
-              Fun:
-                ast.Ident:
-                  Name: fn
-              Args:
-                ast.BasicLit:
-                  Kind: INT
-                  Value: 100
-ast.FuncDecl:
-  Name:
-    ast.Ident:
-      Name: main
-  Type:
-    ast.FuncType:
-      Params:
-        ast.FieldList:
-  Body:
-    ast.BlockStmt:
-      List:
-        ast.ExprStmt:
-          X:
-            ast.CallExpr:
-              Fun:
-                ast.Ident:
-                  Name: call
-              Args:
-                ast.FuncLit:
-                  Type:
-                    ast.FuncType:
-                      Params:
-                        ast.FieldList:
-                          List:
-                            ast.Field:
-                              Names:
-                                ast.Ident:
-                                  Name: x
-                              Type:
-                                ast.Ident:
-                                  Name: int
-                  Body:
-                    ast.BlockStmt:
-                      List:
-                        ast.ExprStmt:
-                          X:
-                            ast.Ident:
-                              Name: println
-        ast.ExprStmt:
-          X:
-            ast.CallExpr:
-              Fun:
-                ast.Ident:
-                  Name: call
-              Args:
-                ast.FuncLit:
-                  Type:
-                    ast.FuncType:
-                      Params:
-                        ast.FieldList:
-                          List:
-                            ast.Field:
-                              Names:
-                                ast.Ident:
-                                  Name: x
-                              Type:
-                                ast.Ident:
-                                  Name: int
-                  Body:
-                    ast.BlockStmt:
-                      List:
-                        ast.ExprStmt:
-                          X:
-                            ast.CallExpr:
-                              Fun:
-                                ast.Ident:
-                                  Name: println
-                              Args:
-                                ast.Ident:
-                                  Name: x
-        ast.ExprStmt:
-          X:
-            ast.CallExpr:
-              Fun:
-                ast.Ident:
-                  Name: call
-              Args:
-                ast.FuncLit:
-                  Type:
-                    ast.FuncType:
-                      Params:
-                        ast.FieldList:
-                          List:
-                            ast.Field:
-                              Names:
-                                ast.Ident:
-                                  Name: x
-                              Type:
-                                ast.Ident:
-                                  Name: int
-                  Body:
-                    ast.BlockStmt:
-                      List:
-                        ast.ExprStmt:
-                          X:
-                            ast.CallExpr:
-                              Fun:
-                                ast.Ident:
-                                  Name: println
-                              Args:
-                                ast.CompositeLit:
-                                  Elts:
-                                    ast.KeyValueExpr:
-                                      Key:
-                                        ast.BasicLit:
-                                          Kind: STRING
-                                          Value: "x"
-                                      Value:
-                                        ast.BasicLit:
-                                          Kind: INT
-                                          Value: 100
-                                    ast.KeyValueExpr:
-                                      Key:
-                                        ast.BasicLit:
-                                          Kind: STRING
-                                          Value: "y"
-                                      Value:
-                                        ast.BasicLit:
-                                          Kind: INT
-                                          Value: 200
-                                ast.Ident:
-                                  Name: x