M0104: Use after move
error
What it means
A value is used after ownership moved away from it: through x.move(), by passing it to an @Owned parameter, by storing it in an owning field, by capturing it in a thread lambda, or by a use {}/close() that consumed it. A move inside a loop that is hit again on the next iteration is reported too.
Minimal failing example
From the test corpus: compiler/testdata/memory/move_explicit.kt. The corpus expects 14:13: M0104 (the ^ comment in the source marks the column).
// REJECT M0104: `val b = a.move()` moves; `a` is dead afterwards. Spec 5.1, 7.4
@file:MemoryMode(Mode.Strict)
package memory.move_explicit
import mini.memory.*
class Box(val v: Int)
context(h: Heap)
fun run() {
val a = Box(1)
val b = a.move()
println(a.v)
// ^ M0104
b.free()
}
fun main() {
context(Heap()) { run() }
}Compiler output
Generated by running ./minic check compiler/testdata/memory/move_explicit.kt (exit status 1).
$ ./minic check compiler/testdata/memory/move_explicit.kt
error[M0104]: use of moved value `a`
--> compiler/testdata/memory/move_explicit.kt:14:13 `a` was moved before
14 | println(a.v)
| ^
help: use the new owner instead, or write `val b = a` (alias) when no move is intended
note: M0104 (Use after move): https://henryschwerdt.github.io/mini/reference/diagnostics/M0104
error: could not compile move_explicit.kt (1 error, 0 warnings)How to fix
Use the new owner instead of the old variable, or write val b = a (a borrowed alias) if no transfer was intended. Inside loops, re-initialise the variable before the back edge.
Corpus programs for this code
borrow_scope_moved.kt(22:13)borrow_use_after_move.kt(19:13)move_explicit.kt(14:13), shown abovemove_into_field.kt(17:13)move_loop.kt(18:17)move_param.kt(18:13)move_thread.kt(17:13)
See also: Diagnostics overview and the memory guide.