# Pointers that escape

Until now, GoPatina accepted a Go pointer only as a borrow: `&x` handed to a function that used it and kept nothing. A constructor returning `&T{...}`, a linked list, a tree, or a struct field of pointer type was rejected. [RFC 0013](https://github.com/pigfox/gopatina/pull/86) lifts that limit, and its first two steps are in.

## A pointer is a cell

A pointer that only lends a variable stays a Rust borrow, exactly as before, so programs that compiled already produce the same Rust. A pointer that escapes (one that is returned, stored, reassigned, compared or set to `nil`) becomes a shared cell, `Option<Rc<RefCell<T>>>`, behind one small type in the generated crate. Copying it copies the pointer, `==` compares which variable it points at, and dereferencing `nil` panics with Go's own message.

Each access borrows the cell for that one access only: `p.with(|v| v.f)` to read, `p.with_mut(|v| v.f = x)` to write, with the value computed first. No borrow is ever held while other code runs, so the cell's run-time check cannot fail. Where Go makes the calls of a statement before it reads the variables, the generated code does too.

## Step 1: cells, fields and results

[The first step](https://github.com/pigfox/gopatina/pull/88) covers pointers in variables, parameters, results and struct fields, methods with pointer receivers, and variables whose address escapes. A slice held through a pointer is used inside its cell, because a copy taken out would not share its elements as Go's does; where that would change a program's meaning, GoPatina rejects it by name rather than guess. Freeing a long list frees one cell at a time, so a [list of a million nodes](https://github.com/pigfox/gopatina/tree/main/testdata/golden/pointers_deep) is freed without overflowing the stack.

## Step 2: slices of pointers and interfaces

[The second step](https://github.com/pigfox/gopatina/pull/92) adds slices of pointers and pointers as an interface's dynamic type: method calls through the interface, type assertions, type switches, comparisons, `%T`, and `errors.As` into a `*MyError`. Go prints a pointer by its address, which no other program can reproduce, so GoPatina prints a pointer only through its type's `String` or `Error` method. When that method panics on a nil pointer, Go's fmt prints `<nil>`, and so does GoPatina.

## Known limitation: cycles

Go's garbage collector reclaims a cycle of pointers; a reference count does not. Output is unaffected, but a program that builds and drops cycles, such as a doubly linked list, keeps their memory. A list or a tree that is never closed into a cycle frees everything. The [subset reference](https://github.com/pigfox/gopatina/blob/main/docs/patina-subset.md#pointers) states it, and the third step adds a warning from `-explain` wherever a type can form a cycle, along with a pointer benchmark that measures memory too.

## Defects found on the way

Writing these tests turned up defects, each fixed in its own pull request with golden programs and generator cases: a return of several values that read a package-level variable before a call that changes it ([#87](https://github.com/pigfox/gopatina/pull/87)), and a string parameter in a cell that did not build ([#91](https://github.com/pigfox/gopatina/pull/91)). The [correctness page](/correctness#defects) lists every defect found so far.
