Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coroc: partially compile functions that contain a colored function literal #135

Merged
merged 3 commits into from
Dec 15, 2023

Conversation

chriso
Copy link
Contributor

@chriso chriso commented Dec 15, 2023

This fixes #84.

Copy link
Contributor

@pelletier pelletier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@chriso chriso self-assigned this Dec 15, 2023
@chriso chriso merged commit a514b6e into main Dec 15, 2023
2 checks passed
@chriso chriso deleted the compile-colored-closure-wrappers branch December 15, 2023 22:58
chriso added a commit that referenced this pull request Jun 25, 2024
We don't currently allow `go` statements when compiling functions
because there's no structured concurrency in Go; goroutines are executed
independently (there's no hierarchy; no concept of ownership), and we
are unable to suspend execution of related goroutines when another
goroutine explicitly yields.

The compiler only needs to compile a subset of functions in the program;
those that yield, those that may be on the call stack of a coroutine
that yields, or those that contain a function literal that yields or may
end up on the call stack of a coroutine that yields. All other functions
are allowed to use `go`.

This PR relaxes the restrictions a bit so that certain functions are now
allowed to use `go`.

The major use case we unlock is to allow patterns like this, which seems
to come up a lot:

```go
func outer() {
    coroutine := func(...) { /* e.g. explicitly yields here */ }

    go func() { /* do something unrelated */ }()
}
```

If the compiler determines that the `coroutine` function literal yields
explicitly, or may end up on the call stack of a coroutine that yields
explicitly, it needs to compile both the inner function literal and the
outer function (since
#135). We currently reject
the `go` statement in the outer function, even though it may not
interact with any coroutines.

This PR relaxes the restriction; we allow `go` statements in this one
instance. The goroutine could still mutate state from the outer function
which is then read/mutated within the inner function literal. Given that
we do not support serializing synchronization primitives at this time
(e.g. channels, mutexes), the assumption is that the user isn't doing
anything unsafe and that it's fine to allow the `go` statement. It
prints a warning just in case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

coroc: compiler does not generate coroutines in function literals of uncolored functions
2 participants