Repository: goplus/gop
Pull request #1743 (2024-02-14 09:51:30)

gmxMainFunc: force remove //line comments for main func


Patch:
diff --git a/README.md b/README.md
index 6115ed5d..8448ffd2 100644
--- a/README.md
+++ b/README.md
@@ -185,13 +185,13 @@ all.bat
 
 * [yap: Yet Another Go/Go+ HTTP Web Framework](https://github.com/goplus/yap)
 
-### HTTP Test Framework
+### HTTP Test
 
 * [yaptest: HTTP Test Framework](https://github.com/goplus/yap/tree/main/ytest)
 
 ### DevOps tools
 
-* [Go+ DevOps Tools](https://github.com/xushiwei/gshexample?tab=readme-ov-file#gsh-example)
+* [Go+ DevOps Tools](https://github.com/goplus/gop/blob/main/doc/dsl-vs-sdf.md#demo-go-devops-tools)
 
 ### Data processing
 
diff --git a/cl/classfile.go b/cl/classfile.go
index 50f062d4..3ebf85e7 100644
--- a/cl/classfile.go
+++ b/cl/classfile.go
@@ -267,10 +267,8 @@ func gmxMainFunc(pkg *gox.Package, ctx *pkgCtx, noAutoGenMain bool) func() {
 				new := pkg.Builtin().Ref("new")
 				cb := fn.BodyStart(pkg).Val(new).Val(o).Call(1).MemberVal("Main")
 
-				// force line of main code = (last comment line + 1)
-				if ctx.lastStmt != nil {
-					commentStmtEx(cb, ctx, ctx.lastStmt, true)
-				}
+				// force remove //line comments for main func
+				cb.SetComments(nil, false)
 
 				sig := cb.Get(-1).Type.(*types.Signature)
 				narg := gmxMainNarg(sig)
diff --git a/cl/compile.go b/cl/compile.go
index f07ee277..9f05b1e2 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -346,8 +346,6 @@ type pkgCtx struct {
 	generics map[string]bool // generic type record
 	idents   []*ast.Ident    // toType ident recored
 	inInst   int             // toType in generic instance
-
-	lastStmt ast.Stmt // set by commentStmt for line:xxx
 }
 
 type pkgImp struct {
diff --git a/cl/stmt.go b/cl/stmt.go
index ca4ac27c..954ca189 100644
--- a/cl/stmt.go
+++ b/cl/stmt.go
@@ -47,25 +47,17 @@ func relFile(dir string, absFile string) string {
 
 func commentStmt(ctx *blockCtx, stmt ast.Stmt) {
 	if ctx.fileLine {
-		ctx.lastStmt = stmt // for gmxMainFunc
-		commentStmtEx(ctx.cb, ctx.pkgCtx, stmt, false)
+		commentStmtEx(ctx.cb, ctx.pkgCtx, stmt)
 	}
 }
 
-func commentStmtEx(cb *gox.CodeBuilder, ctx *pkgCtx, stmt ast.Stmt, next bool) {
-	var start token.Pos
-	var delta int
-	if next {
-		start = stmt.End() - 1
-		delta = 1
-	} else {
-		start = stmt.Pos()
-	}
+func commentStmtEx(cb *gox.CodeBuilder, ctx *pkgCtx, stmt ast.Stmt) {
+	start := stmt.Pos()
 	pos := ctx.fset.Position(start)
 	if ctx.relBaseDir != "" {
 		pos.Filename = fileLineFile(ctx.relBaseDir, pos.Filename)
 	}
-	line := fmt.Sprintf("\n//line %s:%d:1", pos.Filename, pos.Line+delta)
+	line := fmt.Sprintf("\n//line %s:%d:1", pos.Filename, pos.Line)
 	comments := &goast.CommentGroup{
 		List: []*goast.Comment{{Text: line}},
 	}


Test patch:
diff --git a/cl/compile_test.go b/cl/compile_test.go
index 6cd295c3..c75e737a 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -122,6 +122,18 @@ func gopClTestFS(t *testing.T, conf *cl.Config, fs parser.FileSystem, pkgname, e
 	}
 }
 
+func TestTypeDoc(t *testing.T) {
+	gopClTest(t, `
+type (
+	// doc
+	A int
+)
+`, `package main
+// doc
+type A int
+`)
+}
+
 func TestUnsafe(t *testing.T) {
 	gopClTest(t, `
 import "unsafe"