GoPatina

How GoPatina compiles Go to Rust

GoPatina compiles a defined subset of Go to Rust: you write ordinary Go, GoPatina's own borrow checker works out ownership in Go terms, and the program is translated into readable Rust that rustc builds into a native binary with no garbage collector. Without a patina flag, GoPatina is plain Go.

What you write on top

Ordinary Go, formatted by gofmt and type-checked by Go's own libraries. Patina code is always valid Go, so the plain Go toolchain can always build it too. A package can keep a patina version beside a plain one with the //go:build patina constraint.

Compiling Go to Rust, underneath

Both patina flags translate the Go program into readable Rust. -emit-rust stops there and writes a cargo crate you can keep and edit. -patina goes on to build that crate with rustc, so the result is a Rust program: no garbage collector and memory safe. rustc and LLVM do the optimising, and Rust's own borrow checker verifies every program GoPatina writes. Calling Rust crates directly is planned.

Where GoPatina can prove that a loop's integer arithmetic cannot wrap, it writes the loop the way hand-written Rust would (RFC 0006), while keeping Go's behaviour exactly; the benchmarks show what that does to the machine code.

A borrow checker for Go

This is where GoPatina aims to save development time: the ownership work a Rust developer does by hand, choosing between moves, clones and borrows and annotating lifetimes until rustc accepts the code, is done by GoPatina on Go code, and explained in Go terms. The aim is Go's development speed with Rust's output; no measurement of development time exists yet.

Before any Rust is written, GoPatina's own borrow checker runs on the Go code. It decides who owns each value and which values are moved, copied or borrowed, and applies fixes that keep the program's meaning. Phase 1 implements moves, copies of strings and small structs, and inferred shared and exclusive borrows; shared ownership and mutexes for values shared across goroutines come with goroutines. Every decision is reported with -explain; gopatina fix, which will write fixes back into the Go source, is planned. When no safe fix exists, the error is in Go terms. This is real output for the project's ownership example:

./examples/ownership/main.go:33:2: m.Body is changed after m was handed to out.Queue on line 32
        out.Queue(m)     // line 32: out.Queue keeps m
        m.Body[0] = 'D'  // line 33: this also changes the copy out.Queue kept, which shares m.Body's memory
    patina mode does not copy m on its own: a copy would not share that memory, so the program could behave differently
    if out.Queue should keep the original, hand it a copy: Message{To: m.To, Body: bytes.Clone(m.Body)}
    or finish using m before handing it over

Go without a garbage collector

In patina mode memory is managed the way Rust manages it: each value has one owner and is freed when its owner goes out of scope, so there is no garbage collector and no collection pause. The borrow checker above is what makes that safe for Go code that was written for a garbage collector.

Drop GoPatina output into a Rust project

A Go package other than main compiles to a Rust library crate (RFC 0014). Its exported functions, types and constants become public Rust items with Rust types: &str and &[T] for what a function only reads, String and Vec<T> for what it keeps, and Result<T, Error> for a Go error. A Rust project depends on it like any other crate and builds, tests and lints it with plain cargo. The crate's api.txt lists its API and says, for each parameter a function takes ownership of, why.

The rust-interop example shows it: a hand-written Rust command-line application whose pricing rules engine is a crate GoPatina generated from Go. CI builds, tests and lints the workspace with cargo alone, regenerates the crate and requires it unchanged, and runs the Rust application against the Go original on the same inputs. The library has to stay inside the subset: no init functions, no package variables it changes, and no interfaces other than error or pointers that escape in its API.

What phase 1 supports

The patina modes accept a defined subset of Go, specified in docs/patina-subset.md:

Not supported yet: goroutines and channels, maps, closures and function values, generics, defer, general slicing (s[i:j]), slices with an explicit high or max bound, saved slice views, and most of the standard library. Every unsupported construct is rejected with a Go-style error that names the construct; nothing is translated approximately. See the feature matrix and every known place where Go and Rust semantics differ.

Goroutines

Goroutines are not supported yet; this part is designed, not built. In patina mode, goroutines are designed to run as tasks on Tokio's multi-threaded runtime, the closest match to Go's own scheduler. Channels map to Tokio channels and select to tokio::select!. The compiler decides which functions must be async, so Go code never says async or await. Data races, which Go finds only at run time with -race, are to become compile errors or be fixed automatically. See the roadmap.

Portability

A patina binary is native code, so the target machine needs neither Go nor Rust installed. GOOS and GOARCH choose the Rust target. Phase 1 builds for each platform's default C runtime (the -gnu targets on Linux); static musl binaries by default, and cross-compiling with a fetched target, are planned.

Compatibility as Go and Rust evolve

Default mode always uses your installed Go, so new Go releases work there immediately. Patina modes support the two latest Go releases and refuse, by name, any Go feature they do not support yet; they never miscompile silently. Generated Rust uses stable Rust only, a declared edition (2024) and a minimum supported Rust version (1.85), and CI tests against Go and Rust release channels every week.

The full design is in RFC 0001. To try it, see Get started.