Skip to content

Commit

Permalink
Closures expect an extra field for the dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Dec 13, 2023
1 parent 11f3430 commit 4a7de73
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
20 changes: 14 additions & 6 deletions compiler/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,18 @@ func collectFunctypes(p *packages.Package, name string, fn ast.Node, scope *func
signature: signature,
}
if len(freeVars) > 0 {
fields := make([]*ast.Field, 1+len(freeVars))
fields[0] = &ast.Field{
Type: ast.NewIdent("uintptr"),
Names: []*ast.Ident{ast.NewIdent("F")},
fields := []*ast.Field{
{
Type: ast.NewIdent("uintptr"),
Names: []*ast.Ident{ast.NewIdent("F")},
},
}
if g != nil {
// Append a field for the dictionary.
fields = append(fields, &ast.Field{
Type: ast.NewIdent("uintptr"),
Names: []*ast.Ident{ast.NewIdent("D")},
})
}
for i, freeVar := range freeVars {
fieldName := ast.NewIdent(fmt.Sprintf("X%d", i))
Expand All @@ -191,10 +199,10 @@ func collectFunctypes(p *packages.Package, name string, fn ast.Node, scope *func
// and pointers will be less than 128 bytes on all platforms, which
// means that the stack frame pointer is always captured by value.

fields[i+1] = &ast.Field{
fields = append(fields, &ast.Field{
Type: fieldType,
Names: []*ast.Ident{fieldName},
}
})
}
functype.closure = &ast.StructType{
Fields: &ast.FieldList{List: fields},
Expand Down
3 changes: 2 additions & 1 deletion compiler/testdata/coroutine_durable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3255,7 +3255,7 @@ func IdentityGenericStructInt(n int) { (&IdentityGenericStruct[int]{n: n}).Run()

//go:noinline
func IdentityGenericClosure[T any](_fn0 T) {
_c := coroutine.LoadContext[int, any]()
_c := coroutine.LoadContext[T, any]()
var _f0 *struct {
IP int
X0 T
Expand Down Expand Up @@ -3441,6 +3441,7 @@ func init() {
_types.RegisterFunc[func(n int) func()]("github.com/stealthrocket/coroutine/compiler/testdata.buildClosure[go.shape.int]")
_types.RegisterClosure[func(), struct {
F uintptr
D uintptr
X0 int
}]("github.com/stealthrocket/coroutine/compiler/testdata.buildClosure[go.shape.int].func1")
_types.RegisterFunc[func(_fn0 ...int)]("github.com/stealthrocket/coroutine/compiler/testdata.varArgs")
Expand Down

0 comments on commit 4a7de73

Please sign in to comment.