Skip to content

Commit

Permalink
Don't decompose all func result assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Sep 23, 2023
1 parent e24df84 commit c986eda
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
17 changes: 12 additions & 5 deletions compiler/desugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,13 +701,20 @@ func (d *desugarer) decomposeExpression(expr ast.Expr, flags exprFlags) (ast.Exp
// Need to hoist the CallExpr out into a temporary variable in
// this case, so that the relative order of calls (and their
// prerequisites) is preserved.
queue[i] = decompose(e)
} else {
e.Fun = decompose(e.Fun)
for i, arg := range e.Args {
e.Args[i] = decompose(arg)
switch d.info.TypeOf(e).(type) {
case *types.Tuple:
// TODO: can't hoist like this when it's a function
// that returns multiple values
default:
queue[i] = decompose(e)
continue
}
}
e.Fun = decompose(e.Fun)
for i, arg := range e.Args {
e.Args[i] = decompose(arg)
}

case *ast.CompositeLit:
for i, elt := range e.Elts {
e.Elts[i] = decompose(elt)
Expand Down
20 changes: 20 additions & 0 deletions compiler/desugar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,26 @@ _l0:
_v2 := c(_v3)
_v0 <- _v2
}
`,
},
{
name: "FIXME: don't hoist function calls when there are multiple results",
body: "a, b, c = d(e())",
info: func(stmts []ast.Stmt, info *types.Info) {
callExpr := stmts[0].(*ast.AssignStmt).Rhs[0]
info.Types[callExpr] = types.TypeAndValue{
Type: types.NewTuple(
types.NewVar(0, nil, "a", intType),
types.NewVar(0, nil, "b", intType),
types.NewVar(0, nil, "c", intType),
),
}
},
expect: `
{
_v0 := e()
a, b, c = d(_v0)
}
`,
},
} {
Expand Down

0 comments on commit c986eda

Please sign in to comment.