Skip to content

Commit

Permalink
TestCallInlineClosure: ReturnErr(outer=true)
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jul 17, 2021
1 parent ac2876b commit c62e230
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
29 changes: 21 additions & 8 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (p *CodeBuilder) ReturnErr(outer bool) *CodeBuilder {
if last.Type() == TyError { // last result is error
err := p.stk.Pop()
for i := 0; i < n-1; i++ {
p.ZeroLit(results.At(i).Type())
p.doZeroLit(results.At(i).Type(), false)
}
p.stk.Push(err)
p.returnResults(n)
Expand Down Expand Up @@ -223,9 +223,9 @@ func (p *CodeBuilder) Return(n int) *CodeBuilder {
for i := n - 1; i >= 0; i-- {
key := closureParamInst{fn, results.At(i)}
elem := p.stk.Pop()
p.VarRef(p.paramInsts[key])
p.doVarRef(p.paramInsts[key], false)
p.stk.Push(elem)
p.Assign(1)
p.doAssign(1, nil, false)
}
p.Goto(p.getEndingLabel(fn))
} else {
Expand Down Expand Up @@ -420,8 +420,12 @@ func (p *CodeBuilder) NewAutoVar(name string, pv **types.Var) *CodeBuilder {

// VarRef func: p.VarRef(nil) means underscore (_)
func (p *CodeBuilder) VarRef(ref interface{}) *CodeBuilder {
return p.doVarRef(ref, true)
}

func (p *CodeBuilder) doVarRef(ref interface{}, allowDebug bool) *CodeBuilder {
if ref == nil {
if debug {
if allowDebug && debug {
log.Println("VarRef _")
}
p.stk.Push(internal.Elem{
Expand All @@ -430,7 +434,7 @@ func (p *CodeBuilder) VarRef(ref interface{}) *CodeBuilder {
} else {
switch v := ref.(type) {
case *types.Var:
if debug {
if allowDebug && debug {
log.Println("VarRef", v.Name())
}
fn := p.current.fn
Expand Down Expand Up @@ -460,8 +464,13 @@ func (p *CodeBuilder) None() *CodeBuilder {
return p
}

// ZeroLit func
func (p *CodeBuilder) ZeroLit(typ types.Type) *CodeBuilder {
if debug {
return p.doZeroLit(typ, true)
}

func (p *CodeBuilder) doZeroLit(typ types.Type, allowDebug bool) *CodeBuilder {
if allowDebug && debug {
log.Println("ZeroLit")
}
switch t := typ.(type) {
Expand Down Expand Up @@ -916,14 +925,18 @@ func indirect(typ types.Type) types.Type {
}

// Assign func
func (p *CodeBuilder) Assign(lhs int, v ...int) *CodeBuilder {
func (p *CodeBuilder) Assign(lhs int, rhs ...int) *CodeBuilder {
return p.doAssign(lhs, rhs, true)
}

func (p *CodeBuilder) doAssign(lhs int, v []int, allowDebug bool) *CodeBuilder {
var rhs int
if v != nil {
rhs = v[0]
} else {
rhs = lhs
}
if debug {
if allowDebug && debug {
log.Println("Assign", lhs, rhs)
}
args := p.stk.GetArgs(lhs + rhs)
Expand Down
13 changes: 10 additions & 3 deletions package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1572,11 +1572,15 @@ func TestCallInlineClosure(t *testing.T) {
pkg := newMainPackage()
fmt := pkg.Import("fmt")
ret := pkg.NewAutoParam("ret")
sig := types.NewSignature(nil, nil, gox.NewTuple(ret), false)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
err := pkg.NewParam("", gox.TyError)
sig := types.NewSignature(nil, nil, types.NewTuple(ret), false)
pkg.NewFunc(nil, "foo", nil, types.NewTuple(err), false).BodyStart(pkg).
Val(fmt.Ref("Println")).
CallInlineClosureStart(sig, 0, false).
/**/ DefineVarStart("n", "err").Val(fmt.Ref("Println")).Val("Hi").Call(1).EndInit(1).
/**/ If().Val(ctxRef(pkg, "err")).CompareNil(token.NEQ).Then().
/******/ Val(ctxRef(pkg, "err")).ReturnErr(true).
/******/ End().
/**/ Val(ctxRef(pkg, "n")).Return(1).
/**/ End().
Call(1).EndStmt().
Expand All @@ -1585,10 +1589,13 @@ func TestCallInlineClosure(t *testing.T) {
import fmt "fmt"
func main() {
func foo() error {
var _autoGo_1 int
{
n, err := fmt.Println("Hi")
if err != nil {
return err
}
_autoGo_1 = n
goto _autoGo_2
_autoGo_2:
Expand Down

0 comments on commit c62e230

Please sign in to comment.