Repository: goplus/gop
Pull request #1347 (2022-08-14 18:38:07)

Error wrapping
With these changes, the following code
```
import (
	"strconv"
)

const str = "test"

func foo(arg string) (int, error) {
	return strconv.Atoi(arg)?, nil
}

func bar(arg string) (int, error) {
	fn := foo

	return fn(arg)?, nil
}

func baz(arg string) (int, error) {
	return bar(arg)?, nil
}

println baz(str)
```

will print the following
```
0 strconv.Atoi: parsing "test": invalid syntax

===> errors stack:
main.foo()
        cmd/test/main.gop:8 strconv.Atoi(arg)
main.bar()
        cmd/test/main.gop:14 fn(arg)
main.baz()
        cmd/test/main.gop:18 bar(arg)
```


Patch:
diff --git a/README.md b/README.md
index 2dc330a5..4bab1e3d 100644
--- a/README.md
+++ b/README.md
@@ -101,8 +101,3 @@ brew install goplus
 The Go+ project welcomes all contributors. We appreciate your help!
 
 For more details, see [Contributing & compiler design](doc/contributing.md).
-
-
-## Give a Star! ⭐
-
-If you like or are using Go+ to learn or start your projects, please give it a star. Thanks!
diff --git a/VERSION b/VERSION
new file mode 100644
index 00000000..99a4aef0
--- /dev/null
+++ b/VERSION
@@ -0,0 +1,1 @@
+v1.1.3
diff --git a/cl/expr.go b/cl/expr.go
index 7e3b1bc2..6fcad90f 100644
--- a/cl/expr.go
+++ b/cl/expr.go
@@ -17,6 +17,8 @@
 package cl
 
 import (
+	"bytes"
+	"github.com/goplus/gop/printer"
 	goast "go/ast"
 	gotoken "go/token"
 	"go/types"
@@ -70,6 +72,8 @@ const (
 	objCPkgRef
 )
 
+const errorPkgPath = "github.com/qiniu/x/errors"
+
 func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) (obj *gox.PkgRef, kind int) {
 	fvalue := (flags&clIdentSelectorExpr) != 0 || (flags&clIdentLHS) == 0
 	name := ident.Name
@@ -1012,10 +1016,34 @@ func compileErrWrapExpr(ctx *blockCtx, v *ast.ErrWrapExpr, inFlags int) {
 	cb.Assign(n+1, 1)
 
 	cb.If().Val(err).CompareNil(gotoken.NEQ).Then()
+	if v.Default == nil {
+		pos := pkg.Fset.Position(v.Pos())
+		currentFunc := ctx.cb.Func().Ancestor()
+		const newFrameArgs = 5
+
+		currentFuncName := currentFunc.Name()
+		if currentFuncName == "" {
+			currentFuncName = "main"
+		}
+
+		currentFuncName = strings.Join([]string{currentFunc.Pkg().Name(), currentFuncName}, ".")
+
+		cb.
+			VarRef(err).
+			Val(pkg.Import(errorPkgPath).Ref("NewFrame")).
+			Val(err).
+			Val(sprintAst(pkg.Fset, v.X)).
+			Val(pos.Filename).
+			Val(pos.Line).
+			Val(currentFuncName).
+			Call(newFrameArgs).
+			Assign(1)
+	}
+
 	if v.Tok == token.NOT { // expr!
-		cb.Val(pkg.Builtin().Ref("panic")).Val(err).Call(1).EndStmt() // TODO: wrap err
+		cb.Val(pkg.Builtin().Ref("panic")).Val(err).Call(1).EndStmt()
 	} else if v.Default == nil { // expr?
-		cb.Val(err).ReturnErr(true) // TODO: wrap err & return err
+		cb.Val(err).ReturnErr(true)
 	} else { // expr?:val
 		compileExpr(ctx, v.Default)
 		cb.Return(1)
@@ -1026,4 +1054,15 @@ func compileErrWrapExpr(ctx *blockCtx, v *ast.ErrWrapExpr, inFlags int) {
 	}
 }
 
+func sprintAst(fset *token.FileSet, x ast.Node) string {
+	var buf bytes.Buffer
+	err := printer.Fprint(&buf, fset, x)
+	if err != nil {
+		panic("Unexpected error: " + err.Error())
+	}
+
+	return buf.String()
+}
+
+
 // -----------------------------------------------------------------------------
diff --git a/doc/goplus-vs-go.md b/doc/goplus-vs-go.md
index 1fdddaf9..6aca7d2f 100644
--- a/doc/goplus-vs-go.md
+++ b/doc/goplus-vs-go.md
@@ -14,7 +14,3 @@ The Python language appeared very early (1991), but its design idea is very far-
 Python is very popular today, but it's hard to call it an engineering language. Most people just take it as a tool for rapid prototyping.
 
 Is it possible to minimize the complexity of engineering and present it in a low-code form? Go+ hopes to explore a new path at this point.
-
-### Give a Star! ⭐
-
-If you like or are using Go+ to learn or start your projects, please give it a star. Thanks!
diff --git a/go.mod b/go.mod
index 53f36f68..b57d9558 100644
--- a/go.mod
+++ b/go.mod
@@ -4,8 +4,8 @@ go 1.16
 
 require (
 	github.com/goplus/c2go v0.7.10
-	github.com/goplus/gox v1.11.22
-	github.com/goplus/libc v0.3.13
+	github.com/goplus/gox v1.11.21
+	github.com/goplus/libc v0.3.12
 	github.com/goplus/mod v0.9.12
 	github.com/qiniu/x v1.11.9
 )
diff --git a/go.sum b/go.sum
index 74cacaeb..c5362add 100644
--- a/go.sum
+++ b/go.sum
@@ -4,11 +4,10 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
 github.com/goplus/c2go v0.7.10 h1:ov8ubqk39iVPnFNmw6GMIvIVVWFqaOEgd4Olk81gcrE=
 github.com/goplus/c2go v0.7.10/go.mod h1:ryOieVZqh0jmlPp45AMh9/sXIreTnyjDREP0EMX+pF0=
+github.com/goplus/gox v1.11.21 h1:zxWg7kZRDnLSxJZ4G8XPIlbvdGugrB3jgRb+MzV296o=
 github.com/goplus/gox v1.11.21/go.mod h1:wRCRSNukie4cDqADF4w0Btc2Gk6V3p3V6hI5+rsVqa8=
-github.com/goplus/gox v1.11.22 h1:XlDPMTEatJN/jMEcyG5mkIeo1+ZxB/BNKJcW0LqoTgc=
-github.com/goplus/gox v1.11.22/go.mod h1:MiKw6Dv4R5sRXYT3r4WCMIOnHasBHomfDAjDv54dHq4=
-github.com/goplus/libc v0.3.13 h1:2yHG8ghezBTK3Da7Fw1lNiitmt+Va+RzwKJ5GuqypCQ=
-github.com/goplus/libc v0.3.13/go.mod h1:xqG4/g3ilKBE/UDn5vkaE7RRQPQPyspj7ecuMuvlQJ8=
+github.com/goplus/libc v0.3.12 h1:A0OeavtivMwHHo4te2GMg3OpLrTRc7T/t5sFCLjVIhU=
+github.com/goplus/libc v0.3.12/go.mod h1:xqG4/g3ilKBE/UDn5vkaE7RRQPQPyspj7ecuMuvlQJ8=
 github.com/goplus/mod v0.9.12 h1:CjgBGQIYqUTPGl3MrAS5CICzJwxbIfSa4OlEb141Gs4=
 github.com/goplus/mod v0.9.12/go.mod h1:YoPIowz71rnLLROA4YG0AC8bzDtPRyMaQwgTRLr8ri4=
 github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -27,7 +26,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
 github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
-github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
 golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
@@ -35,27 +33,21 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91
 golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
 golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
-golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
 golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
-golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
 golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
-golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
-golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY=
 golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
-golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
-golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=


Test patch:
diff --git a/cl/compile_test.go b/cl/compile_test.go
index 064fedf6..c0fd8241 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -148,6 +148,7 @@ import (
 	fmt "fmt"
 	os "os"
 	iox "github.com/goplus/gop/builtin/iox"
+	errors "github.com/qiniu/x/errors"
 )
 
 func main() {
@@ -155,6 +156,7 @@ func main() {
 		var _gop_err error
 		_gop_ret, _gop_err = os.Open("foo.txt")
 		if _gop_err != nil {
+			_gop_err = errors.NewFrame(_gop_err, "open(\"foo.txt\")", "/foo/bar.gop", 2, "main.main")
 			panic(_gop_err)
 		}
 		return
@@ -912,7 +914,10 @@ func main() {
 	println(a, b)
 }`, `package main
 
-import fmt "fmt"
+import (
+	fmt "fmt"
+	errors "github.com/qiniu/x/errors"
+)
 
 func t() (int, int, error) {
 	return 0, 0, nil
@@ -922,6 +927,7 @@ func main() {
 		var _gop_err error
 		_gop_ret, _gop_ret2, _gop_err = t()
 		if _gop_err != nil {
+			_gop_err = errors.NewFrame(_gop_err, "t()", "/foo/bar.gop", 9, "main.main")
 			panic(_gop_err)
 		}
 		return
@@ -943,6 +949,8 @@ func main() {
 	t()!
 }`, `package main
 
+import errors "github.com/qiniu/x/errors"
+
 func t() error {
 	return nil
 }
@@ -951,6 +959,7 @@ func main() {
 		var _gop_err error
 		_gop_err = t()
 		if _gop_err != nil {
+			_gop_err = errors.NewFrame(_gop_err, "t()", "/foo/bar.gop", 9, "main.main")
 			panic(_gop_err)
 		}
 		return
@@ -1454,6 +1463,7 @@ func foo(script string) {
 import (
 	fmt "fmt"
 	goptest "github.com/goplus/gop/ast/goptest"
+	errors "github.com/qiniu/x/errors"
 	gopq "github.com/goplus/gop/ast/gopq"
 )
 
@@ -1462,6 +1472,7 @@ func foo(script string) {
 		var _gop_err error
 		_gop_ret, _gop_err = goptest.New(script)
 		if _gop_err != nil {
+			_gop_err = errors.NewFrame(_gop_err, "goptest.New(script)", "/foo/bar.gop", 4, "main.foo")
 			panic(_gop_err)
 		}
 		return
@@ -1486,6 +1497,7 @@ func foo(script string) {
 import (
 	fmt "fmt"
 	goptest "github.com/goplus/gop/ast/goptest"
+	errors "github.com/qiniu/x/errors"
 	gopq "github.com/goplus/gop/ast/gopq"
 )
 
@@ -1494,6 +1506,7 @@ func foo(script string) {
 		var _gop_err error
 		_gop_ret, _gop_err = goptest.New(script)
 		if _gop_err != nil {
+			_gop_err = errors.NewFrame(_gop_err, "goptest.New(script)", "/foo/bar.gop", 4, "main.foo")
 			panic(_gop_err)
 		}
 		return
@@ -1513,7 +1526,10 @@ func add(x, y string) (int, error) {
 }
 `, `package main
 
-import strconv "strconv"
+import (
+	strconv "strconv"
+	errors "github.com/qiniu/x/errors"
+)
 
 func add(x string, y string) (int, error) {
 	var _autoGo_1 int
@@ -1521,6 +1537,7 @@ func add(x string, y string) (int, error) {
 		var _gop_err error
 		_autoGo_1, _gop_err = strconv.Atoi(x)
 		if _gop_err != nil {
+			_gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(x)", "/foo/bar.gop", 5, "main.add")
 			return 0, _gop_err
 		}
 		goto _autoGo_2
@@ -1531,6 +1548,7 @@ func add(x string, y string) (int, error) {
 		var _gop_err error
 		_autoGo_3, _gop_err = strconv.Atoi(y)
 		if _gop_err != nil {
+			_gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(y)", "/foo/bar.gop", 5, "main.add")
 			return 0, _gop_err
 		}
 		goto _autoGo_4
@@ -1577,12 +1595,16 @@ func TestErrWrapPanic(t *testing.T) {
 var ret int = println("Hi")!
 `, `package main
 
-import fmt "fmt"
+import (
+	fmt "fmt"
+	errors "github.com/qiniu/x/errors"
+)
 
 var ret int = func() (_gop_ret int) {
 	var _gop_err error
 	_gop_ret, _gop_err = fmt.Println("Hi")
 	if _gop_err != nil {
+		_gop_err = errors.NewFrame(_gop_err, "println(\"Hi\")", "/foo/bar.gop", 2, "main.main")
 		panic(_gop_err)
 	}
 	return
@@ -1599,6 +1621,8 @@ func mkdir(name string) error {
 mkdir! "foo"
 `, `package main
 
+import errors "github.com/qiniu/x/errors"
+
 func mkdir(name string) error {
 	return nil
 }
@@ -1607,6 +1631,7 @@ func main() {
 		var _gop_err error
 		_gop_err = mkdir("foo")
 		if _gop_err != nil {
+			_gop_err = errors.NewFrame(_gop_err, "mkdir \"foo\"", "/foo/bar.gop", 6, "main.main")
 			panic(_gop_err)
 		}
 		return
@@ -1624,6 +1649,8 @@ func foo() (func(), error) {
 foo()!()
 `, `package main
 
+import errors "github.com/qiniu/x/errors"
+
 func foo() (func(), error) {
 	return nil, nil
 }
@@ -1632,6 +1659,7 @@ func main() {
 		var _gop_err error
 		_gop_ret, _gop_err = foo()
 		if _gop_err != nil {
+			_gop_err = errors.NewFrame(_gop_err, "foo()", "/foo/bar.gop", 6, "main.main")
 			panic(_gop_err)
 		}
 		return
diff --git a/testdata/helloc2go/README.md b/testdata/helloc2go/README.md
index 2071d303..b94b584a 100644
--- a/testdata/helloc2go/README.md
+++ b/testdata/helloc2go/README.md
@@ -24,7 +24,3 @@ Run `gop run .` to see the output of this example:
 Hello, c2go!
 Hi,     3.1
 ```
-
-### Give a Star! ⭐
-
-If you like or are using Go+ to learn or start your projects, please give it a star. Thanks!
diff --git a/testdata/mixgo/README.md b/testdata/mixgo/README.md
index a995f3df..12edcc48 100644
--- a/testdata/mixgo/README.md
+++ b/testdata/mixgo/README.md
@@ -31,7 +31,3 @@ Run `gop run .` to see the output of this example:
 Mix Go and Go+
 Hello, world
 ```
-
-### Give a Star! ⭐
-
-If you like or are using Go+ to learn or start your projects, please give it a star. Thanks!