Skip to content

Commit

Permalink
Merge pull request #1286 from xushiwei/cls
Browse files Browse the repository at this point in the history
TestErrWrapCall/TestErrWrapCommand
  • Loading branch information
xushiwei authored Jun 18, 2022
2 parents 188672b + ee4f889 commit bc78f3d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
5 changes: 5 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ func (x *CallExpr) End() token.Pos {
return x.Rparen + 1
}

// IsCommand returns if a CallExpr is a command style CallExpr or not.
func (x *CallExpr) IsCommand() bool {
return x.NoParenEnd != token.NoPos
}

// End returns position of first character immediately after the node.
func (x *StarExpr) End() token.Pos { return x.X.End() }

Expand Down
2 changes: 1 addition & 1 deletion cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestCompileErrWrapExpr(t *testing.T) {
}()
pkg := gox.NewPackage("", "foo", goxConf)
ctx := &blockCtx{pkg: pkg, cb: pkg.CB()}
compileErrWrapExpr(ctx, &ast.ErrWrapExpr{Tok: token.QUESTION})
compileErrWrapExpr(ctx, &ast.ErrWrapExpr{Tok: token.QUESTION}, 0)
}

func TestToString(t *testing.T) {
Expand Down
52 changes: 51 additions & 1 deletion cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@ func foo(script string) {
`)
}

func TestErrWrap(t *testing.T) {
func TestErrWrapBasic(t *testing.T) {
gopClTest(t, `
import "strconv"
Expand Down Expand Up @@ -1497,6 +1497,56 @@ var ret int = func() (_gop_ret int) {
`)
}

func TestErrWrapCommand(t *testing.T) {
gopClTest(t, `
func mkdir(name string) error {
return nil
}
mkdir! "foo"
`, `package main
func mkdir(name string) error {
return nil
}
func main() {
func() {
var _gop_err error
_gop_err = mkdir("foo")
if _gop_err != nil {
panic(_gop_err)
}
return
}()
}
`)
}

func TestErrWrapCall(t *testing.T) {
gopClTest(t, `
func foo() (func(), error) {
return nil, nil
}
foo()!()
`, `package main
func foo() (func(), error) {
return nil, nil
}
func main() {
func() (_gop_ret func()) {
var _gop_err error
_gop_ret, _gop_err = foo()
if _gop_err != nil {
panic(_gop_err)
}
return
}()()
}
`)
}

func TestMakeAndNew(t *testing.T) {
gopClTest(t, `
var a *int = new(int)
Expand Down
16 changes: 13 additions & 3 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func compileExpr(ctx *blockCtx, expr ast.Expr, inFlags ...int) {
case *ast.ParenExpr:
compileExpr(ctx, v.X, inFlags...)
case *ast.ErrWrapExpr:
compileErrWrapExpr(ctx, v)
compileErrWrapExpr(ctx, v, 0)
case *ast.FuncType:
ctx.cb.Typ(toFuncType(ctx, v, nil), v)
case *ast.Ellipsis:
Expand Down Expand Up @@ -468,6 +468,16 @@ func compileCallExpr(ctx *blockCtx, v *ast.CallExpr, inFlags int) {
compileIdent(ctx, fn, clIdentAllowBuiltin|inFlags)
case *ast.SelectorExpr:
compileSelectorExpr(ctx, fn, 0)
case *ast.ErrWrapExpr:
if v.IsCommand() {
callExpr := *v
callExpr.Fun = fn.X
ewExpr := *fn
ewExpr.X = &callExpr
compileErrWrapExpr(ctx, &ewExpr, inFlags)
return
}
compileErrWrapExpr(ctx, fn, 0)
default:
compileExpr(ctx, fn)
}
Expand Down Expand Up @@ -956,14 +966,14 @@ var (
tyError = types.Universe.Lookup("error").Type()
)

func compileErrWrapExpr(ctx *blockCtx, v *ast.ErrWrapExpr) {
func compileErrWrapExpr(ctx *blockCtx, v *ast.ErrWrapExpr, inFlags int) {
pkg, cb := ctx.pkg, ctx.cb
useClosure := v.Tok == token.NOT || v.Default != nil
if !useClosure && (cb.Scope().Parent() == types.Universe) {
panic("TODO: can't use expr? in global")
}

compileExpr(ctx, v.X)
compileExpr(ctx, v.X, inFlags)
x := cb.InternalStack().Pop()
n := 0
results, ok := x.Type.(*types.Tuple)
Expand Down

0 comments on commit bc78f3d

Please sign in to comment.