M0107: Release of a non-owned value
error
What it means
free(), close() or use {} is applied to a value this code does not own: a parameter that is not @Owned, a @Borrowed slot, an alias, a Managed object, or a variable whose ownership was already moved away. The same code reports alias.use { } and passing an alias to an @Owned parameter.
Minimal failing example
From the test corpus: compiler/testdata/memory/free_borrowed_param.kt. The corpus expects 11:5: M0107 (the ^ comment in the source marks the column).
kotlin
// REJECT M0107: a borrowed parameter cannot be released. Spec 4.2, 7.7
@file:MemoryMode(Mode.Strict)
package memory.free_borrowed_param
import mini.memory.*
class Box(val v: Int)
fun drop(b: Box) {
b.free()
// ^ M0107
}
fun main() {
context(Heap()) {
val b = Box(1)
drop(b)
b.free()
}
}Compiler output
Generated by running ./minic check compiler/testdata/memory/free_borrowed_param.kt (exit status 1).
text
$ ./minic check compiler/testdata/memory/free_borrowed_param.kt
error[M0107]: `b` is borrowed and cannot be freed
--> compiler/testdata/memory/free_borrowed_param.kt:11:5 `b` does not own its value
11 | b.free()
| ^
help: free the owner, or declare the parameter `@Owned`
note: M0107 (Release of a non-owned value): https://henryschwerdt.github.io/mini/reference/diagnostics/M0107
error: could not compile free_borrowed_param.kt (1 error, 0 warnings)How to fix
If the function should take over the object, declare the parameter @Owned (and let callers move into it). Otherwise leave the release to the owner.
Corpus programs for this code
borrow_free_via_alias.kt(14:5)free_borrowed_param.kt(11:5), shown abovefree_managed_object.kt(13:5)
See also: Diagnostics overview and the memory guide.