Skip to content

Commit

Permalink
fix: compile non-coroutine functions
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <[email protected]>
  • Loading branch information
achille-roussel committed Sep 21, 2023
1 parent b0d4759 commit 145d380
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
35 changes: 18 additions & 17 deletions compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func (c *compiler) compilePackage(p *packages.Package, colors functionColors, pr
}
color, ok := colorsByDecl[decl]
if !ok {
gen.Decls = append(gen.Decls, decl)
continue
}
// Reject certain language features for now.
Expand Down Expand Up @@ -322,6 +323,23 @@ func (scope *scope) compileFuncBody(p *packages.Package, typ *ast.FuncType, body
yieldTypeExpr[0] = typeExpr(color.Params().At(0).Type())
yieldTypeExpr[1] = typeExpr(color.Results().At(0).Type())

body = astutil.Apply(body,
func(cursor *astutil.Cursor) bool {
switch n := cursor.Node().(type) {
case *ast.FuncLit:
color, ok := scope.colors[n]
if ok {
cursor.Replace(scope.compileFuncLit(p, n, color))
}
}
return true
},
nil,
).(*ast.BlockStmt)

// Desugar statements in the tree.
body = desugar(body, p.TypesInfo).(*ast.BlockStmt)

// _c := coroutine.LoadContext[R, S]()
gen.List = append(gen.List, &ast.AssignStmt{
Lhs: []ast.Expr{ctx},
Expand Down Expand Up @@ -350,23 +368,6 @@ func (scope *scope) compileFuncBody(p *packages.Package, typ *ast.FuncType, body
},
})

body = astutil.Apply(body,
func(cursor *astutil.Cursor) bool {
switch n := cursor.Node().(type) {
case *ast.FuncLit:
color, ok := scope.colors[n]
if ok {
cursor.Replace(scope.compileFuncLit(p, n, color))
}
}
return true
},
nil,
).(*ast.BlockStmt)

// Desugar statements in the tree.
body = desugar(body, p.TypesInfo).(*ast.BlockStmt)

// Handle declarations.
//
// Types, constants and variables can be defined within any scope in the
Expand Down
6 changes: 6 additions & 0 deletions compiler/coroutine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/stealthrocket/coroutine/types"
)

func init() {
// Breaks if the compiler did not retain simple top-level functions in the
// output file.
SomeFunctionThatShouldExistInTheCompiledFile()
}

func TestCoroutineYield(t *testing.T) {
tests := []struct {
name string
Expand Down
3 changes: 3 additions & 0 deletions compiler/testdata/coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (

//go:generate coroc --output coroutine_durable.go --tags durable

func SomeFunctionThatShouldExistInTheCompiledFile() {
}

func Identity(n int) {
coroutine.Yield[int, any](n)
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/testdata/coroutine_durable.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiler/testdata/coroutine_functypes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 145d380

Please sign in to comment.