Repository: goplus/gop
Pull request #2293 (2025-04-17 12:07:07)

cl: loadFuncBody set comments nil


Patch:
diff --git a/cl/compile.go b/cl/compile.go
index 15845d5f..385b6e22 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -1433,6 +1433,7 @@ var unaryGopNames = map[string]string{
 
 func loadFuncBody(ctx *blockCtx, fn *gogen.Func, body *ast.BlockStmt, sigBase *types.Signature, src ast.Node) {
 	cb := fn.BodyStart(ctx.pkg, body)
+	cb.SetComments(nil, false)
 	if sigBase != nil {
 		// this.Sprite.Main(...) or this.Game.MainEntry(...)
 		cb.VarVal("this").MemberVal(ctx.baseClass.Name()).MemberVal(fn.Name())


Test patch:
diff --git a/cl/compile_test.go b/cl/compile_test.go
index 89e0476b..3cabd154 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -4340,3 +4340,132 @@ func main() {
 }
 `)
 }
+
+func TestCommentFunc(t *testing.T) {
+	gopClTestEx(t, gblConfLine, "main", `
+import (
+	"strconv"
+)
+
+func add(x, y string) (int, error) {
+	return strconv.atoi(x)? + strconv.atoi(y)?, nil
+}
+
+func addSafe(x, y string) int {
+	return strconv.atoi(x)?:0 + strconv.atoi(y)?:0
+}
+
+echo add("100", "23")!
+
+sum, err := add("10", "abc")
+echo sum, err
+
+echo addSafe("10", "abc")
+`, `package main
+
+import (
+	"fmt"
+	"github.com/qiniu/x/errors"
+	"strconv"
+)
+//line /foo/bar.gop:6:1
+func add(x string, y string) (int, error) {
+//line /foo/bar.gop:7:1
+	var _autoGo_1 int
+//line /foo/bar.gop:7:1
+	{
+//line /foo/bar.gop:7:1
+		var _gop_err error
+//line /foo/bar.gop:7:1
+		_autoGo_1, _gop_err = strconv.Atoi(x)
+//line /foo/bar.gop:7:1
+		if _gop_err != nil {
+//line /foo/bar.gop:7:1
+			_gop_err = errors.NewFrame(_gop_err, "strconv.atoi(x)", "/foo/bar.gop", 7, "main.add")
+//line /foo/bar.gop:7:1
+			return 0, _gop_err
+		}
+//line /foo/bar.gop:7:1
+		goto _autoGo_2
+	_autoGo_2:
+//line /foo/bar.gop:7:1
+	}
+//line /foo/bar.gop:7:1
+	var _autoGo_3 int
+//line /foo/bar.gop:7:1
+	{
+//line /foo/bar.gop:7:1
+		var _gop_err error
+//line /foo/bar.gop:7:1
+		_autoGo_3, _gop_err = strconv.Atoi(y)
+//line /foo/bar.gop:7:1
+		if _gop_err != nil {
+//line /foo/bar.gop:7:1
+			_gop_err = errors.NewFrame(_gop_err, "strconv.atoi(y)", "/foo/bar.gop", 7, "main.add")
+//line /foo/bar.gop:7:1
+			return 0, _gop_err
+		}
+//line /foo/bar.gop:7:1
+		goto _autoGo_4
+	_autoGo_4:
+//line /foo/bar.gop:7:1
+	}
+//line /foo/bar.gop:7:1
+	return _autoGo_1 + _autoGo_3, nil
+}
+//line /foo/bar.gop:10:1
+func addSafe(x string, y string) int {
+//line /foo/bar.gop:11:1
+	return func() (_gop_ret int) {
+//line /foo/bar.gop:11:1
+		var _gop_err error
+//line /foo/bar.gop:11:1
+		_gop_ret, _gop_err = strconv.Atoi(x)
+//line /foo/bar.gop:11:1
+		if _gop_err != nil {
+//line /foo/bar.gop:11:1
+			return 0
+		}
+//line /foo/bar.gop:11:1
+		return
+	}() + func() (_gop_ret int) {
+//line /foo/bar.gop:11:1
+		var _gop_err error
+//line /foo/bar.gop:11:1
+		_gop_ret, _gop_err = strconv.Atoi(y)
+//line /foo/bar.gop:11:1
+		if _gop_err != nil {
+//line /foo/bar.gop:11:1
+			return 0
+		}
+//line /foo/bar.gop:11:1
+		return
+	}()
+}
+//line /foo/bar.gop:14
+func main() {
+//line /foo/bar.gop:14:1
+	fmt.Println(func() (_gop_ret int) {
+//line /foo/bar.gop:14:1
+		var _gop_err error
+//line /foo/bar.gop:14:1
+		_gop_ret, _gop_err = add("100", "23")
+//line /foo/bar.gop:14:1
+		if _gop_err != nil {
+//line /foo/bar.gop:14:1
+			_gop_err = errors.NewFrame(_gop_err, "add(\"100\", \"23\")", "/foo/bar.gop", 14, "main.main")
+//line /foo/bar.gop:14:1
+			panic(_gop_err)
+		}
+//line /foo/bar.gop:14:1
+		return
+	}())
+//line /foo/bar.gop:16:1
+	sum, err := add("10", "abc")
+//line /foo/bar.gop:17:1
+	fmt.Println(sum, err)
+//line /foo/bar.gop:19:1
+	fmt.Println(addSafe("10", "abc"))
+}
+`)
+}