M0109: Loan is stored or returned as owned
error
What it means
A borrowed value (a parameter without @Owned, an alias, a @Borrowed result) flows into an owning field, an owning return, an @Owned argument, or is captured by an escaping lambda. The loan would outlive the owner it was lent from.
Minimal failing example
From the test corpus: compiler/testdata/memory/borrow_returned_owned.kt. The corpus expects 10:27: M0109 (the ^ comment in the source marks the column).
kotlin
// REJECT M0109: a borrowed parameter is returned as an owned value. Spec 4.2, 7.9
@file:MemoryMode(Mode.Strict)
package memory.borrow_returned_owned
import mini.memory.*
class Item(val v: Int)
fun pick(a: Item): Item = a
// ^ M0109
fun main() {
context(Heap()) {
val a = Item(1)
println(pick(a).v)
a.free()
}
}Compiler output
Generated by running ./minic check compiler/testdata/memory/borrow_returned_owned.kt (exit status 1).
text
$ ./minic check compiler/testdata/memory/borrow_returned_owned.kt
error[M0109]: borrowed `a` is returned as owned
--> compiler/testdata/memory/borrow_returned_owned.kt:10:27 `a` is only borrowed
10 | fun pick(a: Item): Item = a
| ^
help: declare the parameter `@Owned` (and move at the call), or use `.move()` on a real owner
note: M0109 (Loan is stored or returned as owned): https://henryschwerdt.github.io/mini/reference/diagnostics/M0109
error: could not compile borrow_returned_owned.kt (1 error, 0 warnings)How to fix
Make the parameter @Owned (and move at the call site), or declare the function's return type @Borrowed so that it returns a loan instead of ownership.
Corpus programs for this code
borrow_alias_stored.kt(17:14)borrow_returned_owned.kt(10:27), shown aboveborrow_stored_field.kt(15:14)
See also: Diagnostics overview and the memory guide.